Also to component rewriter methods

This commit is contained in:
Nassim Jahnke 2024-04-21 23:37:00 +02:00
parent 2a4bfb936f
commit 0cd46fdac9
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
22 changed files with 71 additions and 74 deletions

View File

@ -204,7 +204,7 @@ public abstract class EntityRewriterBase<C extends ClientboundPacketType, T exte
rewriteParticle(event.user(), meta.value());
} else if (type == optionalComponentType || type == componentType) {
JsonElement text = meta.value();
protocol.getTranslatableRewriter().processText(text);
protocol.getTranslatableRewriter().processText(event.user(), text);
}
});
}
@ -232,7 +232,7 @@ public abstract class EntityRewriterBase<C extends ClientboundPacketType, T exte
} else if (type == particleType) {
rewriteParticle(event.user(), meta.value());
} else if (type == optionalComponentType || type == componentType) {
protocol.getTranslatableRewriter().processTag(meta.value());
protocol.getTranslatableRewriter().processTag(event.user(), meta.value());
}
});
}

View File

@ -57,7 +57,7 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
// Handle name and lore components
StringTag name = display.getStringTag("Name");
if (name != null) {
String newValue = protocol.getTranslatableRewriter().processText(name.getValue()).toString();
String newValue = protocol.getTranslatableRewriter().processText(connection, name.getValue()).toString();
if (!newValue.equals(name.getValue())) {
saveStringTag(display, name, "Name");
}
@ -69,7 +69,7 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
if (lore != null) {
boolean changed = false;
for (StringTag loreEntry : lore) {
String newValue = protocol.getTranslatableRewriter().processText(loreEntry.getValue()).toString();
String newValue = protocol.getTranslatableRewriter().processText(connection, loreEntry.getValue()).toString();
if (!changed && !newValue.equals(loreEntry.getValue())) {
// Backup original lore before doing any modifications
changed = true;
@ -147,8 +147,8 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
final JsonElement description = wrapper.passthrough(Type.COMPONENT);
final TranslatableRewriter<C> translatableRewriter = protocol.getTranslatableRewriter();
if (translatableRewriter != null) {
translatableRewriter.processText(title);
translatableRewriter.processText(description);
translatableRewriter.processText(wrapper.user(), title);
translatableRewriter.processText(wrapper.user(), description);
}
final Item icon = handleItemToClient(wrapper.user(), wrapper.read(itemType()));
@ -195,8 +195,8 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
final Tag description = wrapper.passthrough(Type.TAG);
final TranslatableRewriter<C> translatableRewriter = protocol.getTranslatableRewriter();
if (translatableRewriter != null) {
translatableRewriter.processTag(title);
translatableRewriter.processTag(description);
translatableRewriter.processTag(wrapper.user(), title);
translatableRewriter.processTag(wrapper.user(), description);
}
final Item icon = handleItemToClient(wrapper.user(), wrapper.read(itemType()));

View File

@ -20,6 +20,7 @@ package com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets;
import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12;
import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.AdvancementTranslations;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.rewriter.RewriterBase;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.gson.JsonElement;
@ -31,8 +32,8 @@ public class ChatPackets1_12 extends RewriterBase<Protocol1_11_1To1_12> {
public static final ComponentRewriter<ClientboundPackets1_12> COMPONENT_REWRITER = new ComponentRewriter<ClientboundPackets1_12>(null, ComponentRewriter.ReadType.JSON) {
@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;
}
@ -64,7 +65,7 @@ public class ChatPackets1_12 extends RewriterBase<Protocol1_11_1To1_12> {
protected void registerPackets() {
protocol.registerClientbound(ClientboundPackets1_12.CHAT_MESSAGE, wrapper -> {
JsonElement element = wrapper.passthrough(Type.COMPONENT);
COMPONENT_REWRITER.processText(element);
COMPONENT_REWRITER.processText(wrapper.user(), element);
});
}
}

View File

@ -191,7 +191,7 @@ public class EntityPackets1_12 extends LegacyEntityRewriter<ClientboundPackets1_
filter().handler((event, meta) -> {
if (meta.metaType() == MetaType1_12.Chat) {
ChatPackets1_12.COMPONENT_REWRITER.processText((JsonElement) meta.getValue());
ChatPackets1_12.COMPONENT_REWRITER.processText(event.user(), (JsonElement) meta.getValue());
}
});

View File

@ -148,25 +148,25 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol<ClientboundPackets1_
return translatableRewriter;
}
public String jsonToLegacy(String value) {
public String jsonToLegacy(UserConnection connection, String value) {
if (value.isEmpty()) {
return "";
}
try {
return jsonToLegacy(JsonParser.parseString(value));
return jsonToLegacy(connection, JsonParser.parseString(value));
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String jsonToLegacy(@Nullable JsonElement value) {
public String jsonToLegacy(UserConnection connection, @Nullable JsonElement value) {
if (value == null || value.isJsonNull()) {
return "";
}
translatableToLegacyRewriter.processText(value);
translatableToLegacyRewriter.processText(connection, value);
return ComponentUtil.jsonToLegacy(value);
}
}

View File

@ -570,7 +570,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit
StringTag name = display.getStringTag("Name");
if (name != null) {
display.putString(extraNbtTag + "|Name", name.getValue());
name.setValue(protocol.jsonToLegacy(name.getValue()));
name.setValue(protocol.jsonToLegacy(connection, name.getValue()));
}
}

View File

@ -283,12 +283,12 @@ public class EntityPackets1_13 extends LegacyEntityRewriter<ClientboundPackets1_
int typeId = meta.metaType().typeId();
if (typeId == 4) {
JsonElement element = meta.value();
protocol.translatableRewriter().processText(element);
protocol.translatableRewriter().processText(event.user(), element);
meta.setMetaType(MetaType1_12.Chat);
} else if (typeId == 5) {
// Rewrite optional chat to string
JsonElement element = meta.value();
meta.setTypeAndValue(MetaType1_12.String, protocol.jsonToLegacy(element));
meta.setTypeAndValue(MetaType1_12.String, protocol.jsonToLegacy(event.user(), element));
} else if (typeId == 6) {
Item item = (Item) meta.getValue();
meta.setTypeAndValue(MetaType1_12.Slot, protocol.getItemRewriter().handleItemToClient(event.user(), item));

View File

@ -201,7 +201,7 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
byte mode = wrapper.get(Type.BYTE, 0);
if (mode == 0 || mode == 2) {
JsonElement value = wrapper.read(Type.COMPONENT);
String legacyValue = protocol.jsonToLegacy(value);
String legacyValue = protocol.jsonToLegacy(wrapper.user(), value);
wrapper.write(Type.STRING, ChatUtil.fromLegacy(legacyValue, 'f', 32));
int type = wrapper.read(Type.VAR_INT);
wrapper.write(Type.STRING, type == 1 ? "hearts" : "integer");
@ -219,7 +219,7 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
byte action = wrapper.get(Type.BYTE, 0);
if (action == 0 || action == 2) {
JsonElement displayName = wrapper.read(Type.COMPONENT);
String legacyTextDisplayName = protocol.jsonToLegacy(displayName);
String legacyTextDisplayName = protocol.jsonToLegacy(wrapper.user(), displayName);
wrapper.write(Type.STRING, ChatUtil.fromLegacy(legacyTextDisplayName, 'f', 32));
byte flags = wrapper.read(Type.BYTE);
@ -234,11 +234,11 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
JsonElement prefixComponent = wrapper.read(Type.COMPONENT);
JsonElement suffixComponent = wrapper.read(Type.COMPONENT);
String prefix = protocol.jsonToLegacy(prefixComponent);
String prefix = protocol.jsonToLegacy(wrapper.user(), prefixComponent);
if (ViaBackwards.getConfig().addTeamColorTo1_13Prefix()) {
prefix += "§" + (colour > -1 && colour <= 15 ? Integer.toHexString(colour) : "r");
}
String suffix = protocol.jsonToLegacy(suffixComponent);
String suffix = protocol.jsonToLegacy(wrapper.user(), suffixComponent);
wrapper.write(Type.STRING, ChatUtil.fromLegacyPrefix(prefix, 'f', 16));
wrapper.write(Type.STRING, ChatUtil.fromLegacy(suffix, '\0', 16));

View File

@ -105,7 +105,7 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol<ClientboundPackets1_
map(Type.STRING); // Window Type
handler(wrapper -> {
JsonElement title = wrapper.passthrough(Type.COMPONENT);
translatableRewriter.processText(title);
translatableRewriter.processText(wrapper.user(), title);
if (ViaBackwards.getConfig().fix1_13FormattedInventoryTitle()) {
if (title.isJsonObject() && title.getAsJsonObject().size() == 1
@ -151,7 +151,7 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol<ClientboundPackets1_
handler(wrapper -> {
int action = wrapper.get(Type.VAR_INT, 0);
if (action == 0 || action == 3) {
translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT));
translatableRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT));
if (action == 0) {
wrapper.passthrough(Type.FLOAT);
wrapper.passthrough(Type.VAR_INT);

View File

@ -142,7 +142,7 @@ public class EntityPackets1_13_1 extends LegacyEntityRewriter<ClientboundPackets
rewriteParticle(event.user(), (Particle) meta.getValue());
} else if (meta.metaType() == Types1_13.META_TYPES.optionalComponentType || meta.metaType() == Types1_13.META_TYPES.componentType) {
JsonElement element = meta.value();
protocol.translatableRewriter().processText(element);
protocol.translatableRewriter().processText(event.user(), element);
}
});

View File

@ -76,14 +76,14 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol<ClientboundPackets1_
JsonElement description = object.get("description");
if (description == null) return;
translatableRewriter.processText(description);
translatableRewriter.processText(wrapper.user(), description);
wrapper.set(Type.STRING, 0, object.toString());
});
registerClientbound(ClientboundPackets1_16.CHAT_MESSAGE, new PacketHandlers() {
@Override
public void register() {
handler(wrapper -> translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
handler(wrapper -> translatableRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)));
map(Type.BYTE);
read(Type.UUID); // Sender
}
@ -94,7 +94,7 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol<ClientboundPackets1_
public void register() {
map(Type.VAR_INT); // Window Id
map(Type.VAR_INT); // Window Type
handler(wrapper -> translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
handler(wrapper -> translatableRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)));
handler(wrapper -> {
int windowType = wrapper.get(Type.VAR_INT, 1);
if (windowType == 20) { // Smithing table

View File

@ -19,6 +19,7 @@ package com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.chat;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
@ -52,8 +53,8 @@ public class TranslatableRewriter1_16 extends TranslatableRewriter<ClientboundPa
}
@Override
public void processText(JsonElement value) {
super.processText(value);
public void processText(UserConnection connection, JsonElement value) {
super.processText(connection, value);
if (value == null || !value.isJsonObject()) return;

View File

@ -313,7 +313,7 @@ public class BlockItemPackets1_16 extends com.viaversion.viabackwards.api.rewrit
ListTag<StringTag> pagesTag = tag.getListTag("pages", StringTag.class);
if (pagesTag != null) {
for (StringTag page : pagesTag) {
JsonElement jsonElement = protocol.getTranslatableRewriter().processText(page.getValue());
JsonElement jsonElement = protocol.getTranslatableRewriter().processText(connection, page.getValue());
page.setValue(jsonElement.toString());
}
}

View File

@ -203,36 +203,31 @@ public class EntityPackets1_16 extends EntityRewriter<ClientboundPackets1_16, Pr
}
});
protocol.registerClientbound(ClientboundPackets1_16.PLAYER_INFO, new PacketHandlers() {
@Override
public void register() {
handler(packetWrapper -> {
int action = packetWrapper.passthrough(Type.VAR_INT);
int playerCount = packetWrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < playerCount; i++) {
packetWrapper.passthrough(Type.UUID);
if (action == 0) { // Add
packetWrapper.passthrough(Type.STRING);
int properties = packetWrapper.passthrough(Type.VAR_INT);
for (int j = 0; j < properties; j++) {
packetWrapper.passthrough(Type.STRING);
packetWrapper.passthrough(Type.STRING);
packetWrapper.passthrough(Type.OPTIONAL_STRING);
}
packetWrapper.passthrough(Type.VAR_INT);
packetWrapper.passthrough(Type.VAR_INT);
// Display Name
protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.OPTIONAL_COMPONENT));
} else if (action == 1) { // Update Game Mode
packetWrapper.passthrough(Type.VAR_INT);
} else if (action == 2) { // Update Ping
packetWrapper.passthrough(Type.VAR_INT);
} else if (action == 3) { // Update Display Name
// Display name
protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.OPTIONAL_COMPONENT));
} // 4 = Remove Player
protocol.registerClientbound(ClientboundPackets1_16.PLAYER_INFO, wrapper -> {
int action = wrapper.passthrough(Type.VAR_INT);
int playerCount = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < playerCount; i++) {
wrapper.passthrough(Type.UUID);
if (action == 0) { // Add
wrapper.passthrough(Type.STRING);
int properties = wrapper.passthrough(Type.VAR_INT);
for (int j = 0; j < properties; j++) {
wrapper.passthrough(Type.STRING);
wrapper.passthrough(Type.STRING);
wrapper.passthrough(Type.OPTIONAL_STRING);
}
});
wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.VAR_INT);
// Display Name
protocol.getTranslatableRewriter().processText(wrapper.user(), wrapper.passthrough(Type.OPTIONAL_COMPONENT));
} else if (action == 1) { // Update Game Mode
wrapper.passthrough(Type.VAR_INT);
} else if (action == 2) { // Update Ping
wrapper.passthrough(Type.VAR_INT);
} else if (action == 3) { // Update Display Name
// Display name
protocol.getTranslatableRewriter().processText(wrapper.user(), wrapper.passthrough(Type.OPTIONAL_COMPONENT));
} // 4 = Remove Player
}
});
}
@ -252,7 +247,7 @@ public class EntityPackets1_16 extends EntityRewriter<ClientboundPackets1_16, Pr
} else if (type == Types1_14.META_TYPES.optionalComponentType) {
JsonElement text = meta.value();
if (text != null) {
protocol.getTranslatableRewriter().processText(text);
protocol.getTranslatableRewriter().processText(event.user(), text);
}
}
});

View File

@ -75,7 +75,7 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol<ClientboundPackets
registerClientbound(ClientboundPackets1_16_2.CHAT_MESSAGE, wrapper -> {
JsonElement message = wrapper.passthrough(Type.COMPONENT);
translatableRewriter.processText(message);
translatableRewriter.processText(wrapper.user(), message);
byte position = wrapper.passthrough(Type.BYTE);
if (position == 2) { // https://bugs.mojang.com/browse/MC-119145
wrapper.clearPacket();

View File

@ -200,7 +200,7 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol<ClientboundPac
return;
}
translatableRewriter.processText(decorationResult.content());
translatableRewriter.processText(wrapper.user(), decorationResult.content());
wrapper.write(Type.COMPONENT, decorationResult.content());
wrapper.write(Type.BYTE, decorationResult.overlay() ? (byte) 2 : 1);
wrapper.write(Type.UUID, sender);
@ -216,7 +216,7 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol<ClientboundPac
public void register() {
handler(wrapper -> {
final JsonElement content = wrapper.passthrough(Type.COMPONENT);
translatableRewriter.processText(content);
translatableRewriter.processText(wrapper.user(), content);
// Screw everything that isn't a system or game info type (which would only happen on funny 1.19.0 servers)
final int typeId = wrapper.read(Type.VAR_INT);

View File

@ -309,7 +309,7 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT);
final JsonElement content = unsignedContent != null ? unsignedContent : ComponentUtil.plainToJson(plainContent);
translatableRewriter.processText(content);
translatableRewriter.processText(wrapper.user(), content);
final int filterMaskType = wrapper.read(Type.VAR_INT);
if (filterMaskType == 2) {
wrapper.read(Type.LONG_ARRAY_PRIMITIVE); // Mask
@ -331,7 +331,7 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
});
registerClientbound(ClientboundPackets1_19_3.DISGUISED_CHAT, ClientboundPackets1_19_1.SYSTEM_CHAT, wrapper -> {
final JsonElement content = wrapper.read(Type.COMPONENT);
translatableRewriter.processText(content);
translatableRewriter.processText(wrapper.user(), content);
final int chatTypeId = wrapper.read(Type.VAR_INT);
final JsonElement senderName = wrapper.read(Type.COMPONENT);
final JsonElement targetName = wrapper.read(Type.OPTIONAL_COMPONENT);

View File

@ -59,7 +59,7 @@ public final class BlockItemPackets1_19_4 extends ItemRewriter<ClientboundPacket
wrapper.set(Type.VAR_INT, 1, windowType - 1);
}
protocol.getTranslatableRewriter().processText(wrapper.get(Type.COMPONENT, 0));
protocol.getTranslatableRewriter().processText(wrapper.user(), wrapper.get(Type.COMPONENT, 0));
});
}
});

View File

@ -164,7 +164,7 @@ public final class EntityPackets1_19_4 extends EntityRewriter<ClientboundPackets
event.createExtraMeta(new Metadata(3, Types1_19_3.META_TYPES.booleanType, true)); // Show custom name
final JsonElement element = meta.value();
protocol.getTranslatableRewriter().processText(element);
protocol.getTranslatableRewriter().processText(event.user(), element);
}));
filter().type(EntityTypes1_19_4.DISPLAY).handler((event, meta) -> {
// TODO Maybe spawn an extra entity to ride the armor stand for blocks and items

View File

@ -87,7 +87,7 @@ public final class Protocol1_19_4To1_20 extends BackwardsProtocol<ClientboundPac
registerClientbound(ClientboundPackets1_19_4.COMBAT_KILL, wrapper -> {
wrapper.passthrough(Type.VAR_INT); // Duration
wrapper.write(Type.INT, -1); // Killer ID - unused (who knows for how long?)
translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT));
translatableRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT));
});
}

View File

@ -171,14 +171,14 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol<ClientboundPac
return;
}
translatableRewriter.processText(decoratedMessage);
translatableRewriter.processText(wrapper.user(), decoratedMessage);
wrapper.write(Type.COMPONENT, decoratedMessage);
wrapper.write(Type.VAR_INT, SYSTEM_CHAT_ID);
});
registerClientbound(ClientboundPackets1_19_1.SYSTEM_CHAT, wrapper -> {
final JsonElement content = wrapper.passthrough(Type.COMPONENT);
translatableRewriter.processText(content);
translatableRewriter.processText(wrapper.user(), content);
final boolean overlay = wrapper.read(Type.BOOLEAN);
wrapper.write(Type.VAR_INT, overlay ? GAME_INFO_ID : SYSTEM_CHAT_ID);

View File

@ -370,13 +370,13 @@ public final class Protocol1_20_2To1_20_3 extends BackwardsProtocol<ClientboundP
private void convertComponent(final PacketWrapper wrapper) throws Exception {
final Tag tag = wrapper.read(Type.TAG);
translatableRewriter.processTag(tag);
translatableRewriter.processTag(wrapper.user(), tag);
wrapper.write(Type.COMPONENT, ComponentUtil.tagToJson(tag));
}
private void convertOptionalComponent(final PacketWrapper wrapper) throws Exception {
final Tag tag = wrapper.read(Type.OPTIONAL_TAG);
translatableRewriter.processTag(tag);
translatableRewriter.processTag(wrapper.user(), tag);
wrapper.write(Type.OPTIONAL_COMPONENT, ComponentUtil.tagToJson(tag));
}