From 815d987be66071cfaecf585fc7758056713996a8 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:46:32 +0100 Subject: [PATCH] Don't hardcore NBT key --- .../viafabricplus/fixes/ClientsideFixes.java | 5 +++++ .../fixes/minecraft/network/MixinPacketByteBuf.java | 9 +++++---- .../mixin/fixes/viaversion/MixinEntityIdRewriter.java | 7 ++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java b/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java index 03db0207..dacd42b1 100644 --- a/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java +++ b/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java @@ -55,6 +55,11 @@ public class ClientsideFixes { */ public static final String PACKET_SYNC_IDENTIFIER = UUID.randomUUID() + ":" + UUID.randomUUID(); + /** + * This identifier is an internal identifier used to store the item count in <= 1.10 to implement negative item counts + */ + public static final String ITEM_COUNT_NBT_TAG = "VFP_1_10_ItemCount_" + System.currentTimeMillis(); + /** * The current chat limit */ diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/network/MixinPacketByteBuf.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/network/MixinPacketByteBuf.java index b7b374d0..5ae69714 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/network/MixinPacketByteBuf.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/network/MixinPacketByteBuf.java @@ -19,6 +19,7 @@ package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.network; +import de.florianmichael.viafabricplus.fixes.ClientsideFixes; import de.florianmichael.viafabricplus.injection.access.IItemStack; import de.florianmichael.viafabricplus.protocolhack.ProtocolHack; import net.minecraft.item.ItemStack; @@ -35,10 +36,10 @@ public abstract class MixinPacketByteBuf { @Redirect(method = "readItemStack", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;setNbt(Lnet/minecraft/nbt/NbtCompound;)V")) private void removeViaFabricPlusTag(ItemStack instance, NbtCompound tag) { - if (tag != null && tag.contains("1_10_ViaFabricPlus_ItemCount", NbtElement.BYTE_TYPE) && ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_10)) { + if (tag != null && tag.contains(ClientsideFixes.ITEM_COUNT_NBT_TAG, NbtElement.BYTE_TYPE) && ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_10)) { final IItemStack mixinItemStack = ((IItemStack) (Object) instance); - mixinItemStack.viaFabricPlus$set1_10Count(tag.getByte("1_10_ViaFabricPlus_ItemCount")); - tag.remove("1_10_ViaFabricPlus_ItemCount"); + mixinItemStack.viaFabricPlus$set1_10Count(tag.getByte(ClientsideFixes.ITEM_COUNT_NBT_TAG)); + tag.remove(ClientsideFixes.ITEM_COUNT_NBT_TAG); if (tag.isEmpty()) tag = null; } @@ -52,7 +53,7 @@ public abstract class MixinPacketByteBuf { final IItemStack mixinItemStack = ((IItemStack) (Object) instance); if (mixinItemStack.viaFabricPlus$has1_10ViaFabricPlusTag()) { if (tag == null) tag = new NbtCompound(); - tag.putByte("1_10_ViaFabricPlus_ItemCount", (byte) mixinItemStack.viaFabricPlus$get1_10Count()); + tag.putByte(ClientsideFixes.ITEM_COUNT_NBT_TAG, (byte) mixinItemStack.viaFabricPlus$get1_10Count()); } return tag; diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/MixinEntityIdRewriter.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/MixinEntityIdRewriter.java index ea6b422b..e8f2c666 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/MixinEntityIdRewriter.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/MixinEntityIdRewriter.java @@ -23,6 +23,7 @@ import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.protocols.protocol1_11to1_10.EntityIdRewriter; +import de.florianmichael.viafabricplus.fixes.ClientsideFixes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -40,7 +41,7 @@ public abstract class MixinEntityIdRewriter { item.setTag(tag); } - tag.put("1_10_ViaFabricPlus_ItemCount", new ByteTag((byte) item.amount())); + tag.put(ClientsideFixes.ITEM_COUNT_NBT_TAG, new ByteTag((byte) item.amount())); item.setTag(tag); } } @@ -48,8 +49,8 @@ public abstract class MixinEntityIdRewriter { @Inject(method = "toServerItem(Lcom/viaversion/viaversion/api/minecraft/item/Item;Z)V", at = @At("HEAD")) private static void handleNegativeItemCountC2S(Item item, boolean backwards, CallbackInfo ci) { if (item != null && item.tag() != null) { - if (item.tag().contains("1_10_ViaFabricPlus_ItemCount")) { - item.setAmount(item.tag().remove("1_10_ViaFabricPlus_ItemCount").asByte()); + if (item.tag().contains(ClientsideFixes.ITEM_COUNT_NBT_TAG)) { + item.setAmount(item.tag().remove(ClientsideFixes.ITEM_COUNT_NBT_TAG).asByte()); if (item.tag().isEmpty()) item.setTag(null); } }