Convert component score values

Fixes #1817
This commit is contained in:
KennyTV 2020-06-27 23:41:46 +02:00
parent b2514a9e50
commit 642d427783
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
3 changed files with 93 additions and 2 deletions

View File

@ -3,6 +3,10 @@ package us.myles.ViaVersion.api.rewriters;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import us.myles.ViaVersion.api.protocol.ClientboundPacketType;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.util.GsonUtil;
// Packets using components:
@ -21,7 +25,69 @@ import us.myles.ViaVersion.util.GsonUtil;
// declare commands
// advancements
// update sign
/**
* Handles json chat components, containing methods to override certain parts of the handling.
* Also contains methods to register a few of the packets using components.
*/
public class ComponentRewriter {
protected final Protocol protocol;
public ComponentRewriter(Protocol protocol) {
this.protocol = protocol;
}
/**
* Use empty constructor if no packet registering is needed.
*/
public ComponentRewriter() {
this.protocol = null;
}
public void registerBossBar(ClientboundPacketType packetType) {
protocol.registerOutgoing(packetType, 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) {
processText(wrapper.passthrough(Type.COMPONENT));
}
});
}
});
}
public void registerCombatEvent(ClientboundPacketType packetType) {
protocol.registerOutgoing(packetType, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
if (wrapper.passthrough(Type.VAR_INT) == 2) {
wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.INT);
processText(wrapper.passthrough(Type.COMPONENT));
}
});
}
});
}
public void registerTitle(ClientboundPacketType packetType) {
protocol.registerOutgoing(packetType, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int action = wrapper.passthrough(Type.VAR_INT);
if (action >= 0 && action <= 2) {
processText(wrapper.passthrough(Type.COMPONENT));
}
});
}
});
}
public JsonElement processText(String value) {
JsonElement root = GsonUtil.getJsonParser().parse(value);

View File

@ -99,7 +99,8 @@ public class Protocol1_16To1_15_2 extends Protocol<ClientboundPackets1_15, Clien
}
});
ComponentRewriter componentRewriter = new TranslationMappings();
ComponentRewriter componentRewriter = new TranslationMappings(this);
// Handle (relevant) component cases for translatable and score changes
registerOutgoing(ClientboundPackets1_15.CHAT_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
@ -111,6 +112,9 @@ public class Protocol1_16To1_15_2 extends Protocol<ClientboundPackets1_15, Clien
});
}
});
componentRewriter.registerBossBar(ClientboundPackets1_15.BOSSBAR);
componentRewriter.registerTitle(ClientboundPackets1_15.TITLE);
componentRewriter.registerCombatEvent(ClientboundPackets1_15.COMBAT_EVENT);
SoundRewriter soundRewriter = new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id));
soundRewriter.registerSound(ClientboundPackets1_15.SOUND);

View File

@ -1,6 +1,9 @@
package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import java.util.HashMap;
@ -9,7 +12,8 @@ import java.util.Map;
public class TranslationMappings extends ComponentRewriter {
private final Map<String, String> mappings = new HashMap<>();
public TranslationMappings() {
public TranslationMappings(Protocol protocol) {
super(protocol);
mappings.put("block.minecraft.flowing_water", "Flowing Water");
mappings.put("block.minecraft.flowing_lava", "Flowing Lava");
mappings.put("block.minecraft.bed", "Bed");
@ -35,6 +39,23 @@ public class TranslationMappings extends ComponentRewriter {
mappings.put("biome.minecraft.nether", "Nether");
}
@Override
public void processText(JsonElement element) {
super.processText(element);
if (element == null || !element.isJsonObject()) return;
// Score components no longer contain value fields
JsonObject object = element.getAsJsonObject();
JsonObject score = object.getAsJsonObject("score");
if (score == null || object.has("text")) return;
JsonPrimitive value = score.getAsJsonPrimitive("value");
if (value != null) {
object.remove("score");
object.add("text", value);
}
}
@Override
protected void handleTranslate(JsonObject object, String translate) {
// A few keys were removed - manually set the text of relevant ones