Skip to content

Commit

Permalink
fix: version-check & disable packet due to InventoryView class -> int…
Browse files Browse the repository at this point in the history
…erface
  • Loading branch information
Boy0000 committed Jun 17, 2024
1 parent 9249a15 commit 8ed7022
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.mineinabyss.emojy.nms

import com.mineinabyss.emojy.EmojyPlugin
import org.bukkit.Bukkit

object EmojyNMSHandlers {

private val SUPPORTED_VERSION = arrayOf("v1_20_R4", "v1_21_R1")

fun setup(emojy: EmojyPlugin): IEmojyNMSHandler {
SUPPORTED_VERSION.forEach { version ->
runCatching {
return Class.forName("com.mineinabyss.emojy.nms.${version}.EmojyNMSHandler").getConstructor(EmojyPlugin::class.java)
.newInstance(emojy) as IEmojyNMSHandler
}.onFailure { it.printStackTrace() }
val nmsPackage = when (Bukkit.getMinecraftVersion()) {
"1.20.5", "1.20.6" -> "v1_20_R4"
"1.21" -> "v1_21_R1"
else -> throw IllegalStateException("Unsupported server version")
}
runCatching {
return Class.forName("com.mineinabyss.emojy.nms.${nmsPackage}.EmojyNMSHandler").getConstructor(EmojyPlugin::class.java)
.newInstance(emojy) as IEmojyNMSHandler
}.onFailure { it.printStackTrace() }

throw IllegalStateException("Unsupported server version")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ import io.netty.channel.ChannelPromise
import io.papermc.paper.adventure.AdventureComponent
import io.papermc.paper.adventure.PaperAdventure
import io.papermc.paper.network.ChannelInitializeListenerHolder
import net.minecraft.core.NonNullList
import net.minecraft.network.Connection
import net.minecraft.network.chat.ChatType
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.MessageSignature
import net.minecraft.network.chat.SignedMessageBody
import net.minecraft.network.protocol.common.ClientboundDisconnectPacket
import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket
import net.minecraft.network.protocol.common.ClientboundServerLinksPacket
Expand All @@ -30,10 +27,8 @@ import net.minecraft.network.syncher.EntityDataSerializer
import net.minecraft.network.syncher.SynchedEntityData
import net.minecraft.world.item.ItemStack
import org.bukkit.NamespacedKey
import org.bukkit.ServerLinks
import org.bukkit.craftbukkit.inventory.CraftItemStack
import org.bukkit.entity.Player
import org.bukkit.inventory.AnvilInventory
import java.util.*

class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {
Expand Down Expand Up @@ -72,21 +67,21 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {
} ?: it
})
is ClientboundContainerSetSlotPacket -> ClientboundContainerSetSlotPacket(packet.containerId, packet.stateId, packet.slot, packet.item.transformItemNameLore())
is ClientboundContainerSetContentPacket -> ClientboundContainerSetContentPacket(
packet.containerId, packet.stateId, NonNullList.of(packet.items.first(),
*packet.items.map {
val inv = connection.player.bukkitEntity.openInventory.topInventory
val bukkit = CraftItemStack.asBukkitCopy(it)

// If item is firstItem in AnvilInventory we want to set it to have the plain-text displayname
if (inv is AnvilInventory && inv.firstItem == bukkit)
bukkit.itemMeta?.persistentDataContainer?.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING)?.let { og ->
CraftItemStack.asNMSCopy(bukkit.editItemMeta {
setDisplayName(og)
})
} ?: it.transformItemNameLore()
else it.transformItemNameLore()
}.toTypedArray()), packet.carriedItem)
//is ClientboundContainerSetContentPacket -> ClientboundContainerSetContentPacket(
// packet.containerId, packet.stateId, NonNullList.of(packet.items.first(),
// *packet.items.map {
// val inv = connection.player.bukkitEntity.openInventory.topInventory
// val bukkit = CraftItemStack.asBukkitCopy(it)

// // If item is firstItem in AnvilInventory we want to set it to have the plain-text displayname
// if (inv is AnvilInventory && inv.firstItem == bukkit)
// bukkit.itemMeta?.persistentDataContainer?.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING)?.let { og ->
// CraftItemStack.asNMSCopy(bukkit.editItemMeta {
// setDisplayName(og)
// })
// } ?: it.transformItemNameLore()
// else it.transformItemNameLore()
// }.toTypedArray()), packet.carriedItem)
is ClientboundBossEventPacket -> {
// Access the private field 'operation'
val operationField = ClientboundBossEventPacket::class.java.getDeclaredField("operation").apply { isAccessible = true }
Expand Down

0 comments on commit 8ed7022

Please sign in to comment.