Enhanced custom enchants stripping

This commit is contained in:
libraryaddict 2024-07-02 01:46:12 +12:00
parent 98105683e5
commit 87936a4a5a
11 changed files with 64 additions and 25 deletions

View File

@ -981,7 +981,7 @@ public class FlagWatcher {
for (Player player : DisguiseUtilities.getPerverts(getDisguise())) {
List<Equipment> 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);

View File

@ -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<NBTCompound> nbtList = nbt.getCompoundListTagOrNull(tagName);
if (nbtList == null) {
return false;
}
List<NBTCompound> compounds = nbtList.getTags();
for (NBTCompound compound : compounds) {
EnchantmentType type = EnchantmentTypes.getByName(compound.getStringTagValueOrNull("id"));
if (type != null) {
continue;
}

View File

@ -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);
}

View File

@ -109,6 +109,6 @@ public class LibsPackets<T extends PacketWrapper<T>> {
}
return new WrapperPlayServerEntityEquipment(getDisguise().getEntity().getEntityId(), Collections.singletonList(
new Equipment(DisguiseUtilities.getSlot(slot), SpigotConversionUtil.fromBukkitItemStack(itemToSend))));
new Equipment(DisguiseUtilities.getSlot(slot), DisguiseUtilities.fromBukkitItemStack(itemToSend))));
}
}

View File

@ -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<WrapperPlayServerE
for (Equipment equipment : originalPacket.getEquipment()) {
EquipmentSlot slot = equipment.getSlot();
ItemStack itemInDisguise = disguise.getWatcher().getItemStack(DisguiseUtilities.getSlot(slot));
com.github.retrooper.packetevents.protocol.item.ItemStack itemInPacket = equipment.getItem();
com.github.retrooper.packetevents.protocol.item.ItemStack itemInPacket = DisguiseUtilities.stripEnchants(equipment.getItem());
// Workaround for this pending fix https://github.com/retrooper/packetevents/issues/869
if (DisguiseUtilities.hasCustomEnchants(itemInPacket)) {
equipment.setItem(itemInPacket = DisguiseUtilities.stripEnchants(itemInPacket));
}
equipment.setItem(itemInPacket);
if (itemInDisguise != null) {
// If we haven't decided to send a new packet yet, then construct it
@ -62,7 +59,7 @@ public class PacketHandlerEquipment implements IPacketHandler<WrapperPlayServerE
}
itemInPacket = itemInDisguise.getType() == Material.AIR ? com.github.retrooper.packetevents.protocol.item.ItemStack.EMPTY :
SpigotConversionUtil.fromBukkitItemStack(itemInDisguise);
DisguiseUtilities.fromBukkitItemStack(itemInDisguise);
equipmentBeingSent.add(new Equipment(slot, itemInPacket));
} else {
equipmentBeingSent.add(equipment);

View File

@ -378,10 +378,8 @@ public class PacketHandlerSpawn implements IPacketHandler {
continue;
}
// Workaround for this pending fix https://github.com/retrooper/packetevents/issues/869
WrapperPlayServerEntityEquipment packet = new WrapperPlayServerEntityEquipment(disguisedEntity.getEntityId(),
Collections.singletonList(
new Equipment(slot, DisguiseUtilities.stripEnchants(SpigotConversionUtil.fromBukkitItemStack(itemToSend)))));
Collections.singletonList(new Equipment(slot, DisguiseUtilities.fromBukkitItemStack(itemToSend))));
packets.addDelayedPacket(packet);
}

View File

@ -139,7 +139,7 @@ public class PacketListenerInventory extends SimplePacketListenerAbstract {
return;
}
clickedItem = SpigotConversionUtil.fromBukkitItemStack(player.getItemOnCursor());
clickedItem = DisguiseUtilities.fromBukkitItemStack(player.getItemOnCursor());
}
if (DisguiseUtilities.shouldBeHiddenSelfDisguise(clickedItem) && clickedItem.getType() != ItemTypes.ELYTRA) {

View File

@ -21,6 +21,7 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.params.ParamInfoManager;
import me.libraryaddict.disguise.utilities.params.types.ParamInfoEnum;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
@ -347,7 +348,7 @@ public class ParamInfoParticle extends ParamInfoEnum {
if (split.length > 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());

View File

@ -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);

View File

@ -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 ||

View File

@ -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<SpigotConversionUtil> mockedStatic = Mockito.mockStatic(SpigotConversionUtil.class);
MockedStatic<ParamInfoItemStack> 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);