Skip to content

Commit

Permalink
Update component rewriters
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed Jan 10, 2025
1 parent 29ec026 commit de0c869
Show file tree
Hide file tree
Showing 35 changed files with 282 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.viaversion.viabackwards.api;

import com.viaversion.viabackwards.api.data.BackwardsMappingData;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.api.rewriters.text.TranslatableRewriter;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.Protocol;
Expand Down Expand Up @@ -66,7 +66,7 @@ public boolean hasMappingDataToLoad() {
}

@Override
public @Nullable TranslatableRewriter<CU> getComponentRewriter() {
public @Nullable TranslatableRewriter getComponentRewriter() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import com.viaversion.viabackwards.ViaBackwards;
import com.viaversion.viabackwards.ViaBackwardsConfig;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.api.data.TranslatableMappings;
import com.viaversion.viabackwards.protocol.v1_11to1_10.Protocol1_11To1_10;
import com.viaversion.viabackwards.protocol.v1_12to1_11_1.Protocol1_12To1_11_1;
import com.viaversion.viabackwards.protocol.v1_11_1to1_11.Protocol1_11_1To1_11;
Expand Down Expand Up @@ -92,7 +92,7 @@ default void init(final File configFile) {
Via.getManager().getSubPlatforms().add(VersionInfo.getImplementationVersion());

getLogger().info("Loading translations...");
TranslatableRewriter.loadTranslatables();
TranslatableMappings.loadTranslatables();

getLogger().info("Registering protocols...");
final ProtocolManager protocolManager = Via.getManager().getProtocolManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,19 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.api.rewriters;
package com.viaversion.viabackwards.api.data;

import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viabackwards.ViaBackwards;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.BackwardsMappingDataLoader;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import java.util.HashMap;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;

public class TranslatableRewriter<C extends ClientboundPacketType> extends ComponentRewriter<C> {
public final class TranslatableMappings {

private static final Map<String, Map<String, String>> TRANSLATABLES = new HashMap<>();
private final Map<String, String> translatables;

public static void loadTranslatables() {
if (!TRANSLATABLES.isEmpty()) {
Expand All @@ -53,42 +46,24 @@ public static void fillTranslatables(final JsonObject jsonObject, final Map<Stri
}
}

public TranslatableRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol, final ReadType type) {
this(protocol, type, protocol.getClass().getSimpleName().replace("Protocol", "").split("To")[0].replace("_", "."));
public static Map<String, String> translatablesFor(final Protocol<?, ?, ?, ?> protocol) {
final String version = protocol.getClass().getSimpleName()
.replace("Protocol", "")
.split("To")[0]
.replace("_", ".");
return translatablesFor(version);
}

public TranslatableRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol, final ReadType type, final String version) {
super(protocol, type);
public static Map<String, String> translatablesFor(final String version) {
final Map<String, String> translatableMappings = getTranslatableMappings(version);
if (translatableMappings == null) {
ViaBackwards.getPlatform().getLogger().warning("Missing " + version + " translatables!");
this.translatables = new HashMap<>();
} else {
this.translatables = translatableMappings;
return new HashMap<>();
}
return translatableMappings;
}

@Override
protected void handleTranslate(final JsonObject root, final String translate) {
final String newTranslate = mappedTranslationKey(translate);
if (newTranslate != null) {
root.addProperty("translate", newTranslate);
}
}

@Override
protected void handleTranslate(final UserConnection connection, final CompoundTag parentTag, final StringTag translateTag) {
final String newTranslate = mappedTranslationKey(translateTag.getValue());
if (newTranslate != null) {
parentTag.put("translate", new StringTag(newTranslate));
}
}

public @Nullable String mappedTranslationKey(final String translationKey) {
return translatables.get(translationKey);
}

public static Map<String, String> getTranslatableMappings(final String sectionIdentifier) {
public static @Nullable Map<String, String> getTranslatableMappings(final String sectionIdentifier) {
return TRANSLATABLES.get(sectionIdentifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2016-2025 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.api.rewriters.text;

import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.TranslatableMappings;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.libs.gson.JsonObject;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;

public class JsonNBTComponentRewriter<C extends ClientboundPacketType> extends com.viaversion.viaversion.rewriter.text.JsonNBTComponentRewriter<C> implements TranslatableRewriter {

private final Map<String, String> translatables;

public JsonNBTComponentRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol, final ReadType type) {
super(protocol, type);
this.translatables = TranslatableMappings.translatablesFor(protocol);
}

public JsonNBTComponentRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol, final ReadType type, final String version) {
super(protocol, type);
this.translatables = TranslatableMappings.translatablesFor(version);
}

@Override
protected void handleTranslate(final JsonObject root, final String translate) {
final String newTranslate = mappedTranslationKey(translate);
if (newTranslate != null) {
root.addProperty("translate", newTranslate);
}
}

@Override
protected void handleTranslate(final UserConnection connection, final CompoundTag parentTag, final StringTag translateTag) {
final String newTranslate = mappedTranslationKey(translateTag.getValue());
if (newTranslate != null) {
parentTag.put("translate", new StringTag(newTranslate));
}
}

@Override
public @Nullable String mappedTranslationKey(final String translationKey) {
return translatables.get(translationKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2016-2025 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.api.rewriters.text;

import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.TranslatableMappings;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.libs.gson.JsonObject;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;

public class NBTComponentRewriter<C extends ClientboundPacketType> extends com.viaversion.viaversion.rewriter.text.NBTComponentRewriter<C> implements TranslatableRewriter {

private final Map<String, String> translatables;

public NBTComponentRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol) {
super(protocol);
this.translatables = TranslatableMappings.translatablesFor(protocol);
}

public NBTComponentRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol, final String version) {
super(protocol);
this.translatables = TranslatableMappings.translatablesFor(version);
}

@Override
protected void handleTranslate(final JsonObject root, final String translate) {
final String newTranslate = mappedTranslationKey(translate);
if (newTranslate != null) {
root.addProperty("translate", newTranslate);
}
}

@Override
protected void handleTranslate(final UserConnection connection, final CompoundTag parentTag, final StringTag translateTag) {
final String newTranslate = mappedTranslationKey(translateTag.getValue());
if (newTranslate != null) {
parentTag.put("translate", new StringTag(newTranslate));
}
}

@Override
public @Nullable String mappedTranslationKey(final String translationKey) {
return translatables.get(translationKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2016-2025 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.api.rewriters.text;

import com.viaversion.viaversion.api.rewriter.ComponentRewriter;
import org.checkerframework.checker.nullness.qual.Nullable;

public interface TranslatableRewriter extends ComponentRewriter {

@Nullable
String mappedTranslationKey(String translationKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@

import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.api.rewriters.text.NBTComponentRewriter;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ClientboundPacket1_21_2;
import com.viaversion.viaversion.util.SerializerVersion;

final class ComponentRewriter1_99 extends TranslatableRewriter<ClientboundPacket1_21_2> {
final class ComponentRewriter1_99 extends NBTComponentRewriter<ClientboundPacket1_21_2> {

public ComponentRewriter1_99(final BackwardsProtocol<ClientboundPacket1_21_2, ?, ?, ?> protocol) {
super(protocol, ReadType.NBT);
super(protocol);
}

@Override
Expand All @@ -38,11 +37,6 @@ protected void handleShowItem(final UserConnection connection, final CompoundTag
}

// Remove or update data from componentsTag
// New added data which is not handled otherwise needs to be removed to prevent errors on the client
}

@Override
protected SerializerVersion inputSerializerVersion() {
return SerializerVersion.V1_21_4;
// Newly added data which is not handled otherwise needs to be removed to prevent errors on the client
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.BackwardsMappingData;
import com.viaversion.viabackwards.api.rewriters.SoundRewriter;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.api.rewriters.text.NBTComponentRewriter;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_21_4;
import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypesProvider;
Expand Down Expand Up @@ -57,7 +57,7 @@ final class Protocol1_98To1_99 extends BackwardsProtocol<ClientboundPacket1_21_2
private final EntityPacketRewriter1_99 entityRewriter = new EntityPacketRewriter1_99(this);
private final BlockItemPacketRewriter1_99 itemRewriter = new BlockItemPacketRewriter1_99(this);
private final ParticleRewriter<ClientboundPacket1_21_2> particleRewriter = new ParticleRewriter<>(this, Types1_21_4.PARTICLE/*, Types1_OLD.PARTICLE*/);
private final TranslatableRewriter<ClientboundPacket1_21_2> translatableRewriter = new ComponentRewriter1_99(this);
private final NBTComponentRewriter<ClientboundPacket1_21_2> translatableRewriter = new ComponentRewriter1_99(this);
private final TagRewriter<ClientboundPacket1_21_2> tagRewriter = new TagRewriter<>(this);

public Protocol1_98To1_99() {
Expand All @@ -80,7 +80,7 @@ protected void registerPackets() {
//new AttributeRewriter<>(this).register1_21(ClientboundPackets1_21_2.UPDATE_ATTRIBUTES);

// Registers translatable mappings (missing a whole bunch still)
//translatableRewriter.registerOpenScreen(ClientboundPackets1_21_2.OPEN_SCREEN); // Handled by registerOpenScreen in item rewriters
//translatableRewriter.registerOpenScreen1_14(ClientboundPackets1_21_2.OPEN_SCREEN); // Handled by registerOpenScreen in item rewriters
translatableRewriter.registerComponentPacket(ClientboundPackets1_21_2.SET_ACTION_BAR_TEXT);
translatableRewriter.registerComponentPacket(ClientboundPackets1_21_2.SET_TITLE_TEXT);
translatableRewriter.registerComponentPacket(ClientboundPackets1_21_2.SET_SUBTITLE_TEXT);
Expand Down Expand Up @@ -124,7 +124,7 @@ public ParticleRewriter<ClientboundPacket1_21_2> getParticleRewriter() {
}

@Override
public TranslatableRewriter<ClientboundPacket1_21_2> getComponentRewriter() {
public NBTComponentRewriter<ClientboundPacket1_21_2> getComponentRewriter() {
return translatableRewriter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.BackwardsMappingData;
import com.viaversion.viabackwards.api.rewriters.SoundRewriter;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.api.rewriters.text.JsonNBTComponentRewriter;
import com.viaversion.viabackwards.protocol.v1_10to1_9_3.rewriter.BlockItemPacketRewriter1_10;
import com.viaversion.viabackwards.protocol.v1_10to1_9_3.rewriter.EntityPacketRewriter1_10;
import com.viaversion.viaversion.api.connection.UserConnection;
Expand All @@ -34,7 +34,7 @@
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.v1_9_1to1_9_3.packet.ClientboundPackets1_9_3;
import com.viaversion.viaversion.protocols.v1_9_1to1_9_3.packet.ServerboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.rewriter.text.ComponentRewriterBase;

public class Protocol1_10To1_9_3 extends BackwardsProtocol<ClientboundPackets1_9_3, ClientboundPackets1_9_3, ServerboundPackets1_9_3, ServerboundPackets1_9_3> {

Expand Down Expand Up @@ -92,7 +92,7 @@ public void register() {
}
});

TranslatableRewriter<ClientboundPackets1_9_3> componentRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON);
JsonNBTComponentRewriter<ClientboundPackets1_9_3> componentRewriter = new JsonNBTComponentRewriter<>(this, ComponentRewriterBase.ReadType.JSON);
componentRewriter.registerComponentPacket(ClientboundPackets1_9_3.CHAT);
}

Expand Down
Loading

0 comments on commit de0c869

Please sign in to comment.