Merge pull request #114 from KennyTV/dev

Remap 1.14 enchantments
This commit is contained in:
Mats 2019-07-03 21:37:31 +02:00 committed by GitHub
commit cba2566c21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 272 additions and 247 deletions

View File

@ -15,7 +15,7 @@
<parent> <parent>
<artifactId>viabackwards-parent</artifactId> <artifactId>viabackwards-parent</artifactId>
<groupId>nl.matsv</groupId> <groupId>nl.matsv</groupId>
<version>3.0.0-1.14.3</version> <version>3.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<parent> <parent>
<artifactId>viabackwards-parent</artifactId> <artifactId>viabackwards-parent</artifactId>
<groupId>nl.matsv</groupId> <groupId>nl.matsv</groupId>
<version>3.0.0-1.14.3</version> <version>3.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<parent> <parent>
<artifactId>viabackwards-parent</artifactId> <artifactId>viabackwards-parent</artifactId>
<groupId>nl.matsv</groupId> <groupId>nl.matsv</groupId>
<version>3.0.0-1.14.3</version> <version>3.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<parent> <parent>
<artifactId>viabackwards-parent</artifactId> <artifactId>viabackwards-parent</artifactId>
<groupId>nl.matsv</groupId> <groupId>nl.matsv</groupId>
<version>3.0.0-1.14.3</version> <version>3.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -698,133 +698,12 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
// ench is now Enchantments and now uses identifiers // ench is now Enchantments and now uses identifiers
if (tag.get("Enchantments") instanceof ListTag) { if (tag.get("Enchantments") instanceof ListTag) {
ListTag enchantments = tag.get("Enchantments"); rewriteEnchantmentsToClient(tag, false);
ListTag noMapped = new ListTag(NBT_TAG_NAME + "|Enchantments", CompoundTag.class);
ListTag ench = new ListTag("ench", CompoundTag.class);
List<Tag> lore = new ArrayList<>();
boolean dummyEnchatment = true;
for (Tag enchantmentEntry : enchantments.clone()) {
if (enchantmentEntry instanceof CompoundTag) {
CompoundTag enchEntry = new CompoundTag("");
String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
if (enchantmentMappings.containsKey(newId)) {
lore.add(new StringTag("", enchantmentMappings.get(newId)));
noMapped.add(enchantmentEntry);
} else {
dummyEnchatment = false;
Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId);
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
enchEntry.put(
new ShortTag(
"id",
oldId
)
);
enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()));
ench.add(enchEntry);
}
}
}
if (dummyEnchatment && enchantments.size() > 0) {
ByteTag hideFlags = tag.get("HideFlags");
if (hideFlags == null) {
hideFlags = new ByteTag("HideFlags");
tag.put(new ByteTag(NBT_TAG_NAME + "|noHideFlags"));
}
tag.put(new ByteTag(NBT_TAG_NAME + "|dummyEnchatment", hideFlags.getValue()));
CompoundTag enchEntry = new CompoundTag("");
enchEntry.put(new ShortTag("id", (short) 0));
enchEntry.put(new ShortTag("lvl", (short) 1));
byte value = hideFlags.getValue();
hideFlags.setValue(value |= 1);
tag.put(hideFlags);
ench.add(enchEntry);
}
tag.remove("Enchantment");
tag.put(noMapped);
tag.put(ench);
if (!lore.isEmpty()) {
CompoundTag display = tag.get("display");
if (display == null) {
tag.put(display = new CompoundTag("display"));
tag.put(new ByteTag(NBT_TAG_NAME + "|noDisplay"));
}
ListTag loreTag = display.get("Lore");
if (loreTag == null) {
display.put(loreTag = new ListTag("Lore", StringTag.class));
}
ListTag oldLore = new ListTag(NBT_TAG_NAME + "|OldLore", StringTag.class);
Iterator<Tag> iterator = lore.iterator();
while (iterator.hasNext()) {
oldLore.add(iterator.next().clone());
}
display.put(oldLore);
lore.addAll(loreTag.getValue());
loreTag.setValue(lore);
}
} }
if (tag.get("StoredEnchantments") instanceof ListTag) { if (tag.get("StoredEnchantments") instanceof ListTag) {
ListTag storedEnch = tag.get("StoredEnchantments"); rewriteEnchantmentsToClient(tag, true);
ListTag noMapped = new ListTag(NBT_TAG_NAME + "|StoredEnchantments", CompoundTag.class);
ListTag newStoredEnch = new ListTag("StoredEnchantments", CompoundTag.class);
List<Tag> lore = new ArrayList<>();
for (Tag enchantmentEntry : storedEnch) {
if (enchantmentEntry instanceof CompoundTag) {
CompoundTag enchEntry = new CompoundTag("");
String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
if (enchantmentMappings.containsKey(newId)) {
lore.add(new StringTag("", enchantmentMappings.get(newId)));
noMapped.add(enchantmentEntry);
} else {
Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId);
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
enchEntry.put(
new ShortTag(
"id",
oldId
)
);
enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()));
newStoredEnch.add(enchEntry);
}
}
}
tag.remove("StoredEnchantments");
tag.put(noMapped);
tag.put(newStoredEnch);
if (!lore.isEmpty()) {
CompoundTag display = tag.get("display");
if (display == null) {
tag.put(display = new CompoundTag("display"));
tag.put(new ByteTag(NBT_TAG_NAME + "|noDisplay"));
}
ListTag loreTag = display.get("Lore");
if (loreTag == null) {
display.put(loreTag = new ListTag("Lore", StringTag.class));
}
ListTag oldLore = new ListTag(NBT_TAG_NAME + "|OldLore", StringTag.class);
Iterator<Tag> iterator = lore.iterator();
while (iterator.hasNext()) {
oldLore.add(iterator.next().clone());
}
display.put(oldLore);
lore.addAll(loreTag.getValue());
loreTag.setValue(lore);
}
} }
if (tag.get(NBT_TAG_NAME + "|CanPlaceOn") instanceof ListTag) { if (tag.get(NBT_TAG_NAME + "|CanPlaceOn") instanceof ListTag) {
tag.put(ConverterRegistry.convertToTag( tag.put(ConverterRegistry.convertToTag(
"CanPlaceOn", "CanPlaceOn",
@ -877,6 +756,81 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
return item; return item;
} }
private void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnch) {
String key = storedEnch ? "StoredEnchantments" : "Enchantments";
ListTag enchantments = tag.get(key);
ListTag noMapped = new ListTag(NBT_TAG_NAME + "|" + key, CompoundTag.class);
ListTag newEnchantments = new ListTag(storedEnch ? key : "ench", CompoundTag.class);
List<Tag> lore = new ArrayList<>();
boolean hasValidEnchants = false;
for (Tag enchantmentEntry : enchantments.clone()) {
CompoundTag enchEntry = new CompoundTag("");
String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
if (enchantmentMappings.containsKey(newId)) {
lore.add(new StringTag("", enchantmentMappings.get(newId) + " "
+ getRomanNumber((Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue())));
noMapped.add(enchantmentEntry);
} else if (!newId.isEmpty()) {
Short oldId = MappingData.oldEnchantmentsIds.inverse().get(newId);
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
Short level = (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue();
if (level != 0)
hasValidEnchants = true;
enchEntry.put(new ShortTag("id", oldId));
enchEntry.put(new ShortTag("lvl", level));
newEnchantments.add(enchEntry);
}
}
// Put here to hide empty enchantment from 1.14 rewrites
if (!storedEnch && !hasValidEnchants) {
IntTag hideFlags = tag.get("HideFlags");
if (hideFlags == null) {
hideFlags = new IntTag("HideFlags");
} else {
tag.put(new IntTag(NBT_TAG_NAME + "|OldHideFlags", hideFlags.getValue()));
}
if (newEnchantments.size() == 0) {
CompoundTag enchEntry = new CompoundTag("");
enchEntry.put(new ShortTag("id", (short) 0));
enchEntry.put(new ShortTag("lvl", (short) 0));
newEnchantments.add(enchEntry);
}
int value = hideFlags.getValue() | 1;
hideFlags.setValue(value);
tag.put(hideFlags);
}
if (!lore.isEmpty()) {
tag.put(noMapped);
CompoundTag display = tag.get("display");
if (display == null) {
tag.put(display = new CompoundTag("display"));
}
ListTag loreTag = display.get("Lore");
if (loreTag == null) {
display.put(loreTag = new ListTag("Lore", StringTag.class));
}
ListTag oldLore = new ListTag(NBT_TAG_NAME + "|OldLore", StringTag.class);
for (Tag value : lore) {
oldLore.add(value.clone());
}
display.put(oldLore);
lore.addAll(loreTag.getValue());
loreTag.setValue(lore);
}
tag.remove("Enchantments");
tag.put(newEnchantments);
}
@Override @Override
protected Item handleItemToServer(Item item) { protected Item handleItemToServer(Item item) {
if (item == null) return null; if (item == null) return null;
@ -949,116 +903,12 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
} }
// ench is now Enchantments and now uses identifiers // ench is now Enchantments and now uses identifiers
if (tag.get("ench") instanceof ListTag) { if (tag.get("ench") instanceof ListTag) {
ListTag ench = tag.get("ench"); rewriteEnchantmentsToServer(tag, false);
ListTag enchantments = new ListTag("Enchantments", CompoundTag.class);
boolean dummyEnchatment = false;
if (tag.get(NBT_TAG_NAME + "|dummyEnchatment") instanceof ByteTag) {
ByteTag dummy = tag.get(NBT_TAG_NAME + "|dummyEnchatment");
if (tag.get("HideFlags") instanceof ByteTag) {
if (tag.get(NBT_TAG_NAME + "|noHideFlags") instanceof ByteTag) {
tag.remove("HideFlags");
tag.remove(NBT_TAG_NAME + "|noHideFlags");
} else {
ByteTag hideFlags = tag.get("HideFlags");
hideFlags.setValue(dummy.getValue());
}
}
dummyEnchatment = true;
tag.remove(NBT_TAG_NAME + "|dummyEnchatment");
}
for (Tag enchEntry : ench) {
if (enchEntry instanceof CompoundTag) {
CompoundTag enchantmentEntry = new CompoundTag("");
short oldId = ((Number) ((CompoundTag) enchEntry).get("id").getValue()).shortValue();
if (dummyEnchatment && oldId == 0) {
continue; //Skip dummy enchatment
}
String newId = MappingData.oldEnchantmentsIds.get(oldId);
if (newId == null) {
newId = "viaversion:legacy/" + oldId;
}
enchantmentEntry.put(new StringTag("id", newId));
enchantmentEntry.put(new ShortTag("lvl", ((Number) ((CompoundTag) enchEntry).get("lvl").getValue()).shortValue()));
enchantments.add(enchantmentEntry);
}
}
if (tag.get(NBT_TAG_NAME + "|Enchantments") instanceof ListTag) {
ListTag noMapped = tag.get(NBT_TAG_NAME + "|Enchantments");
Iterator<Tag> iterator = noMapped.iterator();
while (iterator.hasNext()) {
enchantments.add(iterator.next());
}
tag.remove(NBT_TAG_NAME + "|Enchantments");
}
tag.remove("ench");
tag.put(enchantments);
} }
if (tag.get("StoredEnchantments") instanceof ListTag) { if (tag.get("StoredEnchantments") instanceof ListTag) {
ListTag storedEnch = tag.get("StoredEnchantments"); rewriteEnchantmentsToServer(tag, true);
ListTag newStoredEnch = new ListTag("StoredEnchantments", CompoundTag.class);
for (Tag enchEntry : storedEnch) {
if (enchEntry instanceof CompoundTag) {
CompoundTag enchantmentEntry = new CompoundTag("");
short oldId = ((Number) ((CompoundTag) enchEntry).get("id").getValue()).shortValue();
String newId = MappingData.oldEnchantmentsIds.get(oldId);
if (newId == null) {
newId = "viaversion:legacy/" + oldId;
}
enchantmentEntry.put(new StringTag("id",
newId
));
enchantmentEntry.put(new ShortTag("lvl", ((Number) ((CompoundTag) enchEntry).get("lvl").getValue()).shortValue()));
newStoredEnch.add(enchantmentEntry);
}
}
if (tag.get(NBT_TAG_NAME + "|Enchantments") instanceof ListTag) {
ListTag noMapped = tag.get(NBT_TAG_NAME + "|StoredEnchantments");
Iterator<Tag> iterator = noMapped.iterator();
while (iterator.hasNext()) {
newStoredEnch.add(iterator.next());
}
}
tag.remove("StoredEnchantments");
tag.put(newStoredEnch);
}
if (tag.get("CanPlaceOn") instanceof ListTag) {
ListTag old = tag.get("CanPlaceOn");
ListTag newCanPlaceOn = new ListTag("CanPlaceOn", StringTag.class);
tag.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|CanPlaceOn", ConverterRegistry.convertToValue(old))); // There will be data losing
for (Tag oldTag : old) {
Object value = oldTag.getValue();
String[] newValues = BlockIdData.blockIdMapping.get(value instanceof String
? ((String) value).replace("minecraft:", "")
: null);
if (newValues != null) {
for (String newValue : newValues) {
newCanPlaceOn.add(new StringTag("", newValue));
}
} else {
newCanPlaceOn.add(oldTag);
}
}
tag.put(newCanPlaceOn);
}
if (tag.get("CanDestroy") instanceof ListTag) {
ListTag old = tag.get("CanDestroy");
ListTag newCanDestroy = new ListTag("CanDestroy", StringTag.class);
tag.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|CanDestroy", ConverterRegistry.convertToValue(old))); // There will be data losing
for (Tag oldTag : old) {
Object value = oldTag.getValue();
String[] newValues = BlockIdData.blockIdMapping.get(value instanceof String
? ((String) value).replace("minecraft:", "")
: null);
if (newValues != null) {
for (String newValue : newValues) {
newCanDestroy.add(new StringTag("", newValue));
}
} else {
newCanDestroy.add(oldTag);
}
}
tag.put(newCanDestroy);
} }
// Handle SpawnEggs // Handle SpawnEggs
if (item.getId() == 383) { if (item.getId() == 383) {
if (tag.get("EntityTag") instanceof CompoundTag) { if (tag.get("EntityTag") instanceof CompoundTag) {
@ -1110,6 +960,68 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
return item; return item;
} }
private void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnch) {
String key = storedEnch ? "StoredEnchantments" : "Enchantments";
ListTag enchantments = tag.get(storedEnch ? key : "ench");
ListTag newEnchantments = new ListTag(key, CompoundTag.class);
boolean dummyEnchant = false;
if (!storedEnch) {
IntTag hideFlags = tag.get(NBT_TAG_NAME + "|OldHideFlags");
dummyEnchant = hideFlags != null;
if (dummyEnchant) {
tag.put(new IntTag("HideFlags", hideFlags.getValue()));
} else {
tag.remove("HideFlags");
}
}
for (Tag enchEntry : enchantments) {
CompoundTag enchantmentEntry = new CompoundTag("");
short oldId = ((Number) ((CompoundTag) enchEntry).get("id").getValue()).shortValue();
if (dummyEnchant && oldId == 0) {
continue; //Skip dummy enchatment
}
String newId = MappingData.oldEnchantmentsIds.get(oldId);
if (newId == null) {
newId = "viaversion:legacy/" + oldId;
}
enchantmentEntry.put(new StringTag("id", newId));
enchantmentEntry.put(new ShortTag("lvl", ((Number) ((CompoundTag) enchEntry).get("lvl").getValue()).shortValue()));
newEnchantments.add(enchantmentEntry);
}
ListTag noMapped = tag.get(NBT_TAG_NAME + "|Enchantments");
if (noMapped != null) {
for (Tag value : noMapped) {
newEnchantments.add(value);
}
tag.remove(NBT_TAG_NAME + "|Enchantments");
}
CompoundTag display = tag.get("display");
if (display == null) {
tag.put(display = new CompoundTag("display"));
}
ListTag oldLore = tag.get(NBT_TAG_NAME + "|OldLore");
if (oldLore != null) {
ListTag lore = display.get("Lore");
if (lore == null) {
tag.put(lore = new ListTag("Lore"));
}
lore.setValue(oldLore.getValue());
tag.remove(NBT_TAG_NAME + "|OldLore");
} else {
tag.remove("Lore");
if (display.isEmpty())
tag.remove("display");
}
if (!storedEnch)
tag.remove("ench");
tag.put(newEnchantments);
}
// TODO find a less hacky way to do this (https://bugs.mojang.com/browse/MC-74231) // TODO find a less hacky way to do this (https://bugs.mojang.com/browse/MC-74231)
private static void flowerPotSpecialTreatment(UserConnection user, int blockState, Position position) throws Exception { private static void flowerPotSpecialTreatment(UserConnection user, int blockState, Position position) throws Exception {
if (FlowerPotHandler.isFlowah(blockState)) { if (FlowerPotHandler.isFlowah(blockState)) {
@ -1138,4 +1050,31 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
} }
} }
public static String getRomanNumber(int number) {
switch (number) {
case 1:
return "I";
case 2:
return "II";
case 3:
return "III";
case 4:
return "IV";
case 5:
return "V";
case 6:
return "VI";
case 7:
return "VII";
case 8:
return "VIII";
case 9:
return "IX";
case 10:
return "X";
default:
return Integer.toString(number);
}
}
} }

View File

@ -6,6 +6,7 @@ import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.api.entities.types.AbstractEntityType; import nl.matsv.viabackwards.api.entities.types.AbstractEntityType;
import nl.matsv.viabackwards.api.entities.types.EntityType1_14; import nl.matsv.viabackwards.api.entities.types.EntityType1_14;
import nl.matsv.viabackwards.api.rewriters.BlockItemRewriter; import nl.matsv.viabackwards.api.rewriters.BlockItemRewriter;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.BlockItemPackets1_13;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
@ -28,17 +29,14 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import us.myles.viaversion.libs.opennbt.conversion.ConverterRegistry; import us.myles.viaversion.libs.opennbt.conversion.ConverterRegistry;
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; import us.myles.viaversion.libs.opennbt.tag.builtin.*;
import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.Tag;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Set;
public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14> { public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14> {
private static String NBT_TAG_NAME = "ViaBackwards|" + Protocol1_13_2To1_14.class.getSimpleName();
private static final String NBT_TAG_NAME = "ViaBackwards|" + Protocol1_13_2To1_14.class.getSimpleName();
private final Map<String, String> enchantmentMappings = new HashMap<>();
@Override @Override
protected void registerPackets(Protocol1_13_2To1_14 protocol) { protected void registerPackets(Protocol1_13_2To1_14 protocol) {
@ -736,6 +734,10 @@ public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14
rewrite(730).repItem(new Item((short) 734, (byte) 1, (short) -1, getNamedTag("1.14 Ravager Spawn Egg"))); rewrite(730).repItem(new Item((short) 734, (byte) 1, (short) -1, getNamedTag("1.14 Ravager Spawn Egg")));
rewrite(741).repItem(new Item((short) 698, (byte) 1, (short) -1, getNamedTag("1.14 Trader Llama Spawn Egg"))); rewrite(741).repItem(new Item((short) 698, (byte) 1, (short) -1, getNamedTag("1.14 Trader Llama Spawn Egg")));
rewrite(747).repItem(new Item((short) 739, (byte) 1, (short) -1, getNamedTag("1.14 Wandering Trader Spawn Egg"))); rewrite(747).repItem(new Item((short) 739, (byte) 1, (short) -1, getNamedTag("1.14 Wandering Trader Spawn Egg")));
enchantmentMappings.put("minecraft:multishot", "§7Multishot");
enchantmentMappings.put("minecraft:quick_charge", "§7Quick Charge");
enchantmentMappings.put("minecraft:piercing", "§7Piercing");
} }
@Override @Override
@ -768,10 +770,57 @@ public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14
display.remove(NBT_TAG_NAME + "|Lore"); display.remove(NBT_TAG_NAME + "|Lore");
} }
} }
if (tag.get("Enchantments") instanceof ListTag) {
rewriteEnchantmentsToClient(tag, false);
}
if (tag.get("StoredEnchantments") instanceof ListTag) {
rewriteEnchantmentsToClient(tag, true);
}
} }
return item; return item;
} }
private void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) {
String key = storedEnchant ? "StoredEnchantments" : "Enchantments";
ListTag enchantments = tag.get(key);
ListTag noMapped = new ListTag(NBT_TAG_NAME + "|" + key, CompoundTag.class);
List<Tag> lore = new ArrayList<>();
for (Tag enchantmentEntry : enchantments.clone()) {
String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
String enchantmentName = enchantmentMappings.get(newId);
if (enchantmentName != null) {
enchantments.remove(enchantmentEntry);
lore.add(new StringTag("", enchantmentMappings.get(newId) + " " + BlockItemPackets1_13.getRomanNumber((Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue())));
noMapped.add(enchantmentEntry);
}
}
if (!lore.isEmpty()) {
if (!storedEnchant && enchantments.size() == 0) {
CompoundTag dummyEnchantment = new CompoundTag("");
dummyEnchantment.put(new StringTag("id", ""));
dummyEnchantment.put(new ShortTag("lvl", (short) 0));
enchantments.add(dummyEnchantment);
tag.put(new ByteTag(NBT_TAG_NAME + "|dummyEnchant"));
}
tag.put(noMapped);
CompoundTag display = tag.get("display");
if (display == null) {
tag.put(display = new CompoundTag("display"));
}
ListTag loreTag = display.get("Lore");
if (loreTag == null) {
display.put(loreTag = new ListTag("Lore", StringTag.class));
}
lore.addAll(loreTag.getValue());
loreTag.setValue(lore);
}
}
@Override @Override
public Item handleItemToServer(Item item) { public Item handleItemToServer(Item item) {
if (item == null) return null; if (item == null) return null;
@ -797,10 +846,47 @@ public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14
} }
} }
} }
if (tag.contains(NBT_TAG_NAME + "|Enchantments")) {
rewriteEnchantmentsToServer(tag, false);
}
if (tag.contains(NBT_TAG_NAME + "|StoredEnchantments")) {
rewriteEnchantmentsToServer(tag, true);
}
} }
return item; return item;
} }
private void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant) {
String key = storedEnchant ? "StoredEnchantments" : "Enchantments";
ListTag newEnchantments = tag.get(NBT_TAG_NAME + "|" + key);
ListTag enchantments = tag.contains(key) ? tag.get(key) : new ListTag(key, CompoundTag.class);
if (!storedEnchant && tag.contains(NBT_TAG_NAME + "|dummyEnchant")) {
tag.remove(NBT_TAG_NAME + "|dummyEnchant");
for (Tag enchantment : enchantments.clone()) {
String id = (String) ((CompoundTag) enchantment).get("id").getValue();
if (id.isEmpty())
enchantments.remove(enchantment);
}
}
CompoundTag display = tag.get("display");
// A few null checks just to be safe, though they shouldn't actually be
ListTag lore = display != null ? display.get("Lore") : null;
for (Tag enchantment : newEnchantments.clone()) {
enchantments.add(enchantment);
if (lore != null && lore.size() != 0)
lore.remove(lore.get(0));
}
if (lore != null && lore.size() == 0) {
display.remove("Lore");
if (display.isEmpty())
tag.remove("display");
}
tag.put(enchantments);
tag.remove(newEnchantments.getName());
}
@Override @Override
protected CompoundTag getNamedTag(String text) { protected CompoundTag getNamedTag(String text) {
CompoundTag tag = new CompoundTag(""); CompoundTag tag = new CompoundTag("");

View File

@ -15,7 +15,7 @@
<parent> <parent>
<artifactId>viabackwards-parent</artifactId> <artifactId>viabackwards-parent</artifactId>
<groupId>nl.matsv</groupId> <groupId>nl.matsv</groupId>
<version>3.0.0-1.14.3</version> <version>3.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -16,7 +16,7 @@
<groupId>nl.matsv</groupId> <groupId>nl.matsv</groupId>
<artifactId>viabackwards-parent</artifactId> <artifactId>viabackwards-parent</artifactId>
<version>3.0.0-1.14.3</version> <version>3.0.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<description>Allow newer clients to join older server versions.</description> <description>Allow newer clients to join older server versions.</description>

View File

@ -15,7 +15,7 @@
<parent> <parent>
<artifactId>viabackwards-parent</artifactId> <artifactId>viabackwards-parent</artifactId>
<groupId>nl.matsv</groupId> <groupId>nl.matsv</groupId>
<version>3.0.0-1.14.3</version> <version>3.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<parent> <parent>
<artifactId>viabackwards-parent</artifactId> <artifactId>viabackwards-parent</artifactId>
<groupId>nl.matsv</groupId> <groupId>nl.matsv</groupId>
<version>3.0.0-1.14.3</version> <version>3.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>