Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
quantiom authored Aug 9, 2022
2 parents c2db242 + 57400ed commit 88b323d
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Add the dependency and replace `<version>...</version>` with the current version
<dependency>
<groupId>com.github.quantiom</groupId>
<artifactId>AdvancedVanish</artifactId>
<version>v1.1.7</version>
<version>v1.1.8</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>me.quantiom</groupId>
<artifactId>advancedvanish</artifactId>
<version>1.1.7</version>
<version>1.1.8</version>
<packaging>jar</packaging>

<name>AdvancedVanish</name>
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/me/quantiom/advancedvanish/AdvancedVanish.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <player> &8- &fCheck if a player is in vanish.",
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -96,7 +91,8 @@ object VanishListener : Listener {
event,
event.player,
"when-vanished.send-messages",
"cannot-chat-while-vanished"
"cannot-chat-while-vanished",
false
)

@EventHandler
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -179,7 +180,8 @@ object VanishListener : Listener {
event,
event.player,
"when-vanished.pick-up-items",
""
"",
true
)

@EventHandler
Expand All @@ -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
Expand All @@ -198,7 +201,8 @@ object VanishListener : Listener {
event,
event.entity as Player,
"when-vanished.lose-hunger",
""
"",
false
)
}
}
Expand All @@ -210,15 +214,16 @@ object VanishListener : Listener {
event,
event.target as Player,
"when-vanished.mob-targeting",
""
"",
false
)
}
}

@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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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
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
Expand All @@ -17,6 +19,7 @@ import java.util.*

object VanishStateManager {
val savedVanishStates: MutableMap<UUID, Boolean> = Maps.newHashMap()
val interactEnabled: MutableSet<UUID> = Sets.newHashSet()

fun onConfigReload() {
if (!Config.getValueOrDefault("keep-vanish-state", false)
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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)
}
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/me/quantiom/advancedvanish/util/Extensions.kt
Original file line number Diff line number Diff line change
@@ -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, String>): String {
var intermediate = this

Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/me/quantiom/advancedvanish/util/UpdateChecker.kt
Original file line number Diff line number Diff line change
@@ -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<String?>) {
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)
}
})
}
}
8 changes: 8 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit 88b323d

Please sign in to comment.