Skip to content

Commit

Permalink
Added plugin version checking
Browse files Browse the repository at this point in the history
  • Loading branch information
HoshiKurama committed Jun 19, 2021
1 parent 2a744fb commit eecb696
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ sealed class TMLocale(colourCode: String, locale: String) {
val notifyTicketReopenEvent: String
val notifyTicketSetPrioritySuccess: String
val notifyTicketSetPriorityEvent: String
val notifyPluginUpdate: String

// Information:
val informationReloadInitiated: String
Expand Down Expand Up @@ -383,6 +384,7 @@ sealed class TMLocale(colourCode: String, locale: String) {
warningsUnexpectedError = matchOrDefault("Warning_UnexpectedError")
searchClosedBy = matchOrDefault("Search_ClosedBy")
searchLastClosedBy = matchOrDefault("Search_LastClosedBy")
notifyPluginUpdate = matchOrDefault("Notify_Event_PluginUpdate")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,4 +807,22 @@ class Metrics(plugin: JavaPlugin, serviceId: Int) {
logResponseStatusText
)
}
}

internal class UpdateChecker(private val resourceID: Int) {
internal fun getLatestVersion(): String? {
var inputStream: InputStream? = null
var scanner: Scanner? = null

return try {
inputStream = URL("https://api.spigotmc.org/legacy/update.php?resource=$resourceID").openStream()
scanner = Scanner(inputStream!!)
if (scanner.hasNext()) scanner.next() else null
}
catch (ignored: Exception) { null }
finally {
inputStream?.close()
scanner?.close()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import com.hoshikurama.github.ticketmanager.events.TabCompletePaper
import com.hoshikurama.github.ticketmanager.events.TabCompleteSpigot
import org.bukkit.Bukkit
import java.io.File
import java.io.InputStream
import java.net.URL
import java.time.Instant
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicReference
import java.util.logging.Level

class PluginState {
Expand All @@ -19,6 +22,7 @@ class PluginState {
internal val database: Database
internal val serverType: ServerType
internal val allowUnreadTicketUpdates: Boolean
internal val updateAvailable: AtomicReference<Pair<String, String>?> = AtomicReference(null)

init {
mainPlugin.pluginLocked = true
Expand Down Expand Up @@ -61,6 +65,18 @@ class PluginState {
} ?: SQLite()

allowUnreadTicketUpdates = getBoolean("Allow_Unread_Ticket_Updates", true)

// Assigns update available
val allowUpdateCheck = getBoolean("Allow_UpdateChecking", false)
if (allowUpdateCheck) {
Bukkit.getScheduler().runTaskAsynchronously(mainPlugin, Runnable {

val curVersion = mainPlugin.description.version
val latestVersion = UpdateChecker(91178).getLatestVersion()
.run { this ?: curVersion }
updateAvailable.set(curVersion to latestVersion)
})
} else updateAvailable.set(null)
}

serverType = tryOrNull {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ class PlayerJoin : Listener {
if (anyLocksPresent()) return
val player = event.player

// Plugin Update checking
val pluginUpdateStatus = pluginState.updateAvailable.get()
if (player.has("ticketmanager.notify.pluginUpdate") && pluginUpdateStatus != null) {
if (pluginUpdateStatus.first.replace(".", "").toInt() < pluginUpdateStatus.second.replace(".", "").toInt()) {
getLocale(player).notifyPluginUpdate
.replace("%current%", pluginUpdateStatus.first)
.replace("%latest%", pluginUpdateStatus.second)
.run { sendColouredMessageTo(player) }
}
}

// Unread Updates
if (player.has("ticketmanager.notify.unreadUpdates.onJoin")) {
pluginState.database.getTicketIDsWithUpdates(player.uniqueId)
Expand All @@ -30,7 +41,8 @@ class PlayerJoin : Listener {
// View Open-Count and Assigned-Count Tickets
if (player.has("ticketmanager.notify.openTickets.onJoin")) {
val open = pluginState.database.getOpen().size.toString()
val assigned = pluginState.database.getOpenAssigned(player.name, mainPlugin.perms.getPlayerGroups(player).toList())
val assigned =
pluginState.database.getOpenAssigned(player.name, mainPlugin.perms.getPlayerGroups(player).toList())
.count().toString()

getLocale(player).run { notifyOpenAssigned }
Expand Down
10 changes: 7 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Console_Locale: 'en_CA'
# Use Cool-down: Determine if cool-downs should be applied to ticket commands
# that create or modify a ticket. Cool-downs apply to ALL users without the
# override permission.
# Values: 'true', 'false'
# Values: true, false
Use_Cooldowns: false
#
# Ticket Modification Cool-down: Time in seconds before a user is able to create or
Expand Down Expand Up @@ -78,5 +78,9 @@ Colour_Code: '&3'
# This is NOT the same alert one immediately receives on ticket
# status change. Setting value to 'false' prevents TicketManager
# from flagging a ticket as unread.
# Values: 'true', 'false'
Allow_Unread_Ticket_Updates: true
# Values: true, false
Allow_Unread_Ticket_Updates: true
#
# Allow Update Checking: Server can check that the latest version of TicketManager is installed during startup or reloads.
# Values: true, false
Allow_UpdateChecking: true
3 changes: 2 additions & 1 deletion src/main/resources/languages/Locales/Example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,5 @@ Notify_Event_TicketComment: '%user% %id% %message%'
Notify_Event_TicketCreation: '%user% %id% %%message%'
Notify_Event_TicketModification: '%id%'
Notify_Event_TicketReopen: '%user% %id%'
Notify_Event_SetPriority: '%user% %id% %priority%'
Notify_Event_SetPriority: '%user% %id% %priority%'
Notify_Event_PluginUpdate: '%current% %latest%'
3 changes: 2 additions & 1 deletion src/main/resources/languages/Locales/en_CA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,5 @@ Notify_Event_TicketComment: '%CC%[TicketManager]&7 %user%%CC% commented on ticke
Notify_Event_TicketCreation: '%CC%[TicketManager] &7%user%%CC% created ticket &7#%id%%CC%:&7%nl%%message%'
Notify_Event_TicketModification: '%CC%[TicketManager] Ticket &7#%id%%CC% has been updated! Type &7/ticket view %id%%CC% to view this ticket.'
Notify_Event_TicketReopen: '%CC%[TicketManager]&7 %user%%CC% re-opened ticket &7#%id%%CC%.'
Notify_Event_SetPriority: '%CC%[TicketManager] &7%user%%CC% set ticket &7#%id%%CC% to priority %priority%'
Notify_Event_SetPriority: '%CC%[TicketManager] &7%user%%CC% set ticket &7#%id%%CC% to priority %priority%'
Notify_Event_PluginUpdate: '%CC%[TicketManager] TicketManager has an update!%nl% %CC%Current Version: &7%current%%nl% %CC%Latest Version: &7%latest%'
10 changes: 7 additions & 3 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@ permissions:
ticketmanager.notify.unreadUpdates.scheduled:
description: See own tickets with updates at scheduled intervals
default: false
ticketmanager.notify.warning:
description: See modified stacktrace on errors
default: false
ticketmanager.notify.info:
description: See general TicketManager information
default: false
ticketmanager.notify.pluginUpdate:
description: Get notified of plugin update on login
default: false
ticketmanager.notify.warning:
description: See modified stacktrace on errors
default: false
ticketmanager.notify.change.comment:
description: Creator sees when user comments on their ticket
default: false
Expand Down Expand Up @@ -189,6 +192,7 @@ permissions:
ticketmanager.notify.info: true
ticketmanager.massNotify.*: true
ticketmanager.notify.change.*: true
ticketmanager.notify.pluginUpdate: true
default: false
ticketmanager.*:
description: Wildcard for all TicketManager permissions
Expand Down

0 comments on commit eecb696

Please sign in to comment.