3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 09:00:09 +01:00

Handle lore in show_item in 1.13->1.14

Also print out nbt if reading/writing fails
Dieser Commit ist enthalten in:
KennyTV 2020-06-28 22:19:59 +02:00
Ursprung 26b90b6d71
Commit a0195c59cc
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
5 geänderte Dateien mit 155 neuen und 87 gelöschten Zeilen

Datei anzeigen

@ -44,6 +44,15 @@ public class ComponentRewriter {
this.protocol = null; this.protocol = null;
} }
public void registerChatMessage(ClientboundPacketType packetType) {
protocol.registerOutgoing(packetType, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT)));
}
});
}
public void registerBossBar(ClientboundPacketType packetType) { public void registerBossBar(ClientboundPacketType packetType) {
protocol.registerOutgoing(packetType, new PacketRemapper() { protocol.registerOutgoing(packetType, new PacketRemapper() {
@Override @Override

Datei anzeigen

@ -1,25 +1,15 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2; package us.myles.ViaVersion.protocols.protocol1_13to1_12_2;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ShortTag;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.chat.ComponentSerializer;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.nbt.BinaryTagIO;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter; import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ComponentRewriter1_13;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.GsonUtil;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -28,82 +18,7 @@ import java.util.regex.Pattern;
public class ChatRewriter { public class ChatRewriter {
private static final Pattern URL = Pattern.compile("^(?:(https?)://)?([-\\w_.]{2,}\\.[a-z]{2,4})(/\\S*)?$"); private static final Pattern URL = Pattern.compile("^(?:(https?)://)?([-\\w_.]{2,}\\.[a-z]{2,4})(/\\S*)?$");
private static final BaseComponent[] EMPTY_COMPONENTS = new BaseComponent[0]; private static final BaseComponent[] EMPTY_COMPONENTS = new BaseComponent[0];
private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() { private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter1_13();
@Override
protected void handleHoverEvent(JsonObject hoverEvent) {
super.handleHoverEvent(hoverEvent);
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (!action.equals("show_item")) return;
JsonElement value = hoverEvent.get("value");
if (value == null) return;
String text = findItemNBT(value);
if (text == null) return;
try {
CompoundTag tag = BinaryTagIO.readString(text);
CompoundTag itemTag = tag.get("tag");
ShortTag damageTag = tag.get("Damage");
// Call item converter
short damage = damageTag != null ? damageTag.getValue() : 0;
Item item = new Item();
item.setData(damage);
item.setTag(itemTag);
InventoryPackets.toClient(item);
// Serialize again
if (damage != item.getData()) {
tag.put(new ShortTag("Damage", item.getData()));
}
if (itemTag != null) {
tag.put(itemTag);
}
JsonArray array = new JsonArray();
JsonObject object = new JsonObject();
array.add(object);
String serializedNBT = BinaryTagIO.writeString(tag);
object.addProperty("text", serializedNBT);
hoverEvent.add("value", array);
} catch (IOException e) {
Via.getPlatform().getLogger().warning("Invalid NBT in show_item:");
e.printStackTrace();
}
}
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()) {
JsonPrimitive text = element.getAsJsonObject().getAsJsonPrimitive("text");
if (text != null) {
return text.getAsString();
}
} else if (element.isJsonPrimitive()) {
return element.getAsJsonPrimitive().getAsString();
}
return null;
}
@Override
protected void handleTranslate(JsonObject object, String translate) {
super.handleTranslate(object, translate);
String newTranslate;
newTranslate = MappingData.translateMapping.get(translate);
if (newTranslate == null) {
newTranslate = MappingData.mojangTranslation.get(translate);
}
if (newTranslate != null) {
object.addProperty("translate", newTranslate);
}
}
};
// Based on https://github.com/SpigotMC/BungeeCord/blob/master/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java // Based on https://github.com/SpigotMC/BungeeCord/blob/master/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java
public static JsonElement fromLegacyText(String message, ChatColor defaultColor) { public static JsonElement fromLegacyText(String message, ChatColor defaultColor) {

Datei anzeigen

@ -0,0 +1,115 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ShortTag;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.nbt.BinaryTagIO;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
import java.io.IOException;
public class ComponentRewriter1_13 extends ComponentRewriter {
public ComponentRewriter1_13(Protocol protocol) {
super(protocol);
}
public ComponentRewriter1_13() {
}
@Override
protected void handleHoverEvent(JsonObject hoverEvent) {
super.handleHoverEvent(hoverEvent);
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (!action.equals("show_item")) return;
JsonElement value = hoverEvent.get("value");
if (value == null) return;
String text = findItemNBT(value);
if (text == null) return;
CompoundTag tag;
try {
tag = BinaryTagIO.readString(text);
} catch (IOException e) {
Via.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text);
e.printStackTrace();
return;
}
CompoundTag itemTag = tag.get("tag");
ShortTag damageTag = tag.get("Damage");
// Call item converter
short damage = damageTag != null ? damageTag.getValue() : 0;
Item item = new Item();
item.setData(damage);
item.setTag(itemTag);
handleItem(item);
// Serialize again
if (damage != item.getData()) {
tag.put(new ShortTag("Damage", item.getData()));
}
if (itemTag != null) {
tag.put(itemTag);
}
JsonArray array = new JsonArray();
JsonObject object = new JsonObject();
array.add(object);
String serializedNBT;
try {
serializedNBT = BinaryTagIO.writeString(tag);
object.addProperty("text", serializedNBT);
hoverEvent.add("value", array);
} catch (IOException e) {
Via.getPlatform().getLogger().warning("Error writing NBT in show_item:" + text);
e.printStackTrace();
}
}
protected void handleItem(Item item) {
InventoryPackets.toClient(item);
}
protected 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()) {
JsonPrimitive text = element.getAsJsonObject().getAsJsonPrimitive("text");
if (text != null) {
return text.getAsString();
}
} else if (element.isJsonPrimitive()) {
return element.getAsJsonPrimitive().getAsString();
}
return null;
}
@Override
protected void handleTranslate(JsonObject object, String translate) {
super.handleTranslate(object, translate);
String newTranslate;
newTranslate = MappingData.translateMapping.get(translate);
if (newTranslate == null) {
newTranslate = MappingData.mojangTranslation.get(translate);
}
if (newTranslate != null) {
object.addProperty("translate", newTranslate);
}
}
}

Datei anzeigen

@ -6,10 +6,12 @@ import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.rewriters.SoundRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.ComponentRewriter1_14;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.EntityPackets;
@ -36,6 +38,9 @@ public class Protocol1_14To1_13_2 extends Protocol<ClientboundPackets1_13, Clien
new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)).registerSound(ClientboundPackets1_13.SOUND); new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)).registerSound(ClientboundPackets1_13.SOUND);
ComponentRewriter componentRewriter = new ComponentRewriter1_14(this);
componentRewriter.registerChatMessage(ClientboundPackets1_13.CHAT_MESSAGE);
registerOutgoing(ClientboundPackets1_13.ADVANCEMENTS, new PacketRemapper() { registerOutgoing(ClientboundPackets1_13.ADVANCEMENTS, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {

Datei anzeigen

@ -0,0 +1,24 @@
package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data;
import com.google.gson.JsonObject;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ComponentRewriter1_13;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.InventoryPackets;
public class ComponentRewriter1_14 extends ComponentRewriter1_13 {
public ComponentRewriter1_14(Protocol protocol) {
super(protocol);
}
@Override
protected void handleItem(Item item) {
InventoryPackets.toClient(item);
}
@Override
protected void handleTranslate(JsonObject object, String translate) {
// Nothing
}
}