From 1d77e1bb2e38e7d9b0ef9d87207adf80dedef393 Mon Sep 17 00:00:00 2001 From: quantiom Date: Mon, 8 Aug 2022 17:39:49 -0400 Subject: [PATCH 1/5] Fix vanish priority rejoin bug Fixes #11 --- .../me/quantiom/advancedvanish/listener/VanishListener.kt | 7 +------ .../me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt | 6 +++++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt b/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt index 08cffde..8b76966 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt @@ -1,6 +1,5 @@ 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 @@ -10,7 +9,6 @@ import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.block.Barrel import org.bukkit.block.Chest -import org.bukkit.block.EnderChest import org.bukkit.block.ShulkerBox import org.bukkit.entity.Player import org.bukkit.event.Cancellable @@ -24,7 +22,6 @@ import org.bukkit.event.entity.EntityTargetLivingEntityEvent import org.bukkit.event.entity.FoodLevelChangeEvent import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.event.player.* -import java.util.* object VanishListener : Listener { @EventHandler @@ -53,9 +50,7 @@ object VanishListener : Listener { player.sendConfigMessage("vanish-on") } - if (!player.hasPermission(vanishPermission)) { - AdvancedVanishAPI.refreshVanished(player) - } + AdvancedVanishAPI.refreshVanished(player) if (!Config.getValueOrDefault("when-vanished.join-messages", false)) { if (AdvancedVanishAPI.isPlayerVanished(player)) { diff --git a/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt b/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt index 213e494..02bae09 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt @@ -161,7 +161,11 @@ object AdvancedVanishAPI { fun refreshVanished(player: Player) { this.vanishedPlayers.forEach { uuid -> - Bukkit.getPlayer(uuid)?.let { player.hidePlayer(it) } + Bukkit.getPlayer(uuid)?.let { + if (!this.canSee(player, it)) { + player.hidePlayer(it) + } + } } } From 08799f238ff39400b570ad10555c7c7cfd157346 Mon Sep 17 00:00:00 2001 From: quantiom Date: Mon, 8 Aug 2022 18:06:58 -0400 Subject: [PATCH 2/5] Add update checker --- pom.xml | 2 +- .../quantiom/advancedvanish/AdvancedVanish.kt | 14 +++++++++ .../advancedvanish/util/Extensions.kt | 2 -- .../advancedvanish/util/UpdateChecker.kt | 31 +++++++++++++++++++ src/main/resources/config.yml | 3 ++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/me/quantiom/advancedvanish/util/UpdateChecker.kt diff --git a/pom.xml b/pom.xml index bafdfa4..2420fad 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ 1.6.0 official 4.13.1 - 6 + 7 diff --git a/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt b/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt index 319455e..5373c2b 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt @@ -8,10 +8,13 @@ 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 me.quantiom.advancedvanish.util.UpdateChecker import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin +import java.io.IOException import java.util.logging.Level + class AdvancedVanish : JavaPlugin() { companion object { var instance: AdvancedVanish? = null @@ -31,6 +34,17 @@ class AdvancedVanish : JavaPlugin() { } Config.reload() + + // update checker + if (Config.getValueOrDefault("check-for-updates", true)) { + UpdateChecker.getVersion { + if (it != this.description.version) { + this.logger.info("A new update for AdvancedVanish (v${it}) is available:") + this.logger.info("https://www.spigotmc.org/resources/advancedvanish.86036/") + } + } + } + this.server.pluginManager.registerEvents(VanishListener, this) PermissionsManager.setupPermissionsHandler() diff --git a/src/main/kotlin/me/quantiom/advancedvanish/util/Extensions.kt b/src/main/kotlin/me/quantiom/advancedvanish/util/Extensions.kt index 9e60678..80c01fc 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/util/Extensions.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/util/Extensions.kt @@ -1,13 +1,11 @@ package me.quantiom.advancedvanish.util -import joptsimple.internal.Messages.message import me.quantiom.advancedvanish.config.Config import org.bukkit.ChatColor import org.bukkit.command.CommandSender import org.bukkit.entity.Player import java.util.regex.Pattern - fun String.applyPlaceholders(vararg pairs: Pair): String { var intermediate = this diff --git a/src/main/kotlin/me/quantiom/advancedvanish/util/UpdateChecker.kt b/src/main/kotlin/me/quantiom/advancedvanish/util/UpdateChecker.kt new file mode 100644 index 0000000..f687b2e --- /dev/null +++ b/src/main/kotlin/me/quantiom/advancedvanish/util/UpdateChecker.kt @@ -0,0 +1,31 @@ +package me.quantiom.advancedvanish.util + +import me.quantiom.advancedvanish.AdvancedVanish +import org.bukkit.Bukkit +import java.io.IOException +import java.net.URL +import java.util.* +import java.util.function.Consumer + +// credit: https://www.spigotmc.org/wiki/creating-an-update-checker-that-checks-for-updates/ + +object UpdateChecker { + private const val RESOURCE_ID = 86036 + + fun getVersion(consumer: Consumer) { + Bukkit.getScheduler().runTaskAsynchronously(AdvancedVanish.instance!!, Runnable { + try { + URL("https://api.spigotmc.org/legacy/update.php?resource=${RESOURCE_ID}").openStream() + .use { inputStream -> + Scanner(inputStream).use { scanner -> + if (scanner.hasNext()) { + consumer.accept(scanner.next()) + } + } + } + } catch (exception: IOException) { + AdvancedVanish.instance!!.logger.info("Unable to check for updates: " + exception.message) + } + }) + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 48e8183..7fb5d9b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,6 +7,9 @@ ################################################################################### # Made by quantiom, config made for v%PROJECT_VERSION% +# Automatically check for updates +check-for-updates: true + # Enable vanish on join vanish-on-join: true # Saves the vanish state when a player logs off From fe62590f2012175ce8a81a7db092363a32d4db97 Mon Sep 17 00:00:00 2001 From: quantiom Date: Mon, 8 Aug 2022 23:14:52 -0400 Subject: [PATCH 3/5] Added togglable interact when vanished command Closes #13 --- .../quantiom/advancedvanish/AdvancedVanish.kt | 2 - .../advancedvanish/command/VanishCommand.kt | 19 ++++++++ .../advancedvanish/listener/VanishListener.kt | 44 ++++++++++++------- .../state/VanishStateManager.kt | 7 +++ .../advancedvanish/util/AdvancedVanishAPI.kt | 2 + src/main/resources/config.yml | 5 +++ 6 files changed, 62 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt b/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt index 5373c2b..a0e2999 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt @@ -11,10 +11,8 @@ import me.quantiom.advancedvanish.util.AdvancedVanishAPI import me.quantiom.advancedvanish.util.UpdateChecker import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin -import java.io.IOException import java.util.logging.Level - class AdvancedVanish : JavaPlugin() { companion object { var instance: AdvancedVanish? = null diff --git a/src/main/kotlin/me/quantiom/advancedvanish/command/VanishCommand.kt b/src/main/kotlin/me/quantiom/advancedvanish/command/VanishCommand.kt index 0a383b9..bdfcc4a 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/command/VanishCommand.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/command/VanishCommand.kt @@ -7,6 +7,7 @@ import me.quantiom.advancedvanish.AdvancedVanish import me.quantiom.advancedvanish.config.Config import me.quantiom.advancedvanish.hook.HooksManager import me.quantiom.advancedvanish.permission.PermissionsManager +import me.quantiom.advancedvanish.state.VanishStateManager import me.quantiom.advancedvanish.util.AdvancedVanishAPI import me.quantiom.advancedvanish.util.color import me.quantiom.advancedvanish.util.isVanished @@ -23,6 +24,7 @@ object VanishCommand : BaseCommand() { "&c/vanish &8- &fToggle vanish.", "&c/vanish version &8- &fShows the version of the plugin.", "&c/vanish reload &8- &fReloads the config and hooks.", + "&c/vanish interact &8- &fToggles interacting with blocks while in vanish.", "&c/vanish priority &8- &fDisplays your vanish priority.", "&c/vanish list &8- &fDisplays a list of vanished players.", "&c/vanish status &8- &fCheck if a player is in vanish.", @@ -64,6 +66,23 @@ object VanishCommand : BaseCommand() { PermissionsManager.setupPermissionsHandler() } + @Subcommand("interact") + private fun onInteractCommand(player: Player) { + if (!permissionCheck(player, "permissions.interact-command", "advancedvanish.interact-command")) return + + if (!player.isVanished()) { + player.sendConfigMessage("must-be-vanished-to-use-command") + } else { + if (VanishStateManager.interactEnabled.contains(player.uniqueId)) { + VanishStateManager.interactEnabled.remove(player.uniqueId) + player.sendConfigMessage("vanish-interact-toggled", "%interact-status%" to "off") + } else { + VanishStateManager.interactEnabled.add(player.uniqueId) + player.sendConfigMessage("vanish-interact-toggled", "%interact-status%" to "on") + } + } + } + @Subcommand("priority") private fun onPriorityCommand(player: Player) { if (!permissionCheck(player, "permissions.priority-command", "advancedvanish.priority-command")) return diff --git a/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt b/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt index 8b76966..b1e769f 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/listener/VanishListener.kt @@ -85,7 +85,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.send-messages", - "cannot-chat-while-vanished" + "cannot-chat-while-vanished", + false ) @EventHandler @@ -94,7 +95,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.place-blocks", - "cannot-place-blocks-while-vanished" + "cannot-place-blocks-while-vanished", + true ) @EventHandler @@ -103,7 +105,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.break-blocks", - "cannot-break-blocks-while-vanished" + "cannot-break-blocks-while-vanished", + true ) @EventHandler @@ -149,12 +152,15 @@ object VanishListener : Listener { event.isCancelled = true } } - } else genericEventCancel( - event, - event.player, - "when-vanished.interact", - "" - ) + } else if (!VanishStateManager.canInteract(event.player)) { + genericEventCancel( + event, + event.player, + "when-vanished.interact", + "", + true + ) + } } @EventHandler @@ -168,7 +174,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.pick-up-items", - "" + "", + true ) @EventHandler @@ -177,7 +184,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.drop-items", - "cannot-drop-items-while-vanished" + "cannot-drop-items-while-vanished", + true ) @EventHandler @@ -187,7 +195,8 @@ object VanishListener : Listener { event, event.entity as Player, "when-vanished.lose-hunger", - "" + "", + false ) } } @@ -199,7 +208,8 @@ object VanishListener : Listener { event, event.target as Player, "when-vanished.mob-targeting", - "" + "", + false ) } } @@ -207,7 +217,7 @@ object VanishListener : Listener { @EventHandler private fun onDamage(event: EntityDamageByEntityEvent) { (event.damager as? Player)?.let { damager -> - if (damager.isVanished() && !Config.getValueOrDefault("when-vanished.attack-entities", false)) { + if (damager.isVanished() && !Config.getValueOrDefault("when-vanished.attack-entities", false) && !VanishStateManager.canInteract(damager)) { damager.sendConfigMessage("cannot-attack-entities-while-vanished") event.isCancelled = true } @@ -220,8 +230,12 @@ object VanishListener : Listener { } } - private fun genericEventCancel(event: Cancellable, player: Player, toggle: String, message: String) { + private fun genericEventCancel(event: Cancellable, player: Player, toggle: String, message: String, ignoreIfCanInteract: Boolean) { if (player.isVanished() && !Config.getValueOrDefault(toggle, false)) { + if (ignoreIfCanInteract && VanishStateManager.canInteract(player)) { + return + } + if (message.isNotEmpty()) player.sendConfigMessage(message) event.isCancelled = true } diff --git a/src/main/kotlin/me/quantiom/advancedvanish/state/VanishStateManager.kt b/src/main/kotlin/me/quantiom/advancedvanish/state/VanishStateManager.kt index 003fca2..802a922 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/state/VanishStateManager.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/state/VanishStateManager.kt @@ -1,6 +1,7 @@ package me.quantiom.advancedvanish.state import com.google.common.collect.Maps +import com.google.common.collect.Sets import com.google.gson.Gson import com.google.gson.reflect.TypeToken import me.quantiom.advancedvanish.AdvancedVanish @@ -8,6 +9,7 @@ import me.quantiom.advancedvanish.config.Config import me.quantiom.advancedvanish.util.AdvancedVanishAPI import me.quantiom.advancedvanish.util.isVanished import org.bukkit.Bukkit +import org.bukkit.entity.Player import java.io.File import java.io.FileReader import java.io.FileWriter @@ -17,6 +19,7 @@ import java.util.* object VanishStateManager { val savedVanishStates: MutableMap = Maps.newHashMap() + val interactEnabled: MutableSet = Sets.newHashSet() fun onConfigReload() { if (!Config.getValueOrDefault("keep-vanish-state", false) @@ -48,6 +51,10 @@ object VanishStateManager { this.save() } + fun canInteract(player: Player): Boolean { + return this.interactEnabled.contains(player.uniqueId); + } + private fun getFile(): File? { try { return File( diff --git a/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt b/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt index 02bae09..edd51e1 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt @@ -10,6 +10,7 @@ import me.quantiom.advancedvanish.event.PrePlayerUnVanishEvent import me.quantiom.advancedvanish.event.PrePlayerVanishEvent import me.quantiom.advancedvanish.hook.HooksManager import me.quantiom.advancedvanish.permission.PermissionsManager +import me.quantiom.advancedvanish.state.VanishStateManager import org.bukkit.Bukkit import org.bukkit.entity.Player import org.bukkit.potion.PotionEffect @@ -117,6 +118,7 @@ object AdvancedVanishAPI { if (prePlayerUnVanishEvent.isCancelled) return this.vanishedPlayers.remove(player.uniqueId) + VanishStateManager.interactEnabled.remove(player.uniqueId) this.storedPotionEffects[player.uniqueId]?.let { for (potionEffect in it) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7fb5d9b..d932bc4 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -53,6 +53,7 @@ permissions: version-command: "advancedvanish.version-command" help-command: "advancedvanish.help-command" reload-config-command: "advancedvanish.reload-config-command" + interact-command: "advancedvanish.interact-command" priority-command: "advancedvanish.priority-command" list-command: "advancedvanish.list-command" status-command: "advancedvanish.status-command" @@ -180,6 +181,10 @@ messages: # %vanish-status% - "on" or "off" # %vanish-status-word% - "vanished" or "not vanished" vanish-set-other-command-already: "&c%target-name%&f is already &c%vanish-status-word%" + must-be-vanished-to-use-command: "&cYou must be vanished to use this command." + # Placeholders: + # %interact-status%" - "on" or "off" + vanish-interact-toggled: "&fYou have turned %interact-status% interacting while vanished." action-bar: "&cYou are in vanish." disguise-removed-because-vanish: "Your disguise has been &cremoved&f because you are in vanish" From fb66af18f1ae302af309e0f69643cdf3ce792b64 Mon Sep 17 00:00:00 2001 From: quantiom Date: Mon, 8 Aug 2022 23:55:22 -0400 Subject: [PATCH 4/5] Fix fly being disabled on unvanish when in spectator mode Fixes #12 --- .../me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt b/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt index edd51e1..a2f47c7 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt @@ -12,6 +12,7 @@ import me.quantiom.advancedvanish.hook.HooksManager import me.quantiom.advancedvanish.permission.PermissionsManager import me.quantiom.advancedvanish.state.VanishStateManager import org.bukkit.Bukkit +import org.bukkit.GameMode import org.bukkit.entity.Player import org.bukkit.potion.PotionEffect import org.bukkit.potion.PotionEffectType @@ -137,7 +138,8 @@ object AdvancedVanishAPI { it.showPlayer(player) } - if (!player.hasPermission(Config.getValueOrDefault("permissions.keep-fly-on-unvanish", "advancedvanish.keep-fly")) + // ignore if they are in spectator mode (allowed to fly by default) + if (player.gameMode != GameMode.SPECTATOR && !player.hasPermission(Config.getValueOrDefault("permissions.keep-fly-on-unvanish", "advancedvanish.keep-fly")) && !Config.getValueOrDefault("advancedvanish.fly.keep-on-unvanish", false)) { player.isFlying = false player.allowFlight = false From 57400edf7ccba1c2cb16438e2cd8ed50ed9ddbee Mon Sep 17 00:00:00 2001 From: quantiom Date: Mon, 8 Aug 2022 23:55:50 -0400 Subject: [PATCH 5/5] Update v1.1.7 -> v1.1.8 --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d0982f..f587381 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Add the dependency and replace `...` with the current version com.github.quantiom AdvancedVanish - v1.1.7 + v1.1.8 ``` diff --git a/pom.xml b/pom.xml index 2420fad..1b75209 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ me.quantiom advancedvanish - 1.1.7 + 1.1.8 jar AdvancedVanish