mirror of
https://github.com/ViaVersion/ViaBackwards.git
synced 2024-12-29 18:07:40 +01:00
Eliminate full copying/saving of item tags
This unnecessarily stores a hell of a lot of unneeded data and heavily accumulates the more versions the pipeline goes down to.
This commit is contained in:
parent
9f9d05d3c6
commit
b2b8c48fd0
@ -5,6 +5,7 @@ import nl.matsv.viabackwards.api.data.MappedItem;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import us.myles.ViaVersion.api.minecraft.item.Item;
|
import us.myles.ViaVersion.api.minecraft.item.Item;
|
||||||
import us.myles.ViaVersion.api.rewriters.IdRewriteFunction;
|
import us.myles.ViaVersion.api.rewriters.IdRewriteFunction;
|
||||||
|
import us.myles.viaversion.libs.opennbt.tag.builtin.ByteTag;
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
|
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag;
|
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.StringTag;
|
||||||
@ -33,38 +34,41 @@ public abstract class ItemRewriter<T extends BackwardsProtocol> extends ItemRewr
|
|||||||
public Item handleItemToClient(Item item) {
|
public Item handleItemToClient(Item item) {
|
||||||
if (item == null) return null;
|
if (item == null) return null;
|
||||||
|
|
||||||
CompoundTag tag = null;
|
CompoundTag display = null;
|
||||||
boolean textChanged = false;
|
|
||||||
if (translatableRewriter != null
|
if (translatableRewriter != null
|
||||||
&& item.getTag() != null && (tag = item.getTag().get("display")) != null) {
|
&& item.getTag() != null && (display = item.getTag().get("display")) != null) {
|
||||||
// Handle name and lore components
|
// Handle name and lore components
|
||||||
StringTag name = tag.get("Name");
|
StringTag name = display.get("Name");
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
String newValue = translatableRewriter.processText(name.getValue()).toString();
|
String newValue = translatableRewriter.processText(name.getValue()).toString();
|
||||||
if (name.getValue().equals(newValue)) {
|
if (!newValue.equals(name.getValue())) {
|
||||||
textChanged = true;
|
saveNameTag(display, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
name.setValue(newValue);
|
name.setValue(newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
ListTag lore = tag.get("Lore");
|
ListTag lore = display.get("Lore");
|
||||||
if (lore != null) {
|
if (lore != null) {
|
||||||
|
ListTag original = null;
|
||||||
|
boolean changed = false;
|
||||||
for (Tag loreEntry : lore) {
|
for (Tag loreEntry : lore) {
|
||||||
if (!(loreEntry instanceof StringTag)) continue;
|
if (!(loreEntry instanceof StringTag)) continue;
|
||||||
|
|
||||||
StringTag stringTag = (StringTag) loreEntry;
|
StringTag stringTag = (StringTag) loreEntry;
|
||||||
String newValue = translatableRewriter.processText(stringTag.getValue()).toString();
|
String newValue = translatableRewriter.processText(stringTag.getValue()).toString();
|
||||||
if (stringTag.getValue().equals(newValue)) {
|
if (!changed && !newValue.equals(name.getValue())) {
|
||||||
textChanged = true;
|
changed = true;
|
||||||
|
original = lore.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
stringTag.setValue(newValue);
|
stringTag.setValue(newValue);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (textChanged) {
|
if (changed) {
|
||||||
// Backup data for toServer
|
saveLoreTag(display, original);
|
||||||
item.getTag().put(createViaNBT(item));
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MappedItem data = mappedItemFunction.get(item.getIdentifier());
|
MappedItem data = mappedItemFunction.get(item.getIdentifier());
|
||||||
@ -73,23 +77,19 @@ public abstract class ItemRewriter<T extends BackwardsProtocol> extends ItemRewr
|
|||||||
return super.handleItemToClient(item);
|
return super.handleItemToClient(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backup data for toServer if not already done above
|
|
||||||
if (!textChanged) {
|
|
||||||
if (item.getTag() == null) {
|
|
||||||
item.setTag(new CompoundTag(""));
|
|
||||||
}
|
|
||||||
item.getTag().put(createViaNBT(item));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set remapped id
|
// Set remapped id
|
||||||
item.setIdentifier(data.getId());
|
item.setIdentifier(data.getId());
|
||||||
|
|
||||||
// Set custom name - only done if there is no original one
|
// Set custom name - only done if there is no original one
|
||||||
if (tag == null) {
|
if (item.getTag() == null) {
|
||||||
item.getTag().put(tag = new CompoundTag("display"));
|
item.setTag(new CompoundTag(""));
|
||||||
}
|
}
|
||||||
if (!tag.contains("Name")) {
|
if (display == null) {
|
||||||
tag.put(new StringTag("Name", data.getJsonName()));
|
item.getTag().put(display = new CompoundTag("display"));
|
||||||
|
}
|
||||||
|
if (!display.contains("Name")) {
|
||||||
|
display.put(new StringTag("Name", data.getJsonName()));
|
||||||
|
display.put(new ByteTag(nbtTagName + "|customName"));
|
||||||
}
|
}
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,9 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import us.myles.ViaVersion.api.minecraft.item.Item;
|
import us.myles.ViaVersion.api.minecraft.item.Item;
|
||||||
import us.myles.ViaVersion.api.rewriters.IdRewriteFunction;
|
import us.myles.ViaVersion.api.rewriters.IdRewriteFunction;
|
||||||
import us.myles.viaversion.libs.opennbt.conversion.builtin.CompoundTagConverter;
|
import us.myles.viaversion.libs.opennbt.conversion.builtin.CompoundTagConverter;
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.ByteTag;
|
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
|
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.ShortTag;
|
import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag;
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.Tag;
|
import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag;
|
||||||
|
|
||||||
public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewriter<T> {
|
public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewriter<T> {
|
||||||
|
|
||||||
@ -42,53 +41,42 @@ public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewr
|
|||||||
@Nullable
|
@Nullable
|
||||||
public Item handleItemToServer(Item item) {
|
public Item handleItemToServer(Item item) {
|
||||||
if (item == null) return null;
|
if (item == null) return null;
|
||||||
|
if (toServerRewriter != null) {
|
||||||
CompoundTag tag = item.getTag();
|
item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier()));
|
||||||
if (tag == null) {
|
|
||||||
if (toServerRewriter != null) {
|
|
||||||
item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier()));
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
CompoundTag viaTag = tag.remove(nbtTagName);
|
|
||||||
if (viaTag != null) {
|
|
||||||
short id = (short) viaTag.get("id").getValue();
|
|
||||||
item.setIdentifier(id);
|
|
||||||
|
|
||||||
Tag dataTag = viaTag.get("data");
|
|
||||||
short data = dataTag != null ? (short) dataTag.getValue() : 0;
|
|
||||||
item.setData(data);
|
|
||||||
|
|
||||||
Tag amountTag = viaTag.get("amount");
|
|
||||||
byte amount = amountTag != null ? (byte) amountTag.getValue() : 1;
|
|
||||||
item.setAmount(amount);
|
|
||||||
|
|
||||||
CompoundTag extras = viaTag.get("extras");
|
|
||||||
if (extras != null) {
|
|
||||||
item.setTag(CONVERTER.convert("", CONVERTER.convert(extras)));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Rewrite id normally
|
|
||||||
if (toServerRewriter != null) {
|
|
||||||
item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
restoreDisplayTag(item);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected CompoundTag createViaNBT(Item item) {
|
protected void saveNameTag(CompoundTag displayTag, StringTag original) {
|
||||||
CompoundTag tag = new CompoundTag(nbtTagName);
|
displayTag.put(new StringTag(nbtTagName + "|o" + original.getName(), original.getValue()));
|
||||||
tag.put(new ShortTag("id", (short) item.getIdentifier()));
|
}
|
||||||
if (item.getAmount() != 1) {
|
|
||||||
tag.put(new ByteTag("amount", item.getAmount()));
|
protected void saveLoreTag(CompoundTag displayTag, ListTag original) {
|
||||||
|
displayTag.put(new ListTag(nbtTagName + "|o" + original.getName(), original.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void restoreDisplayTag(Item item) {
|
||||||
|
if (item.getTag() == null) return;
|
||||||
|
|
||||||
|
CompoundTag display = item.getTag().get("display");
|
||||||
|
if (display != null) {
|
||||||
|
// Remove custom name / restore original name
|
||||||
|
if (display.remove(nbtTagName + "|customName") != null) {
|
||||||
|
display.remove("Name");
|
||||||
|
} else {
|
||||||
|
restoreDisplayTag(display, "Name");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore lore
|
||||||
|
restoreDisplayTag(display, "Lore");
|
||||||
}
|
}
|
||||||
if (item.getData() != 0) {
|
}
|
||||||
tag.put(new ShortTag("data", item.getData()));
|
|
||||||
|
private void restoreDisplayTag(CompoundTag displayTag, String tagName) {
|
||||||
|
StringTag original = displayTag.remove(nbtTagName + "|o" + tagName);
|
||||||
|
if (original != null) {
|
||||||
|
displayTag.put(original);
|
||||||
}
|
}
|
||||||
if (item.getTag() != null) {
|
|
||||||
tag.put(CONVERTER.convert("extras", CONVERTER.convert(item.getTag())));
|
|
||||||
}
|
|
||||||
return tag;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ import us.myles.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
|
|||||||
import us.myles.viaversion.libs.gson.JsonElement;
|
import us.myles.viaversion.libs.gson.JsonElement;
|
||||||
import us.myles.viaversion.libs.gson.JsonObject;
|
import us.myles.viaversion.libs.gson.JsonObject;
|
||||||
import us.myles.viaversion.libs.gson.JsonPrimitive;
|
import us.myles.viaversion.libs.gson.JsonPrimitive;
|
||||||
|
import us.myles.viaversion.libs.opennbt.tag.builtin.ByteTag;
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
|
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.IntTag;
|
import us.myles.viaversion.libs.opennbt.tag.builtin.IntTag;
|
||||||
import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag;
|
import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag;
|
||||||
@ -98,14 +99,7 @@ public abstract class LegacyBlockItemRewriter<T extends BackwardsProtocol> exten
|
|||||||
return super.handleItemToClient(item);
|
return super.handleItemToClient(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.getTag() == null) {
|
|
||||||
item.setTag(new CompoundTag(""));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Backup data for toServer
|
|
||||||
short originalData = item.getData();
|
short originalData = item.getData();
|
||||||
item.getTag().put(createViaNBT(item));
|
|
||||||
|
|
||||||
item.setIdentifier(data.getId());
|
item.setIdentifier(data.getId());
|
||||||
// Keep original data if mapped data is set to -1
|
// Keep original data if mapped data is set to -1
|
||||||
if (data.getData() != -1) {
|
if (data.getData() != -1) {
|
||||||
@ -114,19 +108,25 @@ public abstract class LegacyBlockItemRewriter<T extends BackwardsProtocol> exten
|
|||||||
|
|
||||||
// Set display name
|
// Set display name
|
||||||
if (data.getName() != null) {
|
if (data.getName() != null) {
|
||||||
CompoundTag tag = item.getTag().get("display");
|
if (item.getTag() == null) {
|
||||||
if (tag == null) {
|
item.setTag(new CompoundTag(""));
|
||||||
item.getTag().put(tag = new CompoundTag("display"));
|
|
||||||
}
|
}
|
||||||
StringTag nameTag = tag.get("Name");
|
|
||||||
|
CompoundTag display = item.getTag().get("display");
|
||||||
|
if (display == null) {
|
||||||
|
item.getTag().put(display = new CompoundTag("display"));
|
||||||
|
}
|
||||||
|
|
||||||
|
StringTag nameTag = display.get("Name");
|
||||||
if (nameTag == null) {
|
if (nameTag == null) {
|
||||||
tag.put(nameTag = new StringTag("Name", data.getName()));
|
display.put(nameTag = new StringTag("Name", data.getName()));
|
||||||
|
display.put(new ByteTag(nbtTagName + "|customName"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle colors
|
// Handle colors
|
||||||
String value = nameTag.getValue();
|
String value = nameTag.getValue();
|
||||||
if (value.contains("%vb_color%")) {
|
if (value.contains("%vb_color%")) {
|
||||||
tag.put(new StringTag("Name", value.replace("%vb_color%", BlockColors.get(originalData))));
|
display.put(new StringTag("Name", value.replace("%vb_color%", BlockColors.get(originalData))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return item;
|
return item;
|
||||||
|
@ -592,8 +592,8 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
|
|||||||
if (display != null) {
|
if (display != null) {
|
||||||
StringTag name = display.get("Name");
|
StringTag name = display.get("Name");
|
||||||
if (name instanceof StringTag) {
|
if (name instanceof StringTag) {
|
||||||
StringTag via = display.remove(extraNbtTag + "|Name");
|
display.put(new StringTag(extraNbtTag + "|Name", name.getValue()));
|
||||||
name.setValue(via != null ? via.getValue() : ChatRewriter.jsonTextToLegacy(name.getValue()));
|
name.setValue(ChatRewriter.jsonTextToLegacy(name.getValue()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -788,8 +788,8 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
|
|||||||
CompoundTag displayTag = (CompoundTag) display;
|
CompoundTag displayTag = (CompoundTag) display;
|
||||||
StringTag name = displayTag.get("Name");
|
StringTag name = displayTag.get("Name");
|
||||||
if (name instanceof StringTag) {
|
if (name instanceof StringTag) {
|
||||||
displayTag.put(new StringTag(extraNbtTag + "|Name", name.getValue()));
|
StringTag via = displayTag.remove(extraNbtTag + "|Name");
|
||||||
name.setValue(ChatRewriter.legacyTextToJson(name.getValue()).toString());
|
name.setValue(via != null ? via.getValue() : ChatRewriter.legacyTextToJson(name.getValue()).toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user