Improved chat item rewriting

This commit is contained in:
RaphiMC 2024-04-01 00:57:59 +02:00
parent d4d448b3e5
commit d980812b2a
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94

View File

@ -23,9 +23,9 @@ import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
import com.viaversion.viaversion.libs.gson.JsonArray; import com.viaversion.viaversion.libs.gson.JsonArray;
import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
import com.viaversion.viaversion.libs.mcstructs.snbt.SNbtSerializer; import com.viaversion.viaversion.libs.mcstructs.snbt.SNbtSerializer;
import com.viaversion.viaversion.libs.mcstructs.snbt.exceptions.SNbtSerializeException; import com.viaversion.viaversion.libs.mcstructs.text.ATextComponent;
import com.viaversion.viaversion.libs.mcstructs.text.serializer.TextComponentSerializer;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
@ -371,74 +371,46 @@ public class ChatItemRewriter {
final JsonElement value = hoverEvent.get("value"); final JsonElement value = hoverEvent.get("value");
if (value == null) return; if (value == null) return;
final String text = findItemNBT(value); final ATextComponent nbt = TextComponentSerializer.V1_7.deserialize(value);
if (text == null) return;
final CompoundTag tag;
try { try {
tag = (CompoundTag) SNbtSerializer.V1_7.deserialize(text); final CompoundTag tag = (CompoundTag) SNbtSerializer.V1_7.deserialize(nbt.asUnformattedString());
} catch (Throwable e) { final CompoundTag itemTag = tag.get("tag");
ViaLegacy.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text); final ShortTag idTag = tag.get("id");
throw new RuntimeException(e); final ShortTag damageTag = tag.get("Damage");
}
final CompoundTag itemTag = tag.get("tag"); // Call item converter
final ShortTag idTag = tag.get("id"); final short damage = damageTag != null ? damageTag.asShort() : 0;
final ShortTag damageTag = tag.get("Damage"); final short id = idTag != null ? idTag.asShort() : 1;
final Item item = new DataItem();
item.setIdentifier(id);
item.setData(damage);
item.setTag(itemTag);
this.handleItem(item);
// Call item converter // Serialize again
final short damage = damageTag != null ? damageTag.asShort() : 0; if (damage != item.data()) {
final short id = idTag != null ? idTag.asShort() : 1; tag.put("Damage", new ShortTag(item.data()));
final Item item = new DataItem(); }
item.setIdentifier(id); tag.put("id", new StringTag("minecraft:" + ID_TO_NAME.getOrDefault(item.identifier(), "stone")));
item.setData(damage); if (item.tag() != null) {
item.setTag(itemTag); tag.put("tag", new CompoundTag(item.tag().getValue()));
handleItem(item); }
// Serialize again final JsonArray array = new JsonArray();
if (damage != item.data()) { final JsonObject object = new JsonObject();
tag.put("Damage", new ShortTag(item.data())); array.add(object);
} final String serializedNBT = SNbtSerializer.V1_8.serialize(tag);
tag.put("id", new StringTag("minecraft:" + ID_TO_NAME.getOrDefault(item.identifier(), "stone")));
if (item.tag() != null) {
tag.put("tag", new CompoundTag(item.tag().getValue()));
}
final JsonArray array = new JsonArray();
final JsonObject object = new JsonObject();
array.add(object);
final String serializedNBT;
try {
serializedNBT = SNbtSerializer.V1_8.serialize(tag);
object.addProperty("text", serializedNBT); object.addProperty("text", serializedNBT);
hoverEvent.add("value", array); hoverEvent.add("value", array);
} catch (SNbtSerializeException e) { } catch (Throwable e) {
ViaLegacy.getPlatform().getLogger().log(Level.WARNING, "Error writing NBT in show_item:" + text, e); ViaLegacy.getPlatform().getLogger().log(Level.WARNING, "Error remapping NBT in show_item:" + nbt.asUnformattedString(), e);
} }
} }
private void handleItem(Item item) { private void handleItem(Item item) {
this.protocol.getItemRewriter().handleItemToClient(item); this.protocol.getItemRewriter().handleItemToClient(item);
} }
private String findItemNBT(JsonElement element) {
if (element.isJsonArray()) {
for (JsonElement jsonElement : element.getAsJsonArray()) {
String value = findItemNBT(jsonElement);
if (value != null) {
return value;
}
}
} else if (element.isJsonObject()) {
final JsonPrimitive text = element.getAsJsonObject().getAsJsonPrimitive("text");
if (text != null) {
return text.getAsString();
}
} else if (element.isJsonPrimitive()) {
return element.getAsJsonPrimitive().getAsString();
}
return null;
}
}; };
} }