Add item handler methods to LegacyEnchantmentRewriter (#709)

This commit is contained in:
EnZaXD 2024-03-28 11:22:34 +01:00 committed by GitHub
parent 4547d5c702
commit 0aacfe3bcb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 30 deletions

View File

@ -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<CompoundTag> enchantments = tag.getListTag(key, CompoundTag.class);

View File

@ -298,12 +298,7 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter<ClientboundPac
// Rewrite spawn eggs (id checks are done in the method itself)
EntityIdRewriter.toClientItem(item, true);
if (tag.getListTag("ench") != null) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
}
if (tag.getListTag("StoredEnchantments") != null) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
}
enchantmentRewriter.handleToClient(item);
return item;
}
@ -318,12 +313,7 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter<ClientboundPac
// Rewrite spawn eggs (id checks are done in the method itself)
EntityIdRewriter.toServerItem(item, true);
if (tag.getListTag(getNbtTagName() + "|ench") != null) {
enchantmentRewriter.rewriteEnchantmentsToServer(tag, false);
}
if (tag.getListTag(getNbtTagName() + "|StoredEnchantments") != null) {
enchantmentRewriter.rewriteEnchantmentsToServer(tag, true);
}
enchantmentRewriter.handleToServer(item);
return item;
}

View File

@ -93,15 +93,7 @@ public class ItemPackets1_11_1 extends LegacyBlockItemRewriter<ClientboundPacket
if (item == null) return null;
super.handleItemToClient(item);
CompoundTag tag = item.tag();
if (tag == null) return item;
if (tag.getListTag("ench") != null) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
}
if (tag.getListTag("StoredEnchantments") != null) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
}
enchantmentRewriter.handleToClient(item);
return item;
}
@ -110,15 +102,7 @@ public class ItemPackets1_11_1 extends LegacyBlockItemRewriter<ClientboundPacket
if (item == null) return null;
super.handleItemToServer(item);
CompoundTag tag = item.tag();
if (tag == null) return item;
if (tag.getListTag(getNbtTagName() + "|ench") != null) {
enchantmentRewriter.rewriteEnchantmentsToServer(tag, false);
}
if (tag.getListTag(getNbtTagName() + "|StoredEnchantments") != null) {
enchantmentRewriter.rewriteEnchantmentsToServer(tag, true);
}
enchantmentRewriter.handleToServer(item);
return item;
}
}