Handle comps as well, remove invalids if in inventory

This commit is contained in:
libraryaddict 2024-07-02 02:30:38 +12:00
parent 87936a4a5a
commit 38a81f213c
2 changed files with 47 additions and 18 deletions

View File

@ -3,6 +3,8 @@ package me.libraryaddict.disguise.utilities;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.event.simple.PacketPlaySendEvent;
import com.github.retrooper.packetevents.protocol.component.ComponentTypes;
import com.github.retrooper.packetevents.protocol.component.builtin.item.ItemEnchantments;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.item.enchantment.type.EnchantmentType;
@ -2828,26 +2830,29 @@ public class DisguiseUtilities {
public static com.github.retrooper.packetevents.protocol.item.ItemStack stripEnchants(
com.github.retrooper.packetevents.protocol.item.ItemStack itemStack) {
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) {
// 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()) {
if (!NmsVersion.v1_13.isSupported() || itemStack == null) {
// Lets just skip 1.12, this is really a 1.20.6 issue anyways
return false;
return itemStack;
}
ItemEnchantments enchantsComp = itemStack.getComponentOr(ComponentTypes.ENCHANTMENTS, ItemEnchantments.EMPTY);
ItemEnchantments storedEnchantsComp = itemStack.getComponentOr(ComponentTypes.STORED_ENCHANTMENTS, ItemEnchantments.EMPTY);
if (!enchantsComp.isEmpty()) {
enchantsComp.setEnchantmentLevel(null, 0);
}
if (!storedEnchantsComp.isEmpty()) {
storedEnchantsComp.setEnchantmentLevel(null, 0);
}
@Nullable NBTCompound nbt = itemStack.getNBT();
if (nbt == null) {
return false;
return itemStack;
}
String tagName = NmsVersion.v1_12.isSupported() ? "Enchantments" : "ench";
@ -2858,21 +2863,27 @@ public class DisguiseUtilities {
@Nullable NBTList<NBTCompound> nbtList = nbt.getCompoundListTagOrNull(tagName);
if (nbtList == null) {
return false;
return itemStack;
}
List<NBTCompound> compounds = nbtList.getTags();
int index = 0;
for (NBTCompound compound : compounds) {
EnchantmentType type = EnchantmentTypes.getByName(compound.getStringTagValueOrNull("id"));
for (NBTCompound nbtCompound : new ArrayList<>(nbtList.getTags())) {
EnchantmentType type = EnchantmentTypes.getByName(nbtCompound.getStringTagValueOrNull("id"));
if (type != null) {
index++;
continue;
}
return true;
nbtList.removeTag(index);
}
return itemStack;
}
public static boolean hasCustomEnchants(com.github.retrooper.packetevents.protocol.item.ItemStack itemStack) {
return false;
}

View File

@ -12,7 +12,6 @@ import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientCl
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientCreativeInventoryAction;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSetSlot;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerWindowItems;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises;
@ -73,7 +72,10 @@ public class PacketListenerInventory extends SimplePacketListenerAbstract {
// If they are in creative and clicked on a slot
if (event.getPacketType() == Client.CREATIVE_INVENTORY_ACTION) {
int slot = new WrapperPlayClientCreativeInventoryAction(event).getSlot();
WrapperPlayClientCreativeInventoryAction wrapper = new WrapperPlayClientCreativeInventoryAction(event);
wrapper.setItemStack(DisguiseUtilities.stripEnchants(wrapper.getItemStack()));
int slot = wrapper.getSlot();
if (slot >= 5 && slot <= 8) {
if (disguise.isHidingArmorFromSelf()) {
@ -111,6 +113,12 @@ public class PacketListenerInventory extends SimplePacketListenerAbstract {
} else if (event.getPacketType() == Client.CLICK_WINDOW) {
WrapperPlayClientClickWindow packet = new WrapperPlayClientClickWindow(event);
packet.setCarriedItemStack(DisguiseUtilities.stripEnchants(packet.getCarriedItemStack()));
if (packet.getSlots().isPresent()) {
packet.getSlots().get().replaceAll((slot, item) -> DisguiseUtilities.stripEnchants(item));
}
int slot = packet.getSlot();
com.github.retrooper.packetevents.protocol.item.ItemStack clickedItem;
@ -213,6 +221,8 @@ public class PacketListenerInventory extends SimplePacketListenerAbstract {
if (event.getPacketType() == Server.SET_SLOT) {
WrapperPlayServerSetSlot packet = new WrapperPlayServerSetSlot(event);
packet.setItem(DisguiseUtilities.stripEnchants(packet.getItem()));
// If the inventory is the players inventory
if (packet.getWindowId() != 0) {
return;
@ -232,6 +242,7 @@ public class PacketListenerInventory extends SimplePacketListenerAbstract {
if (DisguiseUtilities.shouldBeHiddenSelfDisguise(item) && item.getType() != Material.ELYTRA) {
packet.setItem(com.github.retrooper.packetevents.protocol.item.ItemStack.EMPTY);
event.markForReEncode(true);
}
}
// Else if its a hotbar slot
@ -245,6 +256,7 @@ public class PacketListenerInventory extends SimplePacketListenerAbstract {
if (DisguiseUtilities.shouldBeHiddenSelfDisguise(item)) {
packet.setItem(com.github.retrooper.packetevents.protocol.item.ItemStack.EMPTY);
event.markForReEncode(true);
}
}
}
@ -252,6 +264,12 @@ public class PacketListenerInventory extends SimplePacketListenerAbstract {
} else if (event.getPacketType() == Server.WINDOW_ITEMS) {
WrapperPlayServerWindowItems packet = new WrapperPlayServerWindowItems(event);
packet.getItems().replaceAll(DisguiseUtilities::stripEnchants);
if (packet.getCarriedItem().isPresent()) {
packet.setCarriedItem(DisguiseUtilities.stripEnchants(packet.getCarriedItem().get()));
}
// If the inventory is the players inventory
if (packet.getWindowId() != 0) {
return;