Skip to content

Commit

Permalink
Fix #67 - Use an enum for the display objective packet on 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
r4g3baby committed Oct 12, 2023
1 parent 328168c commit 8d2ed3e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import com.comphenix.protocol.wrappers.WrappedChatComponent.fromLegacyText
import com.comphenix.protocol.wrappers.WrappedChatComponent.fromText
import com.r4g3baby.simplescore.scoreboard.models.PlayerBoard
import org.bukkit.entity.Player
import org.bukkit.scoreboard.DisplaySlot
import java.util.*
import java.util.concurrent.ConcurrentHashMap


class ProtocolScoreboard : ScoreboardHandler() {
private val protocolManager = ProtocolLibrary.getProtocolManager()

// Don't use ProtocolLib version enums, so we can support older plugin versions
private val afterAquaticUpdate = MinecraftVersion("1.13").atOrAbove()
private val afterCavesAndCliffsUpdate = MinecraftVersion("1.17").atOrAbove()
private val afterTrailsAndTailsDot2Update = MinecraftVersion("1.20.2").atOrAbove()

private val playerBoards = ConcurrentHashMap(HashMap<UUID, PlayerBoard>())

Expand All @@ -35,7 +38,9 @@ class ProtocolScoreboard : ScoreboardHandler() {

packet = PacketContainer(PacketType.Play.Server.SCOREBOARD_DISPLAY_OBJECTIVE)
packet.modifier.writeDefaults()
packet.integers.write(0, 1) // Position 1: Sidebar
if (afterTrailsAndTailsDot2Update) {
packet.getEnumModifier(DisplaySlot::class.java, 0).write(0, DisplaySlot.SIDEBAR)
} else packet.integers.write(0, 1) // Position 1: Sidebar
packet.strings.write(0, getPlayerIdentifier(player)) // Objective Name
protocolManager.sendServerPacket(player, packet)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.comphenix.protocol.ProtocolLibrary
import com.comphenix.protocol.events.ListeningWhitelist
import com.comphenix.protocol.events.PacketEvent
import com.comphenix.protocol.events.PacketListener
import com.comphenix.protocol.utility.MinecraftVersion
import com.r4g3baby.simplescore.SimpleScore
import com.r4g3baby.simplescore.scoreboard.handlers.ScoreboardHandler.Companion.getPlayerIdentifier
import com.r4g3baby.simplescore.scoreboard.models.CompatibilityMode
Expand All @@ -13,13 +14,16 @@ import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.plugin.Plugin
import org.bukkit.scoreboard.DisplaySlot

class PacketListener : PacketListener, Listener {
private val protocolLibPlugin = ProtocolLibrary.getPlugin()
private val sendingPackets = ListeningWhitelist.newBuilder().highest().types(
PacketType.Play.Server.SCOREBOARD_OBJECTIVE, PacketType.Play.Server.SCOREBOARD_DISPLAY_OBJECTIVE
).build()

private val afterTrailsAndTailsDot2Update = MinecraftVersion("1.20.2").atOrAbove()

override fun getSendingWhitelist(): ListeningWhitelist {
return sendingPackets
}
Expand All @@ -30,7 +34,9 @@ class PacketListener : PacketListener, Listener {
when (SimpleScore.config.compatibilityMode) {
CompatibilityMode.DISABLE -> {
if (e.packet.type == PacketType.Play.Server.SCOREBOARD_DISPLAY_OBJECTIVE) {
val position = e.packet.integers.read(0)
val position = if (afterTrailsAndTailsDot2Update) {
e.packet.getEnumModifier(DisplaySlot::class.java, 0).read(0).ordinal - 1
} else e.packet.integers.read(0)
val name = e.packet.strings.read(0)
if (position == 1 && name != getPlayerIdentifier(e.player)) {
SimpleScore.manager.playersData.setDisabled(protocolLibPlugin, e.player, true)
Expand All @@ -47,7 +53,9 @@ class PacketListener : PacketListener, Listener {
}
CompatibilityMode.BLOCK -> {
if (e.packet.type == PacketType.Play.Server.SCOREBOARD_DISPLAY_OBJECTIVE) {
val position = e.packet.integers.read(0)
val position = if (afterTrailsAndTailsDot2Update) {
e.packet.getEnumModifier(DisplaySlot::class.java, 0).read(0).ordinal - 1
} else e.packet.integers.read(0)
val name = e.packet.strings.read(0)
if (position == 1 && name != getPlayerIdentifier(e.player)) {
e.isCancelled = SimpleScore.manager.needsScoreboard(e.player)
Expand Down

0 comments on commit 8d2ed3e

Please sign in to comment.