Skip to content

Commit 0aacfe3

Browse files
Add item handler methods to LegacyEnchantmentRewriter (#709)
1 parent 4547d5c commit 0aacfe3

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package com.viaversion.viabackwards.api.rewriters;
1919

20+
import com.viaversion.viaversion.api.minecraft.item.Item;
2021
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag;
2122
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
2223
import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag;
@@ -45,6 +46,30 @@ public void registerEnchantment(int id, String replacementLore) {
4546
enchantmentMappings.put((short) id, replacementLore);
4647
}
4748

49+
public void handleToClient(Item item) {
50+
CompoundTag tag = item.tag();
51+
if (tag == null) return;
52+
53+
if (tag.getListTag("ench") != null) {
54+
rewriteEnchantmentsToClient(tag, false);
55+
}
56+
if (tag.getListTag("StoredEnchantments") != null) {
57+
rewriteEnchantmentsToClient(tag, true);
58+
}
59+
}
60+
61+
public void handleToServer(Item item) {
62+
CompoundTag tag = item.tag();
63+
if (tag == null) return;
64+
65+
if (tag.contains(nbtTagName + "|ench")) {
66+
rewriteEnchantmentsToServer(tag, false);
67+
}
68+
if (tag.contains(nbtTagName + "|StoredEnchantments")) {
69+
rewriteEnchantmentsToServer(tag, true);
70+
}
71+
}
72+
4873
public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) {
4974
String key = storedEnchant ? "StoredEnchantments" : "ench";
5075
ListTag<CompoundTag> enchantments = tag.getListTag(key, CompoundTag.class);

common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,7 @@ public Item handleItemToClient(Item item) {
298298
// Rewrite spawn eggs (id checks are done in the method itself)
299299
EntityIdRewriter.toClientItem(item, true);
300300

301-
if (tag.getListTag("ench") != null) {
302-
enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
303-
}
304-
if (tag.getListTag("StoredEnchantments") != null) {
305-
enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
306-
}
301+
enchantmentRewriter.handleToClient(item);
307302
return item;
308303
}
309304

@@ -318,12 +313,7 @@ public Item handleItemToServer(Item item) {
318313
// Rewrite spawn eggs (id checks are done in the method itself)
319314
EntityIdRewriter.toServerItem(item, true);
320315

321-
if (tag.getListTag(getNbtTagName() + "|ench") != null) {
322-
enchantmentRewriter.rewriteEnchantmentsToServer(tag, false);
323-
}
324-
if (tag.getListTag(getNbtTagName() + "|StoredEnchantments") != null) {
325-
enchantmentRewriter.rewriteEnchantmentsToServer(tag, true);
326-
}
316+
enchantmentRewriter.handleToServer(item);
327317
return item;
328318
}
329319

common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11to1_11_1/packets/ItemPackets1_11_1.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,7 @@ public Item handleItemToClient(Item item) {
9393
if (item == null) return null;
9494
super.handleItemToClient(item);
9595

96-
CompoundTag tag = item.tag();
97-
if (tag == null) return item;
98-
99-
if (tag.getListTag("ench") != null) {
100-
enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
101-
}
102-
if (tag.getListTag("StoredEnchantments") != null) {
103-
enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
104-
}
96+
enchantmentRewriter.handleToClient(item);
10597
return item;
10698
}
10799

@@ -110,15 +102,7 @@ public Item handleItemToServer(Item item) {
110102
if (item == null) return null;
111103
super.handleItemToServer(item);
112104

113-
CompoundTag tag = item.tag();
114-
if (tag == null) return item;
115-
116-
if (tag.getListTag(getNbtTagName() + "|ench") != null) {
117-
enchantmentRewriter.rewriteEnchantmentsToServer(tag, false);
118-
}
119-
if (tag.getListTag(getNbtTagName() + "|StoredEnchantments") != null) {
120-
enchantmentRewriter.rewriteEnchantmentsToServer(tag, true);
121-
}
105+
enchantmentRewriter.handleToServer(item);
122106
return item;
123107
}
124108
}

0 commit comments

Comments
 (0)