Skip to content

Commit

Permalink
fix: some bugs related to level and state features
Browse files Browse the repository at this point in the history
  • Loading branch information
Syrent committed Jun 1, 2024
1 parent 4da699f commit 1730ec8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 96 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ description=A modular vanish system for Minecraft servers
website=https://sayandev.org
author=Syrent

version=1.0.0-rc.5
version=1.0.0-rc.6
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import org.sayandev.stickynote.lib.gson.JsonParser
data class VanishOptions(
var sendMessage: Boolean = true,
var notifyStatusChangeToOthers: Boolean = true,
var notifyJoinQuitVanished: Boolean = true
var notifyJoinQuitVanished: Boolean = true,
var isOnJoin: Boolean = false
) {

class Builder {
private var sendMessage = true
private var notifyStatusChangeToOthers = true
private var notifyJoinQuitVanished = true
private var isOnJoin = false


fun sendMessage(sendMessage: Boolean): Builder {
Expand All @@ -31,8 +33,13 @@ data class VanishOptions(
return this
}

fun isOnJoin(isOnJoin: Boolean): Builder {
this.isOnJoin = isOnJoin
return this
}

fun build(): VanishOptions {
return VanishOptions(sendMessage, notifyStatusChangeToOthers, notifyJoinQuitVanished)
return VanishOptions(sendMessage, notifyStatusChangeToOthers, notifyJoinQuitVanished, isOnJoin)
}
}

Expand All @@ -41,6 +48,7 @@ data class VanishOptions(
json.addProperty("send-message", sendMessage)
json.addProperty("notify-status-change-to-others", notifyStatusChangeToOthers)
json.addProperty("notify-join-quit-vanished", notifyJoinQuitVanished)
json.addProperty("is-on-join", isOnJoin)
return Gson.gson.toJson(json)
}

Expand All @@ -51,7 +59,8 @@ data class VanishOptions(
return VanishOptions(
json.get("send-message").asBoolean,
json.get("notify-status-change-to-others").asBoolean,
json.get("notify-join-quit-vanished").asBoolean
json.get("notify-join-quit-vanished").asBoolean,
json.get("is-on-join").asBoolean
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import org.bukkit.GameMode
import org.bukkit.event.EventHandler
import org.bukkit.event.player.PlayerGameModeChangeEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.sayandev.sayanvanish.api.Permission
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI.Companion.user
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserUnVanishEvent
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserVanishEvent
import org.sayandev.sayanvanish.bukkit.feature.ListenedFeature
import org.sayandev.stickynote.bukkit.NMSUtils
import org.sayandev.stickynote.bukkit.PacketUtils
import org.sayandev.stickynote.bukkit.onlinePlayers
import org.sayandev.stickynote.bukkit.plugin
import org.sayandev.stickynote.bukkit.runSync
import org.sayandev.stickynote.bukkit.*
import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.ConfigSerializable

@RegisteredFeature
Expand All @@ -29,8 +26,8 @@ class FeatureLevel(
runSync({
val user = event.user
for (onlinePlayer in onlinePlayers.filter { it.uniqueId != user.uniqueId }) {
val playerVanishLevel = onlinePlayer.user()?.vanishLevel ?: 0
if (playerVanishLevel < user.vanishLevel) {
val playerVanishLevel = onlinePlayer.user(false)?.vanishLevel ?: -1
if (playerVanishLevel < user.vanishLevel || !onlinePlayer.hasPermission(Permission.VANISH.permission())) {
user.player()?.let { player ->
onlinePlayer.hidePlayer(plugin, player)
NMSUtils.sendPacket(onlinePlayer, PacketUtils.getRemoveEntitiesPacket(player.entityId))
Expand All @@ -50,7 +47,7 @@ class FeatureLevel(
runSync({
val user = event.user
for (onlinePlayer in onlinePlayers.filter { it.uniqueId != user.uniqueId }) {
val playerVanishLevel = onlinePlayer.user()?.vanishLevel ?: 0
val playerVanishLevel = onlinePlayer.user(false)?.vanishLevel ?: -1
if (playerVanishLevel >= user.vanishLevel) {
user.player()?.let { player -> NMSUtils.sendPacket(onlinePlayer, PacketUtils.getUpdateGameModePacket(NMSUtils.getServerPlayer(player), player.gameMode)) }
}
Expand All @@ -64,7 +61,7 @@ class FeatureLevel(
val player = event.player
val user = event.player.user() ?: return
for (onlinePlayer in onlinePlayers.filter { it.uniqueId != user.uniqueId }) {
val playerVanishLevel = onlinePlayer.user()?.vanishLevel ?: 0
val playerVanishLevel = onlinePlayer.user(false)?.vanishLevel ?: -1
if (playerVanishLevel >= user.vanishLevel) {
NMSUtils.sendPacket(onlinePlayer, PacketUtils.getUpdateGameModePacket(NMSUtils.getServerPlayer(player), player.gameMode))
} else {
Expand All @@ -75,13 +72,30 @@ class FeatureLevel(

@EventHandler
private fun onPlayerJoin(event: PlayerJoinEvent) {
if (!isActive() || seeAsSpectator) return
if (!isActive() || !seeAsSpectator) return
runSync({
val player = event.player
val playerVanishLevel = player.user()?.vanishLevel ?: 0
val playerVanishLevel = player.user(false)?.vanishLevel ?: -1
for (user in SayanVanishBukkitAPI.getInstance().getVanishedUsers().filter { it.player() != null && it.uniqueId != player.uniqueId }) {
if (playerVanishLevel >= user.vanishLevel) {
NMSUtils.sendPacket(player, PacketUtils.getUpdateGameModePacket(NMSUtils.getServerPlayer(player), GameMode.SPECTATOR))
val vanishedPlayer = user.player() ?: continue
if (playerVanishLevel >= user.vanishLevel && user.hasPermission(Permission.VANISH.permission())) {
NMSUtils.sendPacket(player, PacketUtils.getUpdateGameModePacket(NMSUtils.getServerPlayer(vanishedPlayer), GameMode.SPECTATOR))
}
}
}, 1)
}

@EventHandler
private fun hideOthersOnJoin(event: PlayerJoinEvent) {
if (!isActive()) return
runSync({
val player = event.player
val playerVanishLevel = player.user(false)?.vanishLevel ?: -1
for (user in SayanVanishBukkitAPI.getInstance().getVanishedUsers().filter { it.player() != null && it.uniqueId != player.uniqueId }) {
val vanishedPlayer = user.player() ?: continue
if (playerVanishLevel < user.vanishLevel || !player.hasPermission(Permission.VANISH.permission())) {
player.hidePlayer(plugin, vanishedPlayer)
NMSUtils.sendPacket(player, PacketUtils.getRemoveEntitiesPacket(vanishedPlayer.entityId))
}
}
}, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.sayandev.sayanvanish.api.Permission
import org.sayandev.sayanvanish.api.VanishOptions
import org.sayandev.stickynote.bukkit.utils.AdventureUtils.component
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI.Companion.getOrCreateUser
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI.Companion.user
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserUnVanishEvent
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserVanishEvent
import org.sayandev.sayanvanish.bukkit.config.language
import org.sayandev.sayanvanish.bukkit.feature.ListenedFeature
import org.sayandev.stickynote.bukkit.onlinePlayers
import org.sayandev.stickynote.bukkit.utils.AdventureUtils.component
import org.sayandev.stickynote.lib.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.ConfigSerializable

Expand All @@ -27,19 +24,14 @@ class FeatureState(
val reappearOnQuit: Boolean = false,
val checkPermissionOnQuit: Boolean = false,
val checkPermissionOnJoin: Boolean = false,
val getFromJoinEvent: Boolean = true,
val getFromQuitEvent: Boolean = true,
) : ListenedFeature("state") {

@Transient var generalJoinMessage: String? = null
@Transient var generalQuitMessage: String? = null

@EventHandler(priority = EventPriority.LOWEST)
private fun onJoin(event: PlayerJoinEvent) {
if (!isActive()) return
val player = event.player
val user = player.user(false)
val vanishJoinOptions = VanishOptions.Builder().sendMessage(false).notifyStatusChangeToOthers(false).build()
val vanishJoinOptions = VanishOptions.Builder().sendMessage(false).notifyStatusChangeToOthers(false).isOnJoin(true).build()

if (user == null) {
val tempUser = player.getOrCreateUser()
Expand Down Expand Up @@ -79,13 +71,6 @@ class FeatureState(
}

user.save()

if (player.user(false)?.isVanished == true) {
if (getFromJoinEvent) {
generalJoinMessage = event.joinMessage
}
event.joinMessage = REMOVAL_MESSAGE_ID
}
return
}

Expand All @@ -103,72 +88,11 @@ class FeatureState(
}

if ((reappearOnQuit && user.isVanished) || (checkPermissionOnQuit && !user.hasPermission(Permission.VANISH))) {
user.unVanish()
user.unVanish(VanishOptions.Builder().isOnJoin(true).build())
}
user.isOnline = false

user.save()

if (user.isVanished) {
if (getFromQuitEvent) {
generalQuitMessage = event.quitMessage
}
event.quitMessage = REMOVAL_MESSAGE_ID
}
}

@EventHandler(priority = EventPriority.MONITOR)
private fun setJoinMessage(event: PlayerJoinEvent) {
if (event.joinMessage != REMOVAL_MESSAGE_ID) {
if (getFromJoinEvent) {
generalJoinMessage = event.joinMessage
}
} else {
event.joinMessage = null
}
}

@EventHandler(priority = EventPriority.MONITOR)
private fun setQuitMessage(event: PlayerQuitEvent) {
if (event.quitMessage != REMOVAL_MESSAGE_ID) {
if (getFromQuitEvent) {
generalQuitMessage = event.quitMessage
}
} else {
event.quitMessage = null
}
}

@EventHandler
private fun sendQuitMessageOnVanish(event: BukkitUserVanishEvent) {
val options = event.options

if (options.sendMessage) {
val quitMessage = generalQuitMessage
if (quitMessage != null) {
for (onlinePlayer in onlinePlayers) {
onlinePlayer.sendMessage(quitMessage)
}
}
}
}

@EventHandler
private fun sendJoinMessageOnUnVanish(event: BukkitUserUnVanishEvent) {
val options = event.options

if (options.sendMessage) {
val joinMessage = generalJoinMessage
if (joinMessage != null) {
for (onlinePlayer in onlinePlayers) {
onlinePlayer.sendMessage(joinMessage)
}
}
}
}

companion object {
private const val REMOVAL_MESSAGE_ID = "SAYANVANISH_DISABLE_MESSAGE"
}

}

0 comments on commit 1730ec8

Please sign in to comment.