Add UserConnection to component rewriters as well

This commit is contained in:
Nassim Jahnke 2024-04-21 23:01:58 +02:00
parent a152993ffd
commit 7ac7ea485f
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
11 changed files with 67 additions and 61 deletions

View File

@ -101,7 +101,7 @@ public class Protocol1_12To1_11_1 extends AbstractProtocol<ClientboundPackets1_9
if (!Via.getConfig().is1_12NBTArrayFix()) return;
try {
final JsonElement element = wrapper.passthrough(Type.COMPONENT);
TranslateRewriter.toClient(element);
TranslateRewriter.toClient(wrapper.user(), element);
ChatItemRewriter.toClient(element);
wrapper.set(Type.COMPONENT, 0, element);

View File

@ -21,6 +21,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.data.AchievementTranslationMapping;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
@ -39,10 +40,10 @@ public class TranslateRewriter {
}
@Override
protected void handleHoverEvent(JsonObject hoverEvent) {
protected void handleHoverEvent(UserConnection connection, JsonObject hoverEvent) {
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (!action.equals("show_achievement")) {
super.handleHoverEvent(hoverEvent);
super.handleHoverEvent(connection, hoverEvent);
return;
}
@ -54,7 +55,7 @@ public class TranslateRewriter {
invalidText.addProperty("color", "red");
hoverEvent.addProperty("action", "show_text");
hoverEvent.add("value", invalidText);
super.handleHoverEvent(hoverEvent);
super.handleHoverEvent(connection, hoverEvent);
return;
}
@ -95,17 +96,17 @@ public class TranslateRewriter {
hoverEvent.addProperty("action", "show_text");
hoverEvent.add("value", invalidText);
}
super.handleHoverEvent(hoverEvent);
super.handleHoverEvent(connection, hoverEvent);
}
};
public static void toClient(JsonElement element) {
public static void toClient(UserConnection connection, JsonElement element) {
if (element instanceof JsonObject) {
JsonObject obj = (JsonObject) element;
JsonElement translate = obj.get("translate");
if (translate != null) {
if (translate.getAsString().startsWith("chat.type.achievement")) {
ACHIEVEMENT_TEXT_REWRITER.processText(obj);
ACHIEVEMENT_TEXT_REWRITER.processText(connection, obj);
}
}
}

View File

@ -170,7 +170,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
EntityPackets.register(this);
WorldPackets.register(this);
registerClientbound(State.LOGIN, 0x00, 0x00, wrapper -> componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
registerClientbound(State.LOGIN, 0x00, 0x00, wrapper -> componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)));
registerClientbound(State.STATUS, 0x00, 0x00, new PacketHandlers() {
@Override
@ -296,7 +296,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
public void register() {
map(Type.UNSIGNED_BYTE); // Id
map(Type.STRING); // Window type
handler(wrapper -> componentRewriter.processText(wrapper.passthrough(Type.COMPONENT))); // Title
handler(wrapper -> componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT))); // Title
}
});
@ -511,8 +511,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
new SoundRewriter<>(this).registerSound(ClientboundPackets1_12_1.SOUND);
registerClientbound(ClientboundPackets1_12_1.TAB_LIST, wrapper -> {
componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT));
componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT));
});
registerClientbound(ClientboundPackets1_12_1.ADVANCEMENTS, wrapper -> {
@ -528,8 +528,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
// Display data
if (wrapper.passthrough(Type.BOOLEAN)) {
componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)); // Title
componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)); // Description
componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)); // Title
componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)); // Description
Item icon = wrapper.read(Type.ITEM1_8);
itemRewriter.handleItemToClient(wrapper.user(), icon);
wrapper.write(Type.ITEM1_13, icon); // Translate item to flat item

View File

@ -24,6 +24,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
@ -41,8 +42,8 @@ public class ComponentRewriter1_13<C extends ClientboundPacketType> extends Comp
}
@Override
protected void handleHoverEvent(JsonObject hoverEvent) {
super.handleHoverEvent(hoverEvent);
protected void handleHoverEvent(UserConnection connection, JsonObject hoverEvent) {
super.handleHoverEvent(connection, hoverEvent);
final String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (!action.equals("show_item")) return;

View File

@ -41,7 +41,7 @@ public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandl
StringTag out = tag.getStringTag("LastOutput");
if (out != null) {
JsonElement value = JsonParser.parseString(out.getValue());
protocol.getComponentRewriter().processText(value);
protocol.getComponentRewriter().processText(user, value);
out.setValue(value.toString());
}
return -1;

View File

@ -72,7 +72,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_13, Serve
Short windowId = wrapper.read(Type.UNSIGNED_BYTE);
String type = wrapper.read(Type.STRING);
JsonElement title = wrapper.read(Type.COMPONENT);
COMPONENT_REWRITER.processText(title);
COMPONENT_REWRITER.processText(wrapper.user(), title);
Short slots = wrapper.read(Type.UNSIGNED_BYTE);
if (type.equals("EntityHorse")) {

View File

@ -127,7 +127,7 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
map(Type.COMPONENT);
map(Type.BYTE);
handler(wrapper -> {
componentRewriter.processText(wrapper.get(Type.COMPONENT, 0));
componentRewriter.processText(wrapper.user(), wrapper.get(Type.COMPONENT, 0));
wrapper.write(Type.UUID, ZERO_UUID); // Sender uuid - always send as 'system'
});
}

View File

@ -20,6 +20,7 @@ package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
@ -67,8 +68,8 @@ public class TranslationMappings extends ComponentRewriter<ClientboundPackets1_1
}
@Override
public void processText(JsonElement element) {
super.processText(element);
public void processText(UserConnection connection, JsonElement element) {
super.processText(connection, element);
if (element == null || !element.isJsonObject()) return;
// Score components no longer contain value fields

View File

@ -159,7 +159,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_15, Serve
ListTag<StringTag> pages = tag.getListTag("pages", StringTag.class);
if (pages != null) {
for (StringTag pageTag : pages) {
pageTag.setValue(protocol.getComponentRewriter().processText(pageTag.getValue()).toString());
pageTag.setValue(protocol.getComponentRewriter().processText(connection, pageTag.getValue()).toString());
}
}
}

View File

@ -23,6 +23,7 @@ import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
import com.viaversion.viaversion.api.minecraft.chunks.DataPalette;
@ -35,6 +36,7 @@ import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPacke
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2;
import com.viaversion.viaversion.rewriter.BlockRewriter;
import com.viaversion.viaversion.util.CompactArrayUtil;
import com.viaversion.viaversion.util.Key;
import com.viaversion.viaversion.util.UUIDUtil;
import java.util.Map;
import java.util.UUID;
@ -87,7 +89,7 @@ public class WorldPackets {
if (chunk.getBlockEntities() == null) return;
for (CompoundTag blockEntity : chunk.getBlockEntities()) {
handleBlockEntity(protocol, blockEntity);
handleBlockEntity(protocol, wrapper.user(), blockEntity);
}
});
@ -95,17 +97,17 @@ public class WorldPackets {
wrapper.passthrough(Type.POSITION1_14); // Position
wrapper.passthrough(Type.UNSIGNED_BYTE); // Action
CompoundTag tag = wrapper.passthrough(Type.NAMED_COMPOUND_TAG);
handleBlockEntity(protocol, tag);
handleBlockEntity(protocol, wrapper.user(), tag);
});
blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001);
}
private static void handleBlockEntity(Protocol1_16To1_15_2 protocol, CompoundTag compoundTag) {
private static void handleBlockEntity(Protocol1_16To1_15_2 protocol, UserConnection connection, CompoundTag compoundTag) {
StringTag idTag = compoundTag.getStringTag("id");
if (idTag == null) return;
String id = idTag.getValue();
String id = Key.namespaced(idTag.getValue());
if (id.equals("minecraft:conduit")) {
Tag targetUuidTag = compoundTag.remove("target_uuid");
if (!(targetUuidTag instanceof StringTag)) return;
@ -131,7 +133,7 @@ public class WorldPackets {
for (int i = 1; i <= 4; i++) {
StringTag line = compoundTag.getStringTag("Text" + i);
if (line != null) {
JsonElement text = protocol.getComponentRewriter().processText(line.getValue());
JsonElement text = protocol.getComponentRewriter().processText(connection, line.getValue());
compoundTag.putString("Text" + i, text.toString());
}
}

View File

@ -28,6 +28,7 @@ import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
@ -84,7 +85,7 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
if (wrapper.passthrough(Type.VAR_INT) == 2) {
wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.INT);
processText(wrapper.passthrough(Type.COMPONENT));
processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT));
}
});
}
@ -96,14 +97,14 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
protocol.registerClientbound(packetType, wrapper -> {
final int action = wrapper.passthrough(Type.VAR_INT);
if (action >= 0 && action <= 2) {
processText(wrapper.passthrough(Type.COMPONENT));
processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT));
}
});
}
public void registerPing() {
// Always json
protocol.registerClientbound(State.LOGIN, ClientboundLoginPackets.LOGIN_DISCONNECT, wrapper -> processText(wrapper.passthrough(Type.COMPONENT)));
protocol.registerClientbound(State.LOGIN, ClientboundLoginPackets.LOGIN_DISCONNECT, wrapper -> processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)));
}
public void registerLegacyOpenWindow(final C packetType) {
@ -112,7 +113,7 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
public void register() {
map(Type.UNSIGNED_BYTE); // Id
map(Type.STRING); // Window Type
handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT)));
handler(wrapper -> processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)));
}
});
}
@ -141,7 +142,7 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
public void register() {
map(Type.VAR_INT);
map(Type.INT);
handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT)));
handler(wrapper -> processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)));
}
});
}
@ -159,18 +160,18 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
public void passthroughAndProcess(final PacketWrapper wrapper) throws Exception {
switch (type) {
case JSON:
processText(wrapper.passthrough(Type.COMPONENT));
processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT));
break;
case NBT:
processTag(wrapper.passthrough(Type.TAG));
processTag(wrapper.user(), wrapper.passthrough(Type.TAG));
break;
}
}
public JsonElement processText(final String value) {
public JsonElement processText(final UserConnection connection, final String value) {
try {
final JsonElement root = JsonParser.parseString(value);
processText(root);
processText(connection, root);
return root;
} catch (final JsonSyntaxException e) {
if (Via.getManager().isDebug()) {
@ -182,43 +183,43 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
}
}
public void processText(final JsonElement element) {
public void processText(final UserConnection connection, final JsonElement element) {
if (element == null || element.isJsonNull()) {
return;
}
if (element.isJsonArray()) {
processJsonArray(element.getAsJsonArray());
processJsonArray(connection, element.getAsJsonArray());
} else if (element.isJsonObject()) {
processJsonObject(element.getAsJsonObject());
processJsonObject(connection, element.getAsJsonObject());
}
}
protected void processJsonArray(final JsonArray array) {
protected void processJsonArray(final UserConnection connection, final JsonArray array) {
for (final JsonElement jsonElement : array) {
processText(jsonElement);
processText(connection, jsonElement);
}
}
protected void processJsonObject(final JsonObject object) {
protected void processJsonObject(final UserConnection connection, final JsonObject object) {
final JsonElement translate = object.get("translate");
if (translate != null && translate.isJsonPrimitive()) {
handleTranslate(object, translate.getAsString());
final JsonElement with = object.get("with");
if (with != null && with.isJsonArray()) {
processJsonArray(with.getAsJsonArray());
processJsonArray(connection, with.getAsJsonArray());
}
}
final JsonElement extra = object.get("extra");
if (extra != null && extra.isJsonArray()) {
processJsonArray(extra.getAsJsonArray());
processJsonArray(connection, extra.getAsJsonArray());
}
final JsonElement hoverEvent = object.get("hoverEvent");
if (hoverEvent != null && hoverEvent.isJsonObject()) {
handleHoverEvent(hoverEvent.getAsJsonObject());
handleHoverEvent(connection, hoverEvent.getAsJsonObject());
}
}
@ -226,7 +227,7 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
// To override if needed
}
protected void handleHoverEvent(final JsonObject hoverEvent) {
protected void handleHoverEvent(final UserConnection connection, final JsonObject hoverEvent) {
// To override if needed (don't forget to call super)
final JsonPrimitive actionElement = hoverEvent.getAsJsonPrimitive("action");
if (!actionElement.isString()) {
@ -236,11 +237,11 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
final String action = actionElement.getAsString();
if (action.equals("show_text")) {
final JsonElement value = hoverEvent.get("value");
processText(value != null ? value : hoverEvent.get("contents"));
processText(connection, value != null ? value : hoverEvent.get("contents"));
} else if (action.equals("show_entity")) {
final JsonElement contents = hoverEvent.get("contents");
if (contents != null && contents.isJsonObject()) {
processText(contents.getAsJsonObject().get("name"));
processText(connection, contents.getAsJsonObject().get("name"));
}
}
}
@ -248,51 +249,51 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
// -----------------------------------------------------------------------
// Tag methods
public void processTag(@Nullable final Tag tag) {
public void processTag(final UserConnection connection, @Nullable final Tag tag) {
if (tag == null) {
return;
}
if (tag instanceof ListTag) {
processListTag((ListTag<?>) tag);
processListTag(connection, (ListTag<?>) tag);
} else if (tag instanceof CompoundTag) {
processCompoundTag((CompoundTag) tag);
processCompoundTag(connection, (CompoundTag) tag);
}
}
private void processListTag(final ListTag<?> tag) {
private void processListTag(final UserConnection connection, final ListTag<?> tag) {
for (final Tag entry : tag) {
processTag(entry);
processTag(connection, entry);
}
}
protected void processCompoundTag(final CompoundTag tag) {
protected void processCompoundTag(final UserConnection connection, final CompoundTag tag) {
final StringTag translate = tag.getStringTag("translate");
if (translate != null) {
handleTranslate(tag, translate);
handleTranslate(connection, tag, translate);
final ListTag<?> with = tag.getListTag("with");
if (with != null) {
processListTag(with);
processListTag(connection, with);
}
}
final ListTag<?> extra = tag.getListTag("extra");
if (extra != null) {
processListTag(extra);
processListTag(connection, extra);
}
final CompoundTag hoverEvent = tag.getCompoundTag("hoverEvent");
if (hoverEvent != null) {
handleHoverEvent(hoverEvent);
handleHoverEvent(connection, hoverEvent);
}
}
protected void handleTranslate(final CompoundTag parentTag, final StringTag translateTag) {
protected void handleTranslate(final UserConnection connection, final CompoundTag parentTag, final StringTag translateTag) {
// To override if needed
}
protected void handleHoverEvent(final CompoundTag hoverEventTag) {
protected void handleHoverEvent(final UserConnection connection, final CompoundTag hoverEventTag) {
// To override if needed (don't forget to call super)
final StringTag actionTag = hoverEventTag.getStringTag("action");
if (actionTag == null) {
@ -302,11 +303,11 @@ public class ComponentRewriter<C extends ClientboundPacketType> {
final String action = actionTag.getValue();
if (action.equals("show_text")) {
final Tag value = hoverEventTag.get("value");
processTag(value != null ? value : hoverEventTag.get("contents"));
processTag(connection, value != null ? value : hoverEventTag.get("contents"));
} else if (action.equals("show_entity")) {
final CompoundTag contents = hoverEventTag.getCompoundTag("contents");
if (contents != null) {
processTag(contents.get("name"));
processTag(connection, contents.get("name"));
}
}
}