mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-11-26 20:16:02 +01:00
Translation rewriting
This commit is contained in:
parent
d84c4365c9
commit
d9eff7bb5c
@ -4,7 +4,9 @@ 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.protocols.protocol1_13to1_12_2.data.MappingData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.regex.Matcher;
|
||||
@ -115,4 +117,39 @@ public class ChatRewriter {
|
||||
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 instanceof TranslatableComponent) {
|
||||
String oldTranslate = ((TranslatableComponent) component).getTranslate();
|
||||
String newTranslate;
|
||||
newTranslate = MappingData.translateMapping.get(oldTranslate);
|
||||
if (newTranslate == null) MappingData.mojangTranslation.get(oldTranslate);
|
||||
if (newTranslate != null) {
|
||||
((TranslatableComponent) component).setTranslate(newTranslate);
|
||||
}
|
||||
for (BaseComponent baseComponent : ((TranslatableComponent) component).getWith()) {
|
||||
processTranslate(baseComponent);
|
||||
}
|
||||
}
|
||||
if (component.getHoverEvent() != null) {
|
||||
for (BaseComponent baseComponent : component.getHoverEvent().getValue()) {
|
||||
processTranslate(baseComponent);
|
||||
}
|
||||
}
|
||||
for (BaseComponent baseComponent : component.getExtra()) {
|
||||
processTranslate(baseComponent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -132,6 +132,19 @@ public class Protocol1_13To1_12_2 extends Protocol {
|
||||
|
||||
// Outgoing packets
|
||||
|
||||
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)));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
registerOutgoing(State.STATUS, 0x00, 0x00, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
@ -171,7 +184,36 @@ public class Protocol1_13To1_12_2 extends Protocol {
|
||||
}
|
||||
});
|
||||
|
||||
registerOutgoing(State.PLAY, 0xF, 0xE);
|
||||
// Boss bar
|
||||
registerOutgoing(State.PLAY, 0xC, 0xC, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.UUID);
|
||||
map(Type.VAR_INT);
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
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)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
// Chat message
|
||||
registerOutgoing(State.PLAY, 0xF, 0xE, 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)));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
// WorldPackets 0x10 -> 0x0F
|
||||
|
||||
// Tab-Complete
|
||||
@ -219,13 +261,39 @@ public class Protocol1_13To1_12_2 extends Protocol {
|
||||
// New packet 0x11, declare commands
|
||||
registerOutgoing(State.PLAY, 0x11, 0x12);
|
||||
registerOutgoing(State.PLAY, 0x12, 0x13);
|
||||
registerOutgoing(State.PLAY, 0x13, 0x14);
|
||||
// Open window
|
||||
registerOutgoing(State.PLAY, 0x13, 0x14, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.UNSIGNED_BYTE); // Id
|
||||
map(Type.STRING); // Window type
|
||||
map(Type.STRING); // Title
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
wrapper.set(Type.STRING, 1, ChatRewriter.processTranslate(wrapper.get(Type.STRING, 1)));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
// InventoryPackets 0x14 -> 0x15
|
||||
// InventoryPackets 0x15 -> 0x16
|
||||
// InventoryPackets 0x16 -> 0x17
|
||||
registerOutgoing(State.PLAY, 0x17, 0x18);
|
||||
// WorldPackets 0x18 -> 0x19
|
||||
registerOutgoing(State.PLAY, 0x1A, 0x1B);
|
||||
// Disconnect
|
||||
registerOutgoing(State.PLAY, 0x1A, 0x1B, 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)));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
registerOutgoing(State.PLAY, 0x1B, 0x1C);
|
||||
// New packet 0x1D - NBT Query
|
||||
registerOutgoing(State.PLAY, 0x1C, 0x1E);
|
||||
@ -328,7 +396,23 @@ public class Protocol1_13To1_12_2 extends Protocol {
|
||||
}
|
||||
});
|
||||
registerOutgoing(State.PLAY, 0x2C, 0x2E);
|
||||
registerOutgoing(State.PLAY, 0x2D, 0x2F);
|
||||
// Combat event
|
||||
registerOutgoing(State.PLAY, 0x2D, 0x2F, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.VAR_INT); // Event
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
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)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
registerOutgoing(State.PLAY, 0x2E, 0x30);
|
||||
// New 0x31 - Face Player
|
||||
registerOutgoing(State.PLAY, 0x2F, 0x32);
|
||||
@ -482,7 +566,22 @@ public class Protocol1_13To1_12_2 extends Protocol {
|
||||
});
|
||||
registerOutgoing(State.PLAY, 0x46, 0x49);
|
||||
registerOutgoing(State.PLAY, 0x47, 0x4A);
|
||||
registerOutgoing(State.PLAY, 0x48, 0x4B);
|
||||
// Title
|
||||
registerOutgoing(State.PLAY, 0x48, 0x4B, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.VAR_INT); // Action
|
||||
handler(new PacketHandler() {
|
||||
@Override
|
||||
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)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
// New 0x4C - Stop Sound
|
||||
|
||||
// Sound Effect packet
|
||||
@ -500,7 +599,21 @@ public class Protocol1_13To1_12_2 extends Protocol {
|
||||
});
|
||||
}
|
||||
});
|
||||
registerOutgoing(State.PLAY, 0x4A, 0x4E);
|
||||
// Player list header and footer
|
||||
registerOutgoing(State.PLAY, 0x4A, 0x4E, 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)));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
registerOutgoing(State.PLAY, 0x4B, 0x4F);
|
||||
registerOutgoing(State.PLAY, 0x4C, 0x50);
|
||||
// Advancements
|
||||
@ -522,8 +635,8 @@ public class Protocol1_13To1_12_2 extends Protocol {
|
||||
|
||||
// Display data
|
||||
if (wrapper.passthrough(Type.BOOLEAN)) {
|
||||
wrapper.passthrough(Type.STRING); // Title
|
||||
wrapper.passthrough(Type.STRING); // Description
|
||||
wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING))); // Title
|
||||
wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING))); // Description
|
||||
Item icon = wrapper.read(Type.ITEM);
|
||||
InventoryPackets.toClient(icon);
|
||||
wrapper.write(Type.FLAT_ITEM, icon); // Translate item to flat item
|
||||
|
@ -2,15 +2,19 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import us.myles.ViaVersion.api.Via;
|
||||
import us.myles.ViaVersion.util.GsonUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -21,6 +25,8 @@ public class MappingData {
|
||||
public static Map<String, Integer[]> itemTags = new HashMap<>();
|
||||
public static Map<String, Integer[]> fluidTags = new HashMap<>();
|
||||
public static BiMap<Short, String> oldEnchantmentsIds = HashBiMap.create();
|
||||
public static Map<String, String> translateMapping = new HashMap<>();
|
||||
public static Map<String, String> mojangTranslation = new HashMap<>();
|
||||
public static EnchantmentMappings enchantmentMappings;
|
||||
public static SoundMappings soundMappings;
|
||||
public static BlockMappings blockMappings;
|
||||
@ -42,6 +48,30 @@ public class MappingData {
|
||||
enchantmentMappings = new EnchantmentMappingByteArray(mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments"));
|
||||
Via.getPlatform().getLogger().info("Loading sound mapping...");
|
||||
soundMappings = new SoundMappingShortArray(mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds"));
|
||||
Via.getPlatform().getLogger().info("Loading translation mappping");
|
||||
translateMapping = GsonUtil.getGson().fromJson(
|
||||
new InputStreamReader(
|
||||
MappingData.class.getClassLoader()
|
||||
.getResourceAsStream("assets/viaversion/data/mapping-lang-1.12-1.13.json")
|
||||
),
|
||||
(new TypeToken<Map<String, String>>(){}).getType());
|
||||
try {
|
||||
String[] lines;
|
||||
try (Reader reader = new InputStreamReader(MappingData.class.getClassLoader()
|
||||
.getResourceAsStream("mojang-translations/en_US.properties"), StandardCharsets.UTF_8)) {
|
||||
lines = CharStreams.toString(reader).split("\n");
|
||||
}
|
||||
for (String line : lines) {
|
||||
if (line.isEmpty()) continue;
|
||||
String[] keyAndTranslation = line.split("=", 2);
|
||||
if (keyAndTranslation.length != 2) continue;
|
||||
if (!translateMapping.containsKey(keyAndTranslation[0])) {
|
||||
translateMapping.put(keyAndTranslation[0], keyAndTranslation[1]);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonObject loadData(String name) {
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user