diff --git a/plugin/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/plugin/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index 3d4e1ee1..6de15127 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -981,7 +981,7 @@ public class FlagWatcher { for (Player player : DisguiseUtilities.getPerverts(getDisguise())) { List list = Collections.singletonList( - new Equipment(DisguiseUtilities.getSlot(slot), SpigotConversionUtil.fromBukkitItemStack(itemStack))); + new Equipment(DisguiseUtilities.getSlot(slot), DisguiseUtilities.fromBukkitItemStack(itemStack))); WrapperPlayServerEntityEquipment packet = new WrapperPlayServerEntityEquipment(getDisguise().getEntity().getEntityId(), list); PacketEvents.getAPI().getPlayerManager().sendPacket(player, packet); diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index fd0c9bac..57b806aa 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -8,6 +8,8 @@ import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.item.enchantment.type.EnchantmentType; import com.github.retrooper.packetevents.protocol.item.enchantment.type.EnchantmentTypes; import com.github.retrooper.packetevents.protocol.item.type.ItemTypes; +import com.github.retrooper.packetevents.protocol.nbt.NBTCompound; +import com.github.retrooper.packetevents.protocol.nbt.NBTList; import com.github.retrooper.packetevents.protocol.packettype.PacketType; import com.github.retrooper.packetevents.protocol.particle.Particle; import com.github.retrooper.packetevents.protocol.player.Equipment; @@ -135,6 +137,7 @@ import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team.Option; import org.bukkit.scoreboard.Team.OptionStatus; import org.bukkit.util.Vector; +import org.jetbrains.annotations.Nullable; import java.io.BufferedReader; import java.io.File; @@ -2673,7 +2676,7 @@ public class DisguiseUtilities { continue; } - list.add(new Equipment(slot, SpigotConversionUtil.fromBukkitItemStack(getSlot(player.getInventory(), getSlot(slot))))); + list.add(new Equipment(slot, DisguiseUtilities.fromBukkitItemStack(getSlot(player.getInventory(), getSlot(slot))))); } sendSelfPacket(player, new WrapperPlayServerEntityEquipment(player.getEntityId(), list)); @@ -2815,17 +2818,55 @@ public class DisguiseUtilities { return externalComponentSerializer.deserialize(serialized); } + public static ItemStack toBukkitItemStack(com.github.retrooper.packetevents.protocol.item.ItemStack itemStack) { + return SpigotConversionUtil.toBukkitItemStack(stripEnchants(itemStack)); + } + + public static com.github.retrooper.packetevents.protocol.item.ItemStack fromBukkitItemStack(ItemStack itemStack) { + return stripEnchants(SpigotConversionUtil.fromBukkitItemStack(itemStack)); + } + public static com.github.retrooper.packetevents.protocol.item.ItemStack stripEnchants( com.github.retrooper.packetevents.protocol.item.ItemStack itemStack) { - itemStack.setEnchantments(new ArrayList<>(), PacketEvents.getAPI().getServerManager().getVersion().toClientVersion()); + if (hasCustomEnchants(itemStack)) { + itemStack.setEnchantments(new ArrayList<>(), PacketEvents.getAPI().getServerManager().getVersion().toClientVersion()); + } return itemStack; } public static boolean hasCustomEnchants(com.github.retrooper.packetevents.protocol.item.ItemStack itemStack) { - for (com.github.retrooper.packetevents.protocol.item.enchantment.Enchantment enchant : itemStack.getEnchantments( - PacketEvents.getAPI().getServerManager().getVersion().toClientVersion())) { - if (enchant != null && enchant.getType() != null) { + // We have to copy/paste what PE does for reading enchants because it'll refuse to expose enchants that'll crash netty + // So we have to read it ourselves + // (This was decompiled instead of source code, no particular reason) + if (!NmsVersion.v1_13.isSupported()) { + // Lets just skip 1.12, this is really a 1.20.6 issue anyways + return false; + } + + @Nullable NBTCompound nbt = itemStack.getNBT(); + + if (nbt == null) { + return false; + } + + String tagName = NmsVersion.v1_12.isSupported() ? "Enchantments" : "ench"; + if (itemStack.getType() == ItemTypes.ENCHANTED_BOOK) { + tagName = "StoredEnchantments"; + } + + @Nullable NBTList nbtList = nbt.getCompoundListTagOrNull(tagName); + + if (nbtList == null) { + return false; + } + + List compounds = nbtList.getTags(); + + for (NBTCompound compound : compounds) { + EnchantmentType type = EnchantmentTypes.getByName(compound.getStringTagValueOrNull("id")); + + if (type != null) { continue; } diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java index 9d9ebd3e..2349ae5d 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java @@ -287,7 +287,7 @@ public class DisguiseListener implements Listener { int stateId = NmsVersion.v1_17.isSupported() ? ReflectionManager.getIncrementedStateId(player) : 0; WrapperPlayServerSetSlot packet = - new WrapperPlayServerSetSlot(0, stateId, event.getNewSlot() + 36, SpigotConversionUtil.fromBukkitItemStack(currentlyHeld)); + new WrapperPlayServerSetSlot(0, stateId, event.getNewSlot() + 36, DisguiseUtilities.fromBukkitItemStack(currentlyHeld)); PacketEvents.getAPI().getPlayerManager().sendPacketSilently(player, packet); } diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java index 09e6d7aa..f8ce6391 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java @@ -109,6 +109,6 @@ public class LibsPackets> { } return new WrapperPlayServerEntityEquipment(getDisguise().getEntity().getEntityId(), Collections.singletonList( - new Equipment(DisguiseUtilities.getSlot(slot), SpigotConversionUtil.fromBukkitItemStack(itemToSend)))); + new Equipment(DisguiseUtilities.getSlot(slot), DisguiseUtilities.fromBukkitItemStack(itemToSend)))); } } \ No newline at end of file diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java index 3cb9b52d..ca0deaa4 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java @@ -7,7 +7,6 @@ import com.github.retrooper.packetevents.protocol.player.Equipment; import com.github.retrooper.packetevents.protocol.player.EquipmentSlot; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityEquipment; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata; -import io.github.retrooper.packetevents.util.SpigotConversionUtil; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.MetaIndex; @@ -46,12 +45,10 @@ public class PacketHandlerEquipment implements IPacketHandler 0) { ItemStack item = ParamInfoItemStack.parseToItemstack(split); - data = new ParticleItemStackData(SpigotConversionUtil.fromBukkitItemStack(item)); + data = new ParticleItemStackData(DisguiseUtilities.fromBukkitItemStack(item)); } else { data = new ParticleItemStackData( com.github.retrooper.packetevents.protocol.item.ItemStack.builder().type(ItemTypes.STONE).build()); diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ItemStackSerializer.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ItemStackSerializer.java index 320a61c4..78d81608 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ItemStackSerializer.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ItemStackSerializer.java @@ -13,6 +13,7 @@ import io.github.retrooper.packetevents.util.SpigotConversionUtil; import it.unimi.dsi.fastutil.bytes.ByteList; import it.unimi.dsi.fastutil.ints.IntList; import it.unimi.dsi.fastutil.longs.LongList; +import me.libraryaddict.disguise.utilities.DisguiseUtilities; import org.apache.commons.lang.StringUtils; import org.bukkit.inventory.ItemStack; @@ -43,7 +44,7 @@ public class ItemStackSerializer { } } } else { - NBT nbt = SpigotConversionUtil.fromBukkitItemStack(item).getNBT(); + NBT nbt = DisguiseUtilities.fromBukkitItemStack(item).getNBT(); if (nbt != null) { String asString = serialize(nbt); @@ -67,7 +68,7 @@ public class ItemStackSerializer { } if (item.hasItemMeta()) { - NBT nbt = SpigotConversionUtil.fromBukkitItemStack(item).getNBT(); + NBT nbt = DisguiseUtilities.fromBukkitItemStack(item).getNBT(); if (nbt != null) { String asString = serialize(nbt); diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java index 891f5e4f..f2ddd0f2 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java @@ -1429,7 +1429,7 @@ public class ReflectionManager { } else if (index == MetaIndex.CAT_COLLAR || index == MetaIndex.WOLF_COLLAR) { return (T) AnimalColor.getColorByDye((int) value); } else if (index.isItemStack()) { - return (T) SpigotConversionUtil.toBukkitItemStack((com.github.retrooper.packetevents.protocol.item.ItemStack) value); + return (T) DisguiseUtilities.toBukkitItemStack((com.github.retrooper.packetevents.protocol.item.ItemStack) value); } else if (index.isBlock() || index.isBlockOpt()) { return (T) WrappedBlockState.getByGlobalId((int) value); /* BlockData data = getBlockDataByCombinedId((int) value); @@ -1526,7 +1526,7 @@ public class ReflectionManager { } else if (value instanceof TreeSpecies) { return (int) ((TreeSpecies) value).getData(); } else if (value instanceof ItemStack) { - return SpigotConversionUtil.fromBukkitItemStack((ItemStack) value); + return DisguiseUtilities.fromBukkitItemStack((ItemStack) value); } else if (value instanceof Rabbit.Type) { return RabbitType.getTypeId((Rabbit.Type) value); } else if (value instanceof Enum && !(value instanceof SnifferState || value instanceof EntityPose || value instanceof BlockFace || diff --git a/plugin/src/test/java/me/libraryaddict/disguise/utilities/parser/params/DisguiseParamParticleTest.java b/plugin/src/test/java/me/libraryaddict/disguise/utilities/parser/params/DisguiseParamParticleTest.java index 1faa86f1..cc829066 100644 --- a/plugin/src/test/java/me/libraryaddict/disguise/utilities/parser/params/DisguiseParamParticleTest.java +++ b/plugin/src/test/java/me/libraryaddict/disguise/utilities/parser/params/DisguiseParamParticleTest.java @@ -23,6 +23,7 @@ import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder import io.github.retrooper.packetevents.manager.server.ServerManagerImpl; import io.github.retrooper.packetevents.util.SpigotConversionUtil; import lombok.SneakyThrows; +import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.params.ParamInfoManager; import me.libraryaddict.disguise.utilities.params.types.custom.ParamInfoItemStack; import me.libraryaddict.disguise.utilities.params.types.custom.ParamInfoParticle; @@ -363,8 +364,8 @@ public class DisguiseParamParticleTest { try (MockedStatic mockedStatic = Mockito.mockStatic(SpigotConversionUtil.class); MockedStatic mockedParam = Mockito.mockStatic(ParamInfoItemStack.class)) { // Mock itemstack conversion - mockedStatic.when(() -> SpigotConversionUtil.fromBukkitItemStack(ArgumentMatchers.any())).thenReturn(pPearl); - mockedStatic.when(() -> SpigotConversionUtil.toBukkitItemStack(ArgumentMatchers.any())).thenReturn(bPearl); + mockedStatic.when(() -> DisguiseUtilities.fromBukkitItemStack(ArgumentMatchers.any())).thenReturn(pPearl); + mockedStatic.when(() -> DisguiseUtilities.toBukkitItemStack(ArgumentMatchers.any())).thenReturn(bPearl); ParamInfoParticle param = (ParamInfoParticle) ParamInfoManager.getParamInfo(Particle.class);