From 0aacfe3bcbedc35c79564d4e09734035c4af9f29 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:22:34 +0100 Subject: [PATCH] Add item handler methods to LegacyEnchantmentRewriter (#709) --- .../rewriters/LegacyEnchantmentRewriter.java | 25 +++++++++++++++++++ .../packets/BlockItemPackets1_11.java | 14 ++--------- .../packets/ItemPackets1_11_1.java | 20 ++------------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java index e07db10f..aa682324 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java @@ -17,6 +17,7 @@ */ package com.viaversion.viabackwards.api.rewriters; +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.libs.opennbt.tag.builtin.IntTag; @@ -45,6 +46,30 @@ public class LegacyEnchantmentRewriter { enchantmentMappings.put((short) id, replacementLore); } + public void handleToClient(Item item) { + CompoundTag tag = item.tag(); + if (tag == null) return; + + if (tag.getListTag("ench") != null) { + rewriteEnchantmentsToClient(tag, false); + } + if (tag.getListTag("StoredEnchantments") != null) { + rewriteEnchantmentsToClient(tag, true); + } + } + + public void handleToServer(Item item) { + CompoundTag tag = item.tag(); + if (tag == null) return; + + if (tag.contains(nbtTagName + "|ench")) { + rewriteEnchantmentsToServer(tag, false); + } + if (tag.contains(nbtTagName + "|StoredEnchantments")) { + rewriteEnchantmentsToServer(tag, true); + } + } + public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) { String key = storedEnchant ? "StoredEnchantments" : "ench"; ListTag enchantments = tag.getListTag(key, CompoundTag.class); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java index 6201e0b6..b37d1e60 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java @@ -298,12 +298,7 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter