Updated Via NBT usage

This commit is contained in:
RaphiMC 2024-03-09 14:21:24 +01:00
parent 2bd416ac96
commit 87d16d3724
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
2 changed files with 27 additions and 36 deletions

View File

@ -27,9 +27,9 @@ import com.viaversion.viaversion.api.rewriter.RewriterBase;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.fastutil.objects.ObjectArrayList; import com.viaversion.viaversion.libs.fastutil.objects.ObjectArrayList;
import com.viaversion.viaversion.libs.fastutil.objects.ObjectList; import com.viaversion.viaversion.libs.fastutil.objects.ObjectList;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.*; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag;
import java.util.List; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
public abstract class LegacyItemRewriter<P extends Protocol> extends RewriterBase<P> implements ItemRewriter<P> { public abstract class LegacyItemRewriter<P extends Protocol> extends RewriterBase<P> implements ItemRewriter<P> {
@ -48,7 +48,7 @@ public abstract class LegacyItemRewriter<P extends Protocol> extends RewriterBas
public LegacyItemRewriter(final P protocol, final String protocolName, final Type<Item> itemType, final Type<Item[]> itemArrayType, final Type<Item> mappedItemType, final Type<Item[]> mappedItemArrayType) { public LegacyItemRewriter(final P protocol, final String protocolName, final Type<Item> itemType, final Type<Item[]> itemArrayType, final Type<Item> mappedItemType, final Type<Item[]> mappedItemArrayType) {
super(protocol); super(protocol);
this.tagName = protocolName.replace(".", "_") + "_ViaLegacy_" + System.currentTimeMillis(); this.tagName = "ViaLegacy_" + protocolName.replace(".", "_");
this.protocolName = protocolName; this.protocolName = protocolName;
this.itemType = itemType; this.itemType = itemType;
this.itemArrayType = itemArrayType; this.itemArrayType = itemArrayType;
@ -167,65 +167,56 @@ public abstract class LegacyItemRewriter<P extends Protocol> extends RewriterBas
} }
private void setRemappedNameRead(final Item item, final String name) { private void setRemappedNameRead(final Item item, final String name) {
//Set ViaLegacy tag for later remapping final CompoundTag viaLegacyTag = new CompoundTag();
final CompoundTag viaLegacyTag = (item.tag() != null && item.tag().contains(tagName) ? item.tag().get(tagName) : new CompoundTag()); viaLegacyTag.putInt("Id", item.identifier());
if (item.tag() == null || !item.tag().contains(tagName)) { viaLegacyTag.putShort("Meta", item.data());
viaLegacyTag.put("Id", new IntTag(item.identifier()));
viaLegacyTag.put("Meta", new ShortTag(item.data()));
}
//Get Item tag
CompoundTag tag = item.tag(); CompoundTag tag = item.tag();
if (tag == null) { if (tag == null) {
tag = new CompoundTag(); tag = new CompoundTag();
item.setTag(tag); item.setTag(tag);
viaLegacyTag.put("RemoveTag", new IntTag(0)); viaLegacyTag.putBoolean("RemoveTag", true);
} }
tag.put(tagName, viaLegacyTag); tag.put(this.tagName, viaLegacyTag);
//Set name/lore of item CompoundTag display = tag.getCompoundTag("display");
CompoundTag display = tag.get("display");
if (display == null) { if (display == null) {
display = new CompoundTag(); display = new CompoundTag();
tag.put("display", display); tag.put("display", display);
viaLegacyTag.put("RemoveDisplayTag", new IntTag(0)); viaLegacyTag.putBoolean("RemoveDisplayTag", true);
} }
if (display.contains("Name")) { if (display.contains("Name")) {
ListTag lore = display.get("Lore"); ListTag<StringTag> lore = display.getListTag("Lore", StringTag.class);
if (lore == null) { if (lore == null) {
lore = new ListTag(); lore = new ListTag<>(StringTag.class);
display.put("Lore", lore); display.put("Lore", lore);
viaLegacyTag.put("RemoveLore", new IntTag(0)); viaLegacyTag.putBoolean("RemoveLore", true);
} }
lore.add(new StringTag("§r " + this.protocolName + " Item ID: " + item.identifier() + " (" + name + ")")); lore.add(new StringTag("§r " + this.protocolName + " Item ID: " + item.identifier() + " (" + name + ")"));
viaLegacyTag.put("RemoveLastLore", new IntTag(0)); viaLegacyTag.putBoolean("RemoveLastLore", true);
} else { } else {
display.put("Name", new StringTag("§r" + this.protocolName + " " + name)); display.putString("Name", "§r" + this.protocolName + " " + name);
viaLegacyTag.put("RemoveDisplayName", new IntTag(0)); viaLegacyTag.putBoolean("RemoveDisplayName", true);
} }
} }
private void setRemappedTagWrite(final Item item) { private void setRemappedTagWrite(final Item item) {
if (item.tag() == null) return;
if (!item.tag().contains(tagName)) return;
final CompoundTag tag = item.tag(); final CompoundTag tag = item.tag();
final CompoundTag viaLegacyTag = tag.get(tagName); if (tag == null) return;
tag.remove(tagName); final CompoundTag viaLegacyTag = tag.removeUnchecked(this.tagName);
if (viaLegacyTag == null) return;
item.setIdentifier(((IntTag) viaLegacyTag.get("Id")).asInt()); item.setIdentifier(viaLegacyTag.getNumberTag("Id").asInt());
item.setData(((ShortTag) viaLegacyTag.get("Meta")).asShort()); item.setData(viaLegacyTag.getNumberTag("Meta").asShort());
if (viaLegacyTag.contains("RemoveLastLore")) { if (viaLegacyTag.contains("RemoveLastLore")) {
ListTag lore = ((CompoundTag) tag.get("display")).get("Lore"); final ListTag<StringTag> lore = tag.getCompoundTag("display").getListTag("Lore", StringTag.class);
List<Tag> tags = lore.getValue(); lore.remove(lore.size() - 1);
tags.remove(lore.size() - 1);
lore.setValue(tags);
} }
if (viaLegacyTag.contains("RemoveLore")) { if (viaLegacyTag.contains("RemoveLore")) {
((CompoundTag) tag.get("display")).remove("Lore"); tag.getCompoundTag("display").remove("Lore");
} }
if (viaLegacyTag.contains("RemoveDisplayName")) { if (viaLegacyTag.contains("RemoveDisplayName")) {
((CompoundTag) tag.get("display")).remove("Name"); tag.getCompoundTag("display").remove("Name");
} }
if (viaLegacyTag.contains("RemoveDisplayTag")) { if (viaLegacyTag.contains("RemoveDisplayTag")) {
tag.remove("display"); tag.remove("display");

View File

@ -360,7 +360,7 @@ public class Protocolb1_0_1_1_1toa1_2_3_5_1_2_6 extends StatelessProtocol<Client
} }
private void readItemsFromTag(final CompoundTag tag, final Item[] items) { private void readItemsFromTag(final CompoundTag tag, final Item[] items) {
final ListTag slotList = tag.get("Items"); final ListTag<?> slotList = tag.get("Items");
for (Tag itemTag : slotList) { for (Tag itemTag : slotList) {
final CompoundTag slotTag = (CompoundTag) itemTag; final CompoundTag slotTag = (CompoundTag) itemTag;
items[slotTag.<ByteTag>get("Slot").asByte() & 255] = new DataItem(slotTag.<ShortTag>get("id").asShort(), slotTag.<ByteTag>get("Count").asByte(), slotTag.<ShortTag>get("Damage").asShort(), null); items[slotTag.<ByteTag>get("Slot").asByte() & 255] = new DataItem(slotTag.<ShortTag>get("id").asShort(), slotTag.<ByteTag>get("Count").asByte(), slotTag.<ShortTag>get("Damage").asShort(), null);