Mirror von
https://github.com/ViaVersion/ViaBackwards.git
synchronisiert 2024-09-07 13:22:54 +02: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.
Dieser Commit ist enthalten in:
Ursprung
9f9d05d3c6
Commit
b2b8c48fd0
@ -5,6 +5,7 @@ import nl.matsv.viabackwards.api.data.MappedItem;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import us.myles.ViaVersion.api.minecraft.item.Item;
|
||||
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.ListTag;
|
||||
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) {
|
||||
if (item == null) return null;
|
||||
|
||||
CompoundTag tag = null;
|
||||
boolean textChanged = false;
|
||||
CompoundTag display = 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
|
||||
StringTag name = tag.get("Name");
|
||||
StringTag name = display.get("Name");
|
||||
if (name != null) {
|
||||
String newValue = translatableRewriter.processText(name.getValue()).toString();
|
||||
if (name.getValue().equals(newValue)) {
|
||||
textChanged = true;
|
||||
if (!newValue.equals(name.getValue())) {
|
||||
saveNameTag(display, name);
|
||||
}
|
||||
|
||||
name.setValue(newValue);
|
||||
}
|
||||
|
||||
ListTag lore = tag.get("Lore");
|
||||
ListTag lore = display.get("Lore");
|
||||
if (lore != null) {
|
||||
ListTag original = null;
|
||||
boolean changed = false;
|
||||
for (Tag loreEntry : lore) {
|
||||
if (!(loreEntry instanceof StringTag)) continue;
|
||||
|
||||
StringTag stringTag = (StringTag) loreEntry;
|
||||
String newValue = translatableRewriter.processText(stringTag.getValue()).toString();
|
||||
if (stringTag.getValue().equals(newValue)) {
|
||||
textChanged = true;
|
||||
}
|
||||
stringTag.setValue(newValue);
|
||||
}
|
||||
}
|
||||
if (!changed && !newValue.equals(name.getValue())) {
|
||||
changed = true;
|
||||
original = lore.clone();
|
||||
}
|
||||
|
||||
if (textChanged) {
|
||||
// Backup data for toServer
|
||||
item.getTag().put(createViaNBT(item));
|
||||
stringTag.setValue(newValue);
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
saveLoreTag(display, original);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MappedItem data = mappedItemFunction.get(item.getIdentifier());
|
||||
@ -73,23 +77,19 @@ public abstract class ItemRewriter<T extends BackwardsProtocol> extends ItemRewr
|
||||
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
|
||||
item.setIdentifier(data.getId());
|
||||
|
||||
// Set custom name - only done if there is no original one
|
||||
if (tag == null) {
|
||||
item.getTag().put(tag = new CompoundTag("display"));
|
||||
if (item.getTag() == null) {
|
||||
item.setTag(new CompoundTag(""));
|
||||
}
|
||||
if (!tag.contains("Name")) {
|
||||
tag.put(new StringTag("Name", data.getJsonName()));
|
||||
if (display == null) {
|
||||
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;
|
||||
}
|
||||
|
@ -5,10 +5,9 @@ import org.jetbrains.annotations.Nullable;
|
||||
import us.myles.ViaVersion.api.minecraft.item.Item;
|
||||
import us.myles.ViaVersion.api.rewriters.IdRewriteFunction;
|
||||
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.ShortTag;
|
||||
import us.myles.viaversion.libs.opennbt.tag.builtin.Tag;
|
||||
import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag;
|
||||
import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag;
|
||||
|
||||
public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewriter<T> {
|
||||
|
||||
@ -42,53 +41,42 @@ public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewr
|
||||
@Nullable
|
||||
public Item handleItemToServer(Item item) {
|
||||
if (item == null) return null;
|
||||
|
||||
CompoundTag tag = item.getTag();
|
||||
if (tag == null) {
|
||||
if (toServerRewriter != null) {
|
||||
item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier()));
|
||||
}
|
||||
restoreDisplayTag(item);
|
||||
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)));
|
||||
protected void saveNameTag(CompoundTag displayTag, StringTag original) {
|
||||
displayTag.put(new StringTag(nbtTagName + "|o" + original.getName(), original.getValue()));
|
||||
}
|
||||
|
||||
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 {
|
||||
// Rewrite id normally
|
||||
if (toServerRewriter != null) {
|
||||
item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier()));
|
||||
}
|
||||
}
|
||||
return item;
|
||||
restoreDisplayTag(display, "Name");
|
||||
}
|
||||
|
||||
protected CompoundTag createViaNBT(Item item) {
|
||||
CompoundTag tag = new CompoundTag(nbtTagName);
|
||||
tag.put(new ShortTag("id", (short) item.getIdentifier()));
|
||||
if (item.getAmount() != 1) {
|
||||
tag.put(new ByteTag("amount", item.getAmount()));
|
||||
// Restore lore
|
||||
restoreDisplayTag(display, "Lore");
|
||||
}
|
||||
if (item.getData() != 0) {
|
||||
tag.put(new ShortTag("data", item.getData()));
|
||||
}
|
||||
if (item.getTag() != null) {
|
||||
tag.put(CONVERTER.convert("extras", CONVERTER.convert(item.getTag())));
|
||||
|
||||
private void restoreDisplayTag(CompoundTag displayTag, String tagName) {
|
||||
StringTag original = displayTag.remove(nbtTagName + "|o" + tagName);
|
||||
if (original != null) {
|
||||
displayTag.put(original);
|
||||
}
|
||||
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.JsonObject;
|
||||
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.IntTag;
|
||||
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);
|
||||
}
|
||||
|
||||
if (item.getTag() == null) {
|
||||
item.setTag(new CompoundTag(""));
|
||||
}
|
||||
|
||||
// Backup data for toServer
|
||||
short originalData = item.getData();
|
||||
item.getTag().put(createViaNBT(item));
|
||||
|
||||
item.setIdentifier(data.getId());
|
||||
// Keep original data if mapped data is set to -1
|
||||
if (data.getData() != -1) {
|
||||
@ -114,19 +108,25 @@ public abstract class LegacyBlockItemRewriter<T extends BackwardsProtocol> exten
|
||||
|
||||
// Set display name
|
||||
if (data.getName() != null) {
|
||||
CompoundTag tag = item.getTag().get("display");
|
||||
if (tag == null) {
|
||||
item.getTag().put(tag = new CompoundTag("display"));
|
||||
if (item.getTag() == null) {
|
||||
item.setTag(new CompoundTag(""));
|
||||
}
|
||||
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) {
|
||||
tag.put(nameTag = new StringTag("Name", data.getName()));
|
||||
display.put(nameTag = new StringTag("Name", data.getName()));
|
||||
display.put(new ByteTag(nbtTagName + "|customName"));
|
||||
}
|
||||
|
||||
// Handle colors
|
||||
String value = nameTag.getValue();
|
||||
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;
|
||||
|
@ -592,8 +592,8 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
|
||||
if (display != null) {
|
||||
StringTag name = display.get("Name");
|
||||
if (name instanceof StringTag) {
|
||||
StringTag via = display.remove(extraNbtTag + "|Name");
|
||||
name.setValue(via != null ? via.getValue() : ChatRewriter.jsonTextToLegacy(name.getValue()));
|
||||
display.put(new StringTag(extraNbtTag + "|Name", 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;
|
||||
StringTag name = displayTag.get("Name");
|
||||
if (name instanceof StringTag) {
|
||||
displayTag.put(new StringTag(extraNbtTag + "|Name", name.getValue()));
|
||||
name.setValue(ChatRewriter.legacyTextToJson(name.getValue()).toString());
|
||||
StringTag via = displayTag.remove(extraNbtTag + "|Name");
|
||||
name.setValue(via != null ? via.getValue() : ChatRewriter.legacyTextToJson(name.getValue()).toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren