Skip to content

Commit

Permalink
Add keep-vanish-state-persistent option
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
quantiom committed Aug 7, 2022
1 parent ca8744d commit b515607
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<kotlin.version>1.6.0</kotlin.version>
<kotlin.code.style>official</kotlin.code.style>
<junit.version>4.13.1</junit.version>
<config.version>5</config.version>
<config.version>6</config.version>
</properties>

<repositories>
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/me/quantiom/advancedvanish/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,7 +86,8 @@ object Config {
}

this.reloadMessages()
this.reloadCommandHandlerMessages();
this.reloadCommandHandlerMessages()
VanishStateManager.onConfigReload()
this.usingPriorities = this.getValueOrDefault("priority.enable", false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,8 +27,6 @@ import org.bukkit.event.player.*
import java.util.*

object VanishListener : Listener {
private val savedVanishStates: MutableMap<UUID, Boolean> = Maps.newHashMap()

@EventHandler
private fun onJoin(event: PlayerJoinEvent) {
val player = event.player
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UUID, Boolean> = 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<MutableMap<UUID, Boolean>>() {}.type
val fileReader = FileReader(it)
val inventories: MutableMap<UUID, Boolean> = Gson().fromJson(fileReader, type)
this.savedVanishStates.putAll(inventories)
fileReader.close()
}
} catch (ignored: NullPointerException) {

}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b515607

Please sign in to comment.