Translation rewriting

This commit is contained in:
creeper123123321 2018-11-08 16:10:45 -02:00
parent d84c4365c9
commit d9eff7bb5c
No known key found for this signature in database
GPG Key ID: 0AC57D54786721D1
4 changed files with 1867 additions and 8 deletions

View File

@ -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.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.chat.ComponentSerializer; 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.ArrayList;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -115,4 +117,39 @@ public class ChatRewriter {
public static String jsonTextToLegacy(String value) { public static String jsonTextToLegacy(String value) {
return TextComponent.toLegacyText(ComponentSerializer.parse(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);
}
}
} }

View File

@ -132,6 +132,19 @@ public class Protocol1_13To1_12_2 extends Protocol {
// Outgoing packets // 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() { registerOutgoing(State.STATUS, 0x00, 0x00, new PacketRemapper() {
@Override @Override
public void registerMap() { 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 // WorldPackets 0x10 -> 0x0F
// Tab-Complete // Tab-Complete
@ -219,13 +261,39 @@ public class Protocol1_13To1_12_2 extends Protocol {
// New packet 0x11, declare commands // New packet 0x11, declare commands
registerOutgoing(State.PLAY, 0x11, 0x12); registerOutgoing(State.PLAY, 0x11, 0x12);
registerOutgoing(State.PLAY, 0x12, 0x13); 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 0x14 -> 0x15
// InventoryPackets 0x15 -> 0x16 // InventoryPackets 0x15 -> 0x16
// InventoryPackets 0x16 -> 0x17 // InventoryPackets 0x16 -> 0x17
registerOutgoing(State.PLAY, 0x17, 0x18); registerOutgoing(State.PLAY, 0x17, 0x18);
// WorldPackets 0x18 -> 0x19 // 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); registerOutgoing(State.PLAY, 0x1B, 0x1C);
// New packet 0x1D - NBT Query // New packet 0x1D - NBT Query
registerOutgoing(State.PLAY, 0x1C, 0x1E); 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, 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); registerOutgoing(State.PLAY, 0x2E, 0x30);
// New 0x31 - Face Player // New 0x31 - Face Player
registerOutgoing(State.PLAY, 0x2F, 0x32); 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, 0x46, 0x49);
registerOutgoing(State.PLAY, 0x47, 0x4A); 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 // New 0x4C - Stop Sound
// Sound Effect packet // 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, 0x4B, 0x4F);
registerOutgoing(State.PLAY, 0x4C, 0x50); registerOutgoing(State.PLAY, 0x4C, 0x50);
// Advancements // Advancements
@ -522,8 +635,8 @@ public class Protocol1_13To1_12_2 extends Protocol {
// Display data // Display data
if (wrapper.passthrough(Type.BOOLEAN)) { if (wrapper.passthrough(Type.BOOLEAN)) {
wrapper.passthrough(Type.STRING); // Title wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING))); // Title
wrapper.passthrough(Type.STRING); // Description wrapper.write(Type.STRING, ChatRewriter.processTranslate(wrapper.read(Type.STRING))); // Description
Item icon = wrapper.read(Type.ITEM); Item icon = wrapper.read(Type.ITEM);
InventoryPackets.toClient(icon); InventoryPackets.toClient(icon);
wrapper.write(Type.FLAT_ITEM, icon); // Translate item to flat item wrapper.write(Type.FLAT_ITEM, icon); // Translate item to flat item

View File

@ -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.BiMap;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import com.google.common.io.CharStreams;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.GsonUtil;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -21,6 +25,8 @@ public class MappingData {
public static Map<String, Integer[]> itemTags = new HashMap<>(); public static Map<String, Integer[]> itemTags = new HashMap<>();
public static Map<String, Integer[]> fluidTags = new HashMap<>(); public static Map<String, Integer[]> fluidTags = new HashMap<>();
public static BiMap<Short, String> oldEnchantmentsIds = HashBiMap.create(); 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 EnchantmentMappings enchantmentMappings;
public static SoundMappings soundMappings; public static SoundMappings soundMappings;
public static BlockMappings blockMappings; public static BlockMappings blockMappings;
@ -42,6 +48,30 @@ public class MappingData {
enchantmentMappings = new EnchantmentMappingByteArray(mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments")); enchantmentMappings = new EnchantmentMappingByteArray(mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments"));
Via.getPlatform().getLogger().info("Loading sound mapping..."); Via.getPlatform().getLogger().info("Loading sound mapping...");
soundMappings = new SoundMappingShortArray(mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds")); 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) { public static JsonObject loadData(String name) {

File diff suppressed because it is too large Load Diff