Don't hardcore NBT key

This commit is contained in:
FlorianMichael 2023-12-03 20:46:32 +01:00
parent b34576a80d
commit 815d987be6
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
3 changed files with 14 additions and 7 deletions

View File

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

View File

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

View File

@ -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().<ByteTag>remove("1_10_ViaFabricPlus_ItemCount").asByte());
if (item.tag().contains(ClientsideFixes.ITEM_COUNT_NBT_TAG)) {
item.setAmount(item.tag().<ByteTag>remove(ClientsideFixes.ITEM_COUNT_NBT_TAG).asByte());
if (item.tag().isEmpty()) item.setTag(null);
}
}