From b515607775cd7ea0001a63ccfbfd6717204887d4 Mon Sep 17 00:00:00 2001 From: quantiom Date: Sun, 7 Aug 2022 16:58:39 -0400 Subject: [PATCH] Add keep-vanish-state-persistent option Closes #7 --- pom.xml | 2 +- .../quantiom/advancedvanish/AdvancedVanish.kt | 2 + .../quantiom/advancedvanish/config/Config.kt | 4 +- .../advancedvanish/listener/VanishListener.kt | 11 +-- .../state/VanishStateManager.kt | 94 +++++++++++++++++++ src/main/resources/config.yml | 4 + 6 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/me/quantiom/advancedvanish/state/VanishStateManager.kt diff --git a/pom.xml b/pom.xml index 523becf..71260d4 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ 1.6.0 official 4.13.1 - 5 + 6 diff --git a/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt b/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt index a55006d..319455e 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt @@ -6,6 +6,7 @@ import me.quantiom.advancedvanish.config.Config import me.quantiom.advancedvanish.hook.HooksManager import me.quantiom.advancedvanish.listener.VanishListener import me.quantiom.advancedvanish.permission.PermissionsManager +import me.quantiom.advancedvanish.state.VanishStateManager import me.quantiom.advancedvanish.util.AdvancedVanishAPI import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin @@ -37,6 +38,7 @@ class AdvancedVanish : JavaPlugin() { } override fun onDisable() { + VanishStateManager.onDisable() AdvancedVanishAPI.vanishedPlayers.map(Bukkit::getPlayer).forEach { AdvancedVanishAPI.unVanishPlayer(it!!) } HooksManager.disableHooks() commandManager?.unregisterCommand(VanishCommand) diff --git a/src/main/kotlin/me/quantiom/advancedvanish/config/Config.kt b/src/main/kotlin/me/quantiom/advancedvanish/config/Config.kt index 75f06f1..2fb27a4 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/config/Config.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/config/Config.kt @@ -6,6 +6,7 @@ import co.aikar.locales.MessageKeyProvider import com.google.common.collect.Maps import com.google.common.io.Closeables import me.quantiom.advancedvanish.AdvancedVanish +import me.quantiom.advancedvanish.state.VanishStateManager import me.quantiom.advancedvanish.util.applyPlaceholders import me.quantiom.advancedvanish.util.color import org.bukkit.command.CommandSender @@ -85,7 +86,8 @@ object Config { } this.reloadMessages() - this.reloadCommandHandlerMessages(); + this.reloadCommandHandlerMessages() + VanishStateManager.onConfigReload() this.usingPriorities = this.getValueOrDefault("priority.enable", false) } diff --git a/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt b/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt index a861ac6..08cffde 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt @@ -2,6 +2,7 @@ package me.quantiom.advancedvanish.listener import com.google.common.collect.Maps import me.quantiom.advancedvanish.config.Config +import me.quantiom.advancedvanish.state.VanishStateManager import me.quantiom.advancedvanish.util.AdvancedVanishAPI import me.quantiom.advancedvanish.util.isVanished import me.quantiom.advancedvanish.util.sendConfigMessage @@ -26,8 +27,6 @@ import org.bukkit.event.player.* import java.util.* object VanishListener : Listener { - private val savedVanishStates: MutableMap = Maps.newHashMap() - @EventHandler private fun onJoin(event: PlayerJoinEvent) { val player = event.player @@ -39,10 +38,10 @@ object VanishListener : Listener { var doVanish = false if (player.hasPermission(vanishPermission)) { - if (Config.getValueOrDefault("keep-vanish-state", false) && this.savedVanishStates.containsKey(player.uniqueId)) { - if (this.savedVanishStates[player.uniqueId]!!) { + if (Config.getValueOrDefault("keep-vanish-state", false) && VanishStateManager.savedVanishStates.containsKey(player.uniqueId)) { + if (VanishStateManager.savedVanishStates[player.uniqueId]!!) { doVanish = true - this.savedVanishStates.remove(player.uniqueId) + VanishStateManager.savedVanishStates.remove(player.uniqueId) } } else if (Config.getValueOrDefault("vanish-on-join", false)) { doVanish = true @@ -71,7 +70,7 @@ object VanishListener : Listener { val isVanished = player.isVanished() if (isVanished || player.hasPermission(Config.getValueOrDefault("permissions.vanish", "advancedvanish.vanish"))) { - this.savedVanishStates[player.uniqueId] = isVanished + VanishStateManager.savedVanishStates[player.uniqueId] = isVanished if (isVanished) { AdvancedVanishAPI.unVanishPlayer(player, true) diff --git a/src/main/kotlin/me/quantiom/advancedvanish/state/VanishStateManager.kt b/src/main/kotlin/me/quantiom/advancedvanish/state/VanishStateManager.kt new file mode 100644 index 0000000..003fca2 --- /dev/null +++ b/src/main/kotlin/me/quantiom/advancedvanish/state/VanishStateManager.kt @@ -0,0 +1,94 @@ +package me.quantiom.advancedvanish.state + +import com.google.common.collect.Maps +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken +import me.quantiom.advancedvanish.AdvancedVanish +import me.quantiom.advancedvanish.config.Config +import me.quantiom.advancedvanish.util.AdvancedVanishAPI +import me.quantiom.advancedvanish.util.isVanished +import org.bukkit.Bukkit +import java.io.File +import java.io.FileReader +import java.io.FileWriter +import java.lang.NullPointerException +import java.lang.reflect.Type +import java.util.* + +object VanishStateManager { + val savedVanishStates: MutableMap = Maps.newHashMap() + + fun onConfigReload() { + if (!Config.getValueOrDefault("keep-vanish-state", false) + || !Config.getValueOrDefault("keep-vanish-state-persistent", false)) { + return + } + + File(AdvancedVanish.instance!!.dataFolder.path).also { + if (!it.exists()) it.mkdirs() + } + + this.load() + } + + fun onDisable() { + if (!Config.getValueOrDefault("keep-vanish-state", false) + || !Config.getValueOrDefault("keep-vanish-state-persistent", false)) { + return + } + + Bukkit.getOnlinePlayers().forEach { + val isVanished = it.isVanished() + + if (isVanished || it.hasPermission(Config.getValueOrDefault("permissions.vanish", "advancedvanish.vanish"))) { + this.savedVanishStates[it.uniqueId] = isVanished + } + } + + this.save() + } + + private fun getFile(): File? { + try { + return File( + "${AdvancedVanish.instance!!.dataFolder}${File.separator}vanishStates.json" + ).also { + if (!it.exists()) { + it.createNewFile() + } + } + } catch (e: Exception) { + e.printStackTrace() + } + + return null + } + + private fun save() { + try { + this.getFile()?.let { + val jsonElement: String = Gson().toJson(this.savedVanishStates) + FileWriter(it, false).close() + val fileWriter = FileWriter(it) + fileWriter.write(jsonElement) + fileWriter.close() + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + private fun load() { + try { + this.getFile()?.let { + val type: Type = object : TypeToken>() {}.type + val fileReader = FileReader(it) + val inventories: MutableMap = Gson().fromJson(fileReader, type) + this.savedVanishStates.putAll(inventories) + fileReader.close() + } + } catch (ignored: NullPointerException) { + + } + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2ee8251..48e8183 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -13,6 +13,10 @@ vanish-on-join: true # Example: Player joins with `vanish-on-join` enabled, turns # off vanish, and rejoins without vanish enabled keep-vanish-state: false +# The option above must be set to true +# Stores vanish states to a file, so they +# persist outside of server restarts +keep-vanish-state-persistent: false # Actions when players are vanished # true = enabled, false = disabled