3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Merge pull request #1840 from KennyTV/component-reading

Directly read components as json, to only de-/serialize once
Dieser Commit ist enthalten in:
Nassim 2020-06-27 17:42:25 +02:00 committet von GitHub
Commit a942d3aad5
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
28 geänderte Dateien mit 308 neuen und 267 gelöschten Zeilen

Datei anzeigen

@ -9,8 +9,8 @@ public enum MetaType1_13 implements MetaType {
VarInt(1, Type.VAR_INT),
Float(2, Type.FLOAT),
String(3, Type.STRING),
Chat(4, Type.STRING),
OptChat(5, Type.OPTIONAL_CHAT),
Chat(4, Type.COMPONENT),
OptChat(5, Type.OPTIONAL_COMPONENT),
Slot(6, Type.FLAT_ITEM),
Boolean(7, Type.BOOLEAN),
Vector3F(8, Type.ROTATION),

Datei anzeigen

@ -9,8 +9,8 @@ public enum MetaType1_13_2 implements MetaType {
VarInt(1, Type.VAR_INT),
Float(2, Type.FLOAT),
String(3, Type.STRING),
Chat(4, Type.STRING),
OptChat(5, Type.OPTIONAL_CHAT),
Chat(4, Type.COMPONENT),
OptChat(5, Type.OPTIONAL_COMPONENT),
Slot(6, Type.FLAT_VAR_INT_ITEM),
Boolean(7, Type.BOOLEAN),
Vector3F(8, Type.ROTATION),

Datei anzeigen

@ -9,8 +9,8 @@ public enum MetaType1_14 implements MetaType {
VarInt(1, Type.VAR_INT),
Float(2, Type.FLOAT),
String(3, Type.STRING),
Chat(4, Type.STRING),
OptChat(5, Type.OPTIONAL_CHAT),
Chat(4, Type.COMPONENT),
OptChat(5, Type.OPTIONAL_COMPONENT),
Slot(6, Type.FLAT_VAR_INT_ITEM),
Boolean(7, Type.BOOLEAN),
Vector3F(8, Type.ROTATION),

Datei anzeigen

@ -0,0 +1,97 @@
package us.myles.ViaVersion.api.rewriters;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import us.myles.ViaVersion.util.GsonUtil;
// Packets using components:
// ping (status)
// disconnect (play and login)
// chat
// bossbar
// open window
// combat event
// title
// tablist
// teams
// scoreboard
// player info
// map data
// declare commands
// advancements
// update sign
public class ComponentRewriter {
public JsonElement processText(String value) {
JsonElement root = GsonUtil.getJsonParser().parse(value);
processText(root);
return root;
}
public void processText(JsonElement element) {
if (element == null || element.isJsonNull()) return;
if (element.isJsonArray()) {
processAsArray(element);
return;
}
if (element.isJsonPrimitive()) {
handleText(element.getAsJsonPrimitive());
return;
}
JsonObject object = element.getAsJsonObject();
JsonPrimitive text = object.getAsJsonPrimitive("text");
if (text != null) {
handleText(text);
}
JsonElement translate = object.get("translate");
if (translate != null) {
handleTranslate(object, translate.getAsString());
JsonElement with = object.get("with");
if (with != null) {
processAsArray(with);
}
}
JsonElement extra = object.get("extra");
if (extra != null) {
processAsArray(extra);
}
JsonObject hoverEvent = object.getAsJsonObject("hoverEvent");
if (hoverEvent != null) {
handleHoverEvent(hoverEvent);
}
}
protected void handleText(JsonPrimitive text) {
// To override if needed
}
protected void handleTranslate(JsonObject object, String translate) {
// To override if needed
}
// To override if needed (don't forget to call super if needed)
protected void handleHoverEvent(JsonObject hoverEvent) {
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (action.equals("show_text")) {
JsonElement value = hoverEvent.get("value");
processText(value != null ? value : hoverEvent.get("contents"));
} else if (action.equals("show_entity")) {
JsonObject contents = hoverEvent.getAsJsonObject("contents");
if (contents != null) {
processText(contents.get("name"));
}
}
}
private void processAsArray(JsonElement element) {
for (JsonElement jsonElement : element.getAsJsonArray()) {
processText(jsonElement);
}
}
}

Datei anzeigen

@ -2,6 +2,7 @@ package us.myles.ViaVersion.api.type;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.google.gson.JsonElement;
import us.myles.ViaVersion.api.minecraft.*;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.type.types.*;
@ -77,7 +78,7 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
@Deprecated
public static final Type<Integer[]> UNSIGNED_SHORT_ARRAY = new ArrayType<>(Type.UNSIGNED_SHORT);
/* Other Types */
public static final Type<String> COMPONENT_STRING = new ComponentStringType();
public static final Type<JsonElement> COMPONENT = new ComponentType();
public static final Type<String> STRING = new StringType();
public static final Type<String[]> STRING_ARRAY = new ArrayType<>(Type.STRING);
@ -110,7 +111,7 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
public static final Type<CompoundTag[]> NBT_ARRAY = new ArrayType<>(Type.NBT);
public static final Type<UUID> OPTIONAL_UUID = new OptUUIDType();
public static final Type<String> OPTIONAL_CHAT = new OptionalChatType();
public static final Type<JsonElement> OPTIONAL_COMPONENT = new OptionalComponentType();
public static final Type<Position> OPTIONAL_POSITION = new OptPositionType();
public static final Type<Position> OPTIONAL_POSITION_1_14 = new OptPosition1_14Type();

Datei anzeigen

@ -1,15 +0,0 @@
package us.myles.ViaVersion.api.type.types;
import us.myles.ViaVersion.api.type.Type;
public class ComponentStringType extends StringType {
public ComponentStringType() {
super(262144);
}
@Override
public Class<? extends Type> getBaseClass() {
return StringType.class;
}
}

Datei anzeigen

@ -0,0 +1,24 @@
package us.myles.ViaVersion.api.type.types;
import com.google.gson.JsonElement;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.util.GsonUtil;
public class ComponentType extends Type<JsonElement> {
private static final StringType STRING_TAG = new StringType(262144);
public ComponentType() {
super(JsonElement.class);
}
@Override
public JsonElement read(ByteBuf buffer) throws Exception {
return GsonUtil.getJsonParser().parse(STRING_TAG.read(buffer));
}
@Override
public void write(ByteBuf buffer, JsonElement object) throws Exception {
STRING_TAG.write(buffer, object.toString());
}
}

Datei anzeigen

@ -1,27 +0,0 @@
package us.myles.ViaVersion.api.type.types.minecraft;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.type.Type;
public class OptionalChatType extends Type<String> {
public OptionalChatType() {
super(String.class);
}
@Override
public String read(ByteBuf buffer) throws Exception {
boolean present = buffer.readBoolean();
if (!present) return null;
return Type.STRING.read(buffer);
}
@Override
public void write(ByteBuf buffer, String object) throws Exception {
if (object == null) {
buffer.writeBoolean(false);
} else {
buffer.writeBoolean(true);
Type.STRING.write(buffer, object);
}
}
}

Datei anzeigen

@ -0,0 +1,28 @@
package us.myles.ViaVersion.api.type.types.minecraft;
import com.google.gson.JsonElement;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.type.Type;
public class OptionalComponentType extends Type<JsonElement> {
public OptionalComponentType() {
super(JsonElement.class);
}
@Override
public JsonElement read(ByteBuf buffer) throws Exception {
boolean present = buffer.readBoolean();
return present ? Type.COMPONENT.read(buffer) : null;
}
@Override
public void write(ByteBuf buffer, JsonElement object) throws Exception {
if (object == null) {
buffer.writeBoolean(false);
} else {
buffer.writeBoolean(true);
Type.COMPONENT.write(buffer, object);
}
}
}

Datei anzeigen

@ -150,12 +150,11 @@ public class Protocol1_11To1_10 extends Protocol<ClientboundPackets1_9_3, Client
int action = wrapper.get(Type.VAR_INT, 0);
// Handle the new ActionBar
if (action >= 2)
if (action >= 2) {
wrapper.set(Type.VAR_INT, 0, action + 1);
}
}
});
}
});

Datei anzeigen

@ -16,16 +16,15 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.SoundRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_12;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.metadata.MetadataRewriter1_12To1_11_1;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.packets.InventoryPackets;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.EntityTracker1_12;
import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import us.myles.ViaVersion.util.GsonUtil;
public class Protocol1_12To1_11_1 extends Protocol<ClientboundPackets1_9_3, ClientboundPackets1_12, ServerboundPackets1_9_3, ServerboundPackets1_12> {
@ -76,21 +75,18 @@ public class Protocol1_12To1_11_1 extends Protocol<ClientboundPackets1_9_3, Clie
registerOutgoing(ClientboundPackets1_9_3.CHAT_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 0 - Chat Message (json)
map(Type.BYTE); // 1 - Chat Positon
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
if (!Via.getConfig().is1_12NBTArrayFix()) return;
try {
JsonElement obj = GsonUtil.getJsonParser().parse(wrapper.get(Type.STRING, 0));
JsonElement obj = Protocol1_9To1_8.FIX_JSON.transform(null, wrapper.passthrough(Type.COMPONENT).toString());
if (!TranslateRewriter.toClient(obj, wrapper.user())) {
wrapper.cancel();
return;
}
ChatItemRewriter.toClient(obj, wrapper.user());
wrapper.set(Type.STRING, 0, obj.toString());
} catch (Exception e) {
e.printStackTrace();
}

Datei anzeigen

@ -105,7 +105,7 @@ public class Protocol1_13_1To1_13 extends Protocol<ClientboundPackets1_13, Clien
public void handle(PacketWrapper wrapper) throws Exception {
int action = wrapper.get(Type.VAR_INT, 0);
if (action == 0) {
wrapper.passthrough(Type.STRING);
wrapper.passthrough(Type.COMPONENT);
wrapper.passthrough(Type.FLOAT);
wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.VAR_INT);

Datei anzeigen

@ -1,27 +1,45 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData;
import us.myles.ViaVersion.util.GsonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChatRewriter {
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 ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() {
@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
private static final Pattern url = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$");
public static BaseComponent[] fromLegacyText(String message, ChatColor defaultColor) {
ArrayList<BaseComponent> components = new ArrayList<>();
public static JsonElement fromLegacyText(String message, ChatColor defaultColor) {
List<BaseComponent> components = new ArrayList<>();
StringBuilder builder = new StringBuilder();
TextComponent component = new TextComponent();
Matcher matcher = url.matcher(message);
Matcher matcher = URL.matcher(message);
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
@ -110,56 +128,19 @@ public class ChatRewriter {
component.setText(builder.toString());
components.add(component);
return components.toArray(new BaseComponent[0]);
final String serializedComponents = ComponentSerializer.toString(components.toArray(EMPTY_COMPONENTS));
return GsonUtil.getJsonParser().parse(serializedComponents);
}
public static String legacyTextToJson(String legacyText) {
return ComponentSerializer.toString(fromLegacyText(legacyText, ChatColor.WHITE));
public static JsonElement legacyTextToJson(String legacyText) {
return fromLegacyText(legacyText, ChatColor.WHITE);
}
public static String jsonTextToLegacy(String value) {
return TextComponent.toLegacyText(ComponentSerializer.parse(value));
}
public static String processTranslate(String value) {
BaseComponent[] components = ComponentSerializer.parse(value);
for (BaseComponent component : components) {
processTranslate(component);
}
if (components.length == 1) {
return ComponentSerializer.toString(components[0]);
} else {
return ComponentSerializer.toString(components);
}
}
private static void processTranslate(BaseComponent component) {
if (component == null) return;
if (component instanceof TranslatableComponent) {
String oldTranslate = ((TranslatableComponent) component).getTranslate();
String newTranslate;
newTranslate = MappingData.translateMapping.get(oldTranslate);
if (newTranslate == null) {
newTranslate = MappingData.mojangTranslation.get(oldTranslate);
}
if (newTranslate != null) {
((TranslatableComponent) component).setTranslate(newTranslate);
}
if (((TranslatableComponent) component).getWith() != null) {
for (BaseComponent baseComponent : ((TranslatableComponent) component).getWith()) {
processTranslate(baseComponent);
}
}
}
if (component.getHoverEvent() != null) {
for (BaseComponent baseComponent : component.getHoverEvent().getValue()) {
processTranslate(baseComponent);
}
}
if (component.getExtra() != null) {
for (BaseComponent baseComponent : component.getExtra()) {
processTranslate(baseComponent);
}
}
public static void processTranslate(JsonElement value) {
COMPONENT_REWRITER.processText(value);
}
}

Datei anzeigen

@ -154,13 +154,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
registerOutgoing(State.LOGIN, 0x0, 0x0, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(Type.STRING, 0, ChatRewriter.processTranslate(wrapper.get(Type.STRING, 0)));
}
});
handler(wrapper -> ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)));
}
});
@ -258,7 +252,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
public void handle(PacketWrapper wrapper) throws Exception {
int action = wrapper.get(Type.VAR_INT, 0);
if (action == 0 || action == 3) {
wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING)));
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT));
}
}
});
@ -267,13 +261,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
registerOutgoing(ClientboundPackets1_12_1.CHAT_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(Type.STRING, 0, ChatRewriter.processTranslate(wrapper.get(Type.STRING, 0)));
}
});
handler(wrapper -> ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)));
}
});
registerOutgoing(ClientboundPackets1_12_1.TAB_COMPLETE, new PacketRemapper() {
@ -322,8 +310,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
public void registerMap() {
map(Type.UNSIGNED_BYTE); // Id
map(Type.STRING); // Window type
map(Type.STRING); // Title
handler(wrapper -> wrapper.set(Type.STRING, 1, ChatRewriter.processTranslate(wrapper.get(Type.STRING, 1))));
handler(wrapper -> ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT))); // Title
}
});
@ -369,8 +356,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
registerOutgoing(ClientboundPackets1_12_1.DISCONNECT, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING);
handler(wrapper -> wrapper.set(Type.STRING, 0, ChatRewriter.processTranslate(wrapper.get(Type.STRING, 0))));
handler(wrapper -> ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)));
}
});
@ -420,30 +406,6 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
}
});
registerOutgoing(ClientboundPackets1_12_1.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Map id
map(Type.BYTE); // 1 - Scale
map(Type.BOOLEAN); // 2 - Tracking Position
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int iconCount = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < iconCount; i++) {
byte directionAndType = wrapper.read(Type.BYTE);
int type = (directionAndType & 0xF0) >> 4;
wrapper.write(Type.VAR_INT, type);
wrapper.passthrough(Type.BYTE); // Icon X
wrapper.passthrough(Type.BYTE); // Icon Z
byte direction = (byte) (directionAndType & 0x0F);
wrapper.write(Type.BYTE, direction);
wrapper.write(Type.OPTIONAL_CHAT, null); // Display Name
}
}
});
}
});
registerOutgoing(ClientboundPackets1_12_1.CRAFT_RECIPE_RESPONSE, new PacketRemapper() {
@Override
@ -462,7 +424,31 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
if (wrapper.get(Type.VAR_INT, 0) == 2) { // Entity dead
wrapper.passthrough(Type.VAR_INT); // Player id
wrapper.passthrough(Type.INT); // Entity id
wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING)));
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT));
}
}
});
}
});
registerOutgoing(ClientboundPackets1_12_1.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Map id
map(Type.BYTE); // 1 - Scale
map(Type.BOOLEAN); // 2 - Tracking Position
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int iconCount = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < iconCount; i++) {
byte directionAndType = wrapper.read(Type.BYTE);
int type = (directionAndType & 0xF0) >> 4;
wrapper.write(Type.VAR_INT, type);
wrapper.passthrough(Type.BYTE); // Icon X
wrapper.passthrough(Type.BYTE); // Icon Z
byte direction = (byte) (directionAndType & 0x0F);
wrapper.write(Type.BYTE, direction);
wrapper.write(Type.OPTIONAL_COMPONENT, null); // Display Name
}
}
});
@ -586,8 +572,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
// On create or update
if (mode == 0 || mode == 2) {
String value = wrapper.read(Type.STRING); // Value
value = ChatRewriter.legacyTextToJson(value);
wrapper.write(Type.STRING, value);
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(value));
String type = wrapper.read(Type.STRING);
// integer or hearts
@ -611,8 +596,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
if (action == 0 || action == 2) {
String displayName = wrapper.read(Type.STRING); // Display Name
displayName = ChatRewriter.legacyTextToJson(displayName);
wrapper.write(Type.STRING, displayName);
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(displayName));
String prefix = wrapper.read(Type.STRING); // Prefix moved
String suffix = wrapper.read(Type.STRING); // Suffix moved
@ -635,8 +619,8 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
wrapper.write(Type.VAR_INT, colour);
wrapper.write(Type.STRING, ChatRewriter.legacyTextToJson(prefix)); // Prefix
wrapper.write(Type.STRING, ChatRewriter.legacyTextToJson(suffix)); // Suffix
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(prefix)); // Prefix
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(suffix)); // Suffix
}
if (action == 0 || action == 3 || action == 4) {
@ -681,7 +665,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
public void handle(PacketWrapper wrapper) throws Exception {
int action = wrapper.get(Type.VAR_INT, 0);
if (action >= 0 && action <= 2) {
wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING)));
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT));
}
}
});
@ -694,13 +678,11 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
registerOutgoing(ClientboundPackets1_12_1.TAB_LIST, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING);
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(Type.STRING, 0, ChatRewriter.processTranslate(wrapper.get(Type.STRING, 0)));
wrapper.set(Type.STRING, 1, ChatRewriter.processTranslate(wrapper.get(Type.STRING, 1)));
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT));
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT));
}
});
}
@ -724,15 +706,16 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
// Display data
if (wrapper.passthrough(Type.BOOLEAN)) {
wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING))); // Title
wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING))); // Description
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)); // Title
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)); // Description
Item icon = wrapper.read(Type.ITEM);
InventoryPackets.toClient(icon);
wrapper.write(Type.FLAT_ITEM, icon); // Translate item to flat item
wrapper.passthrough(Type.VAR_INT); // Frame type
int flags = wrapper.passthrough(Type.INT); // Flags
if ((flags & 1) != 0)
if ((flags & 1) != 0) {
wrapper.passthrough(Type.STRING); // Background texture
}
wrapper.passthrough(Type.FLOAT); // X
wrapper.passthrough(Type.FLOAT); // Y
}
@ -902,12 +885,7 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
registerIncoming(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
create(new ValueCreator() {
@Override
public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.STRING, "MC|AutoCmd");
}
});
create(wrapper -> wrapper.write(Type.STRING, "MC|AutoCmd"));
handler(POS_TO_3_INT);
map(Type.STRING); // Command
handler(new PacketHandler() {

Datei anzeigen

@ -29,7 +29,7 @@ import java.util.Map;
import java.util.Map.Entry;
public class ConnectionData {
private static final BlockChangeRecord[] A = new BlockChangeRecord[0];
private static final BlockChangeRecord[] EMPTY_RECORDS = new BlockChangeRecord[0];
public static BlockConnectionProvider blockConnectionProvider;
static Int2ObjectMap<String> idToKey = new Int2ObjectOpenHashMap<>(8582, 1F);
static Map<String, Integer> keyToId = new HashMap<>(8582, 1F);
@ -121,7 +121,7 @@ public class ConnectionData {
PacketWrapper wrapper = new PacketWrapper(0x0F, null, user);
wrapper.write(Type.INT, chunkX + chunkDeltaX);
wrapper.write(Type.INT, chunkZ + chunkDeltaZ);
wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, updates.toArray(A));
wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, updates.toArray(EMPTY_RECORDS));
try {
wrapper.send(Protocol1_13To1_12_2.class, true, true);
} catch (Exception e) {

Datei anzeigen

@ -304,11 +304,7 @@ public class InventoryPackets {
if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name");
display.put(new StringTag(NBT_TAG_NAME + "|Name", name.getValue()));
name.setValue(
ChatRewriter.legacyTextToJson(
name.getValue()
)
);
name.setValue(ChatRewriter.legacyTextToJson(name.getValue()).toString());
}
}
// ench is now Enchantments and now uses identifiers

Datei anzeigen

@ -55,7 +55,7 @@ public class BannerHandler implements BlockEntityProvider.BlockEntityHandler {
Tag name = tag.get("CustomName");
if (name instanceof StringTag) {
((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()));
((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()).toString());
}
return blockId;

Datei anzeigen

@ -3,20 +3,24 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.blockentiti
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonElement;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
import us.myles.ViaVersion.util.GsonUtil;
public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandler {
@Override
public int transform(UserConnection user, CompoundTag tag) {
Tag name = tag.get("CustomName");
if (name instanceof StringTag) {
((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()));
((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()).toString());
}
Tag out = tag.get("LastOutput");
if (out instanceof StringTag) {
((StringTag) out).setValue(ChatRewriter.processTranslate(((StringTag) out).getValue()));
JsonElement value = GsonUtil.getJsonParser().parse(((StringTag) out).getValue());
ChatRewriter.processTranslate(value);
((StringTag) out).setValue(value.toString());
}
return -1;
}

Datei anzeigen

@ -1,46 +0,0 @@
package us.myles.ViaVersion.protocols.protocol1_14to1_13_2;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.chat.ComponentSerializer;
public class InventoryNameRewriter {
public static String processTranslate(String value) {
BaseComponent[] components = ComponentSerializer.parse(value);
for (BaseComponent component : components) {
processTranslate(component);
}
if (components.length == 1) {
return ComponentSerializer.toString(components[0]);
} else {
return ComponentSerializer.toString(components);
}
}
private static void processTranslate(BaseComponent component) {
if (component == null) return;
if (component instanceof TranslatableComponent) {
String oldTranslate = ((TranslatableComponent) component).getTranslate();
// Mojang decided to remove .name from inventory titles
if (oldTranslate.startsWith("block.") && oldTranslate.endsWith(".name")) {
((TranslatableComponent) component).setTranslate(oldTranslate.substring(0, oldTranslate.length() - 5));
}
if (((TranslatableComponent) component).getWith() != null) {
for (BaseComponent baseComponent : ((TranslatableComponent) component).getWith()) {
processTranslate(baseComponent);
}
}
}
if (component.getHoverEvent() != null) {
for (BaseComponent baseComponent : component.getHoverEvent().getValue()) {
processTranslate(baseComponent);
}
}
if (component.getExtra() != null) {
for (BaseComponent baseComponent : component.getExtra()) {
processTranslate(baseComponent);
}
}
}
}

Datei anzeigen

@ -7,17 +7,19 @@ import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.common.collect.Sets;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.InventoryNameRewriter;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData;
@ -29,6 +31,16 @@ import java.util.concurrent.ThreadLocalRandom;
public class InventoryPackets {
private static final String NBT_TAG_NAME = "ViaVersion|" + Protocol1_14To1_13_2.class.getSimpleName();
private static final Set<String> REMOVED_RECIPE_TYPES = Sets.newHashSet("crafting_special_banneraddpattern", "crafting_special_repairitem");
private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() {
@Override
protected void handleTranslate(JsonObject object, String translate) {
super.handleTranslate(object, translate);
// Mojang decided to remove .name from inventory titles
if (translate.startsWith("block.") && translate.endsWith(".name")) {
object.addProperty("translate", translate.substring(0, translate.length() - 5));
}
}
};
public static void register(Protocol protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
@ -43,10 +55,10 @@ public class InventoryPackets {
public void handle(PacketWrapper wrapper) throws Exception {
Short windowsId = wrapper.read(Type.UNSIGNED_BYTE);
String type = wrapper.read(Type.STRING);
String title = InventoryNameRewriter.processTranslate(wrapper.read(Type.STRING));
JsonElement title = wrapper.read(Type.COMPONENT);
COMPONENT_REWRITER.processText(title);
Short slots = wrapper.read(Type.UNSIGNED_BYTE);
if (type.equals("EntityHorse")) {
wrapper.setId(0x1F);
int entityId = wrapper.read(Type.INT);
@ -101,7 +113,7 @@ public class InventoryPackets {
}
wrapper.write(Type.VAR_INT, typeId);
wrapper.write(Type.COMPONENT_STRING, title);
wrapper.write(Type.COMPONENT, title);
}
}
});
@ -258,11 +270,7 @@ public class InventoryPackets {
display.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|Lore", ConverterRegistry.convertToValue(lore)));
for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) {
((StringTag) loreEntry).setValue(
ChatRewriter.legacyTextToJson(
((StringTag) loreEntry).getValue()
)
);
((StringTag) loreEntry).setValue(ChatRewriter.legacyTextToJson(((StringTag) loreEntry).getValue()).toString());
}
}
}

Datei anzeigen

@ -17,7 +17,7 @@ import java.util.Arrays;
import java.util.List;
public class Chunk1_15Type extends PartialType<Chunk, ClientWorld> {
private static final CompoundTag[] A = new CompoundTag[0];
private static final CompoundTag[] EMPTY_COMPOUNDS = new CompoundTag[0];
public Chunk1_15Type(ClientWorld param) {
super(param, Chunk.class);
@ -98,7 +98,7 @@ public class Chunk1_15Type extends PartialType<Chunk, ClientWorld> {
}
// Write Block Entities
Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(A));
Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(EMPTY_COMPOUNDS));
}
@Override

Datei anzeigen

@ -100,7 +100,7 @@ public class Protocol1_16To1_15_2 extends Protocol<ClientboundPackets1_15, Clien
registerOutgoing(ClientboundPackets1_15.CHAT_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.COMPONENT_STRING);
map(Type.COMPONENT);
map(Type.BYTE);
handler(wrapper -> wrapper.write(Type.UUID, ZERO_UUID)); // sender uuid
}

Datei anzeigen

@ -27,7 +27,7 @@ public class InventoryPackets {
public void registerMap() {
map(Type.VAR_INT);
map(Type.VAR_INT);
map(Type.COMPONENT_STRING);
map(Type.COMPONENT);
handler(wrapper -> {
int windowType = wrapper.get(Type.VAR_INT, 1);

Datei anzeigen

@ -17,7 +17,7 @@ import java.util.Arrays;
import java.util.List;
public class Chunk1_16Type extends PartialType<Chunk, ClientWorld> {
private static final CompoundTag[] A = new CompoundTag[0];
private static final CompoundTag[] EMPTY_COMPOUNDS = new CompoundTag[0];
public Chunk1_16Type(ClientWorld param) {
super(param, Chunk.class);
@ -100,7 +100,7 @@ public class Chunk1_16Type extends PartialType<Chunk, ClientWorld> {
}
// Write Block Entities
Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(A));
Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(EMPTY_COMPOUNDS));
}
@Override

Datei anzeigen

@ -3,6 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.google.gson.JsonElement;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.Position;
@ -37,9 +38,10 @@ public class Protocol1_9_3To1_9_1_2 extends Protocol<ClientboundPackets1_9, Clie
public void handle(PacketWrapper wrapper) throws Exception {
//read data
Position position = wrapper.read(Type.POSITION);
String[] lines = new String[4];
for (int i = 0; i < 4; i++)
lines[i] = wrapper.read(Type.STRING);
JsonElement[] lines = new JsonElement[4];
for (int i = 0; i < 4; i++) {
lines[i] = wrapper.read(Type.COMPONENT);
}
wrapper.clearInputBuffer();
@ -54,8 +56,9 @@ public class Protocol1_9_3To1_9_1_2 extends Protocol<ClientboundPackets1_9, Clie
tag.put(new IntTag("x", position.getX()));
tag.put(new IntTag("y", position.getY()));
tag.put(new IntTag("z", position.getZ()));
for (int i = 0; i < lines.length; i++)
tag.put(new StringTag("Text" + (i + 1), lines[i]));
for (int i = 0; i < lines.length; i++) {
tag.put(new StringTag("Text" + (i + 1), lines[i].toString()));
}
wrapper.write(Type.NBT, tag);
}

Datei anzeigen

@ -263,7 +263,7 @@ public class ItemRewriter {
}
ListTag pages = tag.get("pages");
if (pages == null) {
pages = new ListTag("pages", Collections.<Tag>singletonList(new StringTag(Protocol1_9To1_8.fixJson(""))));
pages = new ListTag("pages", Collections.<Tag>singletonList(new StringTag(Protocol1_9To1_8.fixJson("").toString())));
tag.put(pages);
item.setTag(tag);
return;
@ -273,7 +273,7 @@ public class ItemRewriter {
if (!(pages.get(i) instanceof StringTag))
continue;
StringTag page = pages.get(i);
page.setValue(Protocol1_9To1_8.fixJson(page.getValue()));
page.setValue(Protocol1_9To1_8.fixJson(page.getValue()).toString());
}
item.setTag(tag);
}

Datei anzeigen

@ -1,5 +1,6 @@
package us.myles.ViaVersion.protocols.protocol1_9to1_8;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
@ -14,17 +15,32 @@ import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_8.ClientboundPackets1_8;
import us.myles.ViaVersion.protocols.protocol1_8.ServerboundPackets1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.*;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.*;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.*;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.EntityPackets;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.InventoryPackets;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.PlayerPackets;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.SpawnPackets;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.WorldPackets;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BossBarProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.CommandBlockProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.EntityIdProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MainHandProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.CommandBlockStorage;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.InventoryTracker;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.PlaceBlockTracker;
import us.myles.ViaVersion.util.GsonUtil;
import java.util.List;
public class Protocol1_9To1_8 extends Protocol<ClientboundPackets1_8, ClientboundPackets1_9, ServerboundPackets1_8, ServerboundPackets1_9> {
public static final ValueTransformer<String, String> FIX_JSON = new ValueTransformer<String, String>(Type.STRING) {
public static final ValueTransformer<String, JsonElement> FIX_JSON = new ValueTransformer<String, JsonElement>(Type.COMPONENT) {
@Override
public String transform(PacketWrapper wrapper, String line) {
public JsonElement transform(PacketWrapper wrapper, String line) {
return fixJson(line);
}
};
@ -33,7 +49,7 @@ public class Protocol1_9To1_8 extends Protocol<ClientboundPackets1_8, Clientboun
super(ClientboundPackets1_8.class, ClientboundPackets1_9.class, ServerboundPackets1_8.class, ServerboundPackets1_9.class);
}
public static String fixJson(String line) {
public static JsonElement fixJson(String line) {
if (line == null || line.equalsIgnoreCase("null")) {
line = "{\"text\":\"\"}";
} else {
@ -45,22 +61,21 @@ public class Protocol1_9To1_8 extends Protocol<ClientboundPackets1_8, Clientboun
}
}
try {
GsonUtil.getGson().fromJson(line, JsonObject.class);
return GsonUtil.getGson().fromJson(line, JsonObject.class);
} catch (Exception e) {
if (Via.getConfig().isForceJsonTransform()) {
return constructJson(line);
} else {
Via.getPlatform().getLogger().warning("Invalid JSON String: \"" + line + "\" Please report this issue to the ViaVersion Github: " + e.getMessage());
return "{\"text\":\"\"}";
return GsonUtil.getGson().fromJson("{\"text\":\"\"}", JsonObject.class);
}
}
return line;
}
private static String constructJson(String text) {
private static JsonElement constructJson(String text) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("text", text);
return GsonUtil.getGson().toJson(jsonObject);
return jsonObject;
}
public static Item getHandItem(final UserConnection info) {

Datei anzeigen

@ -35,9 +35,8 @@ public class PlayerPackets {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
try {
JsonObject obj = (JsonObject) GsonUtil.getJsonParser().parse(wrapper.get(Type.STRING, 0));
JsonObject obj = (JsonObject) wrapper.get(Type.COMPONENT, 0);
ChatRewriter.toClient(obj, wrapper.user());
wrapper.set(Type.STRING, 0, obj.toString());
} catch (Exception e) {
e.printStackTrace();
}