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 40c7441..53f57b7 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ me.quantiom advancedvanish - 1.1.7 + 1.1.8 jar AdvancedVanish diff --git a/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt b/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt index 319455e..a0e2999 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt @@ -8,6 +8,7 @@ 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.util.logging.Level @@ -31,6 +32,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/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 9a97770..64a15c7 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 @@ -59,9 +56,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)) { @@ -96,7 +91,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.send-messages", - "cannot-chat-while-vanished" + "cannot-chat-while-vanished", + false ) @EventHandler @@ -105,7 +101,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.place-blocks", - "cannot-place-blocks-while-vanished" + "cannot-place-blocks-while-vanished", + true ) @EventHandler @@ -114,7 +111,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.break-blocks", - "cannot-break-blocks-while-vanished" + "cannot-break-blocks-while-vanished", + true ) @EventHandler @@ -160,12 +158,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 @@ -179,7 +180,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.pick-up-items", - "" + "", + true ) @EventHandler @@ -188,7 +190,8 @@ object VanishListener : Listener { event, event.player, "when-vanished.drop-items", - "cannot-drop-items-while-vanished" + "cannot-drop-items-while-vanished", + true ) @EventHandler @@ -198,7 +201,8 @@ object VanishListener : Listener { event, event.entity as Player, "when-vanished.lose-hunger", - "" + "", + false ) } } @@ -210,7 +214,8 @@ object VanishListener : Listener { event, event.target as Player, "when-vanished.mob-targeting", - "" + "", + false ) } } @@ -218,7 +223,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 } @@ -231,8 +236,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 213e494..a2f47c7 100644 --- a/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt +++ b/src/main/kotlin/me/quantiom/advancedvanish/util/AdvancedVanishAPI.kt @@ -10,7 +10,9 @@ 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.GameMode import org.bukkit.entity.Player import org.bukkit.potion.PotionEffect import org.bukkit.potion.PotionEffectType @@ -117,6 +119,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) { @@ -135,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 @@ -161,7 +165,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) + } + } } } 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 543ac0b..f8fe7b2 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 @@ -50,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" @@ -178,6 +182,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"