Handle 1.13->1.12.2 translatable key changes

This commit is contained in:
KennyTV 2020-01-16 11:09:26 +01:00
parent 8878a18108
commit b00ae1b701
4 changed files with 149 additions and 17 deletions

View File

@ -14,11 +14,11 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public abstract class SoundRewriter<T extends BackwardsProtocol> extends Rewriter<T> {
private Map<Integer, SoundData> soundRewrites = new ConcurrentHashMap<>();
private final Map<Integer, SoundData> soundRewrites = new HashMap<>();
public SoundData added(int id, int replacement) {
return added(id, replacement, -1);
@ -38,8 +38,9 @@ public abstract class SoundRewriter<T extends BackwardsProtocol> extends Rewrite
public int handleSounds(int soundId) {
int newSoundId = soundId;
if (soundRewrites.containsKey(soundId))
return soundRewrites.get(soundId).getReplacementSound();
SoundData data = soundRewrites.get(soundId);
if (data != null) return data.getReplacementSound();
for (Map.Entry<Integer, SoundData> entry : soundRewrites.entrySet()) {
if (soundId > entry.getKey()) {
if (entry.getValue().isAdded()) {
@ -53,20 +54,18 @@ public abstract class SoundRewriter<T extends BackwardsProtocol> extends Rewrite
}
public boolean hasPitch(int soundId) {
if (soundRewrites.containsKey(soundId))
return soundRewrites.get(soundId).isChangePitch();
return false;
SoundData data = soundRewrites.get(soundId);
return data != null && data.isChangePitch();
}
public float handlePitch(int soundId) {
if (soundRewrites.containsKey(soundId))
return soundRewrites.get(soundId).getNewPitch();
return 1f;
SoundData data = soundRewrites.get(soundId);
return data != null ? data.getNewPitch() : 1F;
}
@Data
@AllArgsConstructor
public class SoundData {
public static class SoundData {
private int replacementSound;
private boolean changePitch = false;
private float newPitch = 1f;

View File

@ -28,6 +28,7 @@ import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.providers.ViaProviders;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
@ -50,13 +51,10 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol {
// Thanks to https://wiki.vg/index.php?title=Pre-release_protocol&oldid=14150
out(State.PLAY, 0x0E, 0x0F); // Chat Message (clientbound)
out(State.PLAY, 0x11, -1, cancel()); // Declare Commands TODO NEW
out(State.PLAY, 0x12, 0x11); // Confirm Transaction (clientbound)
out(State.PLAY, 0x13, 0x12); // Close Window (clientbound)
out(State.PLAY, 0x14, 0x13); // Open Window
out(State.PLAY, 0x16, 0x15); // Window Property
out(State.PLAY, 0x1B, 0x1A); // Disconnect (play)
out(State.PLAY, 0x1C, 0x1B); // Entity Status
out(State.PLAY, 0x1D, -1, cancel()); // NBT Query Response (client won't send a request, so the server should not answer)
out(State.PLAY, 0x1E, 0x1C); // Explosion
@ -71,7 +69,6 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol {
out(State.PLAY, 0x2C, 0x2A); // Open Sign Editor
out(State.PLAY, 0x2D, 0x2B, cancel()); // Craft Recipe Response TODO MODIFIED
out(State.PLAY, 0x2E, 0x2C); // Player Abilities (clientbound)
out(State.PLAY, 0x2F, 0x2D); // Combat Event
out(State.PLAY, 0x33, 0x30); // Use Bed
out(State.PLAY, 0x34, 0x31, cancel()); // Unlock Recipes TODO MODIFIED
out(State.PLAY, 0x36, 0x33); // Remove Entity Effect
@ -90,8 +87,6 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol {
out(State.PLAY, 0x48, 0x45); // Update Score
out(State.PLAY, 0x49, 0x46); // Spawn Position
out(State.PLAY, 0x4A, 0x47); // Time Update
out(State.PLAY, 0x4B, 0x48); // Title
out(State.PLAY, 0x4E, 0x4A); // Player List Header And Footer
out(State.PLAY, 0x4F, 0x4B); // Collect Item
out(State.PLAY, 0x50, 0x4C); // Entity Teleport
out(State.PLAY, 0x51, 0x4D, cancel()); // Advancements
@ -124,6 +119,94 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol {
in(State.PLAY, 0x28, 0x1E); // Spectate
in(State.PLAY, 0x29, 0x1F); // Player Block Placement
in(State.PLAY, 0x2A, 0x20); // Use Item
// Handle translation key changes
out(State.LOGIN, 0x00, 0x00, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING))));
}
});
// Bossbar
out(State.LOGIN, 0x0C, 0x0C, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UUID);
map(Type.VAR_INT);
handler(wrapper -> {
int action = wrapper.get(Type.VAR_INT, 0);
if (action == 0 || action == 3) {
wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING)));
}
});
}
});
// Chat Message
out(State.PLAY, 0x0E, 0x0F, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING))));
}
});
// Open Window
out(State.PLAY, 0x14, 0x13, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // Id
map(Type.STRING); // Window Type
handler(wrapper -> wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING))));
}
});
// Disconnect
out(State.PLAY, 0x1B, 0x1A, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING))));
}
});
// Combat Event
out(State.PLAY, 0x2F, 0x2D, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
if (wrapper.passthrough(Type.VAR_INT) == 2) {
wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.INT);
wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING)));
}
});
}
});
// Title
out(State.PLAY, 0x4B, 0x48, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int action = wrapper.passthrough(Type.VAR_INT);
if (action >= 0 && action <= 2) {
wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING)));
}
});
}
});
// Player List Header And Footer
out(State.PLAY, 0x4E, 0x4A, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING)));
wrapper.write(Type.STRING, TranslationRewriter.processTranslate(wrapper.read(Type.STRING)));
});
}
});
}
@Override

View File

@ -0,0 +1,45 @@
package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings;
// Slightly changed methods of the ChatRewriter
public class TranslationRewriter {
public static String processTranslate(String value) {
BaseComponent[] components = ComponentSerializer.parse(value);
for (BaseComponent component : components) {
processTranslate(component);
}
return components.length == 1 ? ComponentSerializer.toString(components[0]) : ComponentSerializer.toString(components);
}
private static void processTranslate(BaseComponent component) {
if (component == null) return;
if (component instanceof TranslatableComponent) {
TranslatableComponent translatableComponent = (TranslatableComponent) component;
String oldTranslate = translatableComponent.getTranslate();
String newTranslate = BackwardsMappings.translateMappings.get(oldTranslate);
if (newTranslate != null) {
translatableComponent.setTranslate(newTranslate);
}
if (translatableComponent.getWith() != null) {
for (BaseComponent baseComponent : translatableComponent.getWith()) {
processTranslate(baseComponent);
}
}
}
if (component.getHoverEvent() != null) {
for (BaseComponent baseComponent : component.getHoverEvent().getValue()) {
processTranslate(baseComponent);
}
}
if (component.getExtra() != null) {
for (BaseComponent baseComponent : component.getExtra()) {
processTranslate(baseComponent);
}
}
}
}

View File

@ -16,6 +16,7 @@ import nl.matsv.viabackwards.api.data.VBMappings;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.Mappings;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.StatisticMappings;
import us.myles.viaversion.libs.gson.JsonElement;
import us.myles.viaversion.libs.gson.JsonObject;
@ -27,6 +28,7 @@ import java.util.Map;
public class BackwardsMappings {
public static final Map<Integer, String> statisticMappings = new HashMap<>();
public static final Map<String, String> translateMappings = new HashMap<>();
public static BlockMappingsShortArray blockMappings;
public static Mappings soundMappings;
@ -43,6 +45,9 @@ public class BackwardsMappings {
for (Map.Entry<String, Integer> entry : StatisticMappings.statistics.entrySet()) {
statisticMappings.put(entry.getValue(), entry.getKey());
}
for (Map.Entry<String, String> entry : MappingData.translateMapping.entrySet()) {
translateMappings.put(entry.getValue(), entry.getKey());
}
}
// Has lots of compat layers, so we can't use the default Via method