3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-08 17:20:24 +01:00

Convert component score values

Fixes #1817
Dieser Commit ist enthalten in:
KennyTV 2020-06-27 23:41:46 +02:00
Ursprung b2514a9e50
Commit 642d427783
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
3 geänderte Dateien mit 93 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -3,6 +3,10 @@ package us.myles.ViaVersion.api.rewriters;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive; 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; import us.myles.ViaVersion.util.GsonUtil;
// Packets using components: // Packets using components:
@ -21,7 +25,69 @@ import us.myles.ViaVersion.util.GsonUtil;
// declare commands // declare commands
// advancements // advancements
// update sign // 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 { 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) { public JsonElement processText(String value) {
JsonElement root = GsonUtil.getJsonParser().parse(value); JsonElement root = GsonUtil.getJsonParser().parse(value);

Datei anzeigen

@ -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() { registerOutgoing(ClientboundPackets1_15.CHAT_MESSAGE, new PacketRemapper() {
@Override @Override
public void registerMap() { 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 soundRewriter = new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id));
soundRewriter.registerSound(ClientboundPackets1_15.SOUND); soundRewriter.registerSound(ClientboundPackets1_15.SOUND);

Datei anzeigen

@ -1,6 +1,9 @@
package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data; package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; 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 us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import java.util.HashMap; import java.util.HashMap;
@ -9,7 +12,8 @@ import java.util.Map;
public class TranslationMappings extends ComponentRewriter { public class TranslationMappings extends ComponentRewriter {
private final Map<String, String> mappings = new HashMap<>(); 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_water", "Flowing Water");
mappings.put("block.minecraft.flowing_lava", "Flowing Lava"); mappings.put("block.minecraft.flowing_lava", "Flowing Lava");
mappings.put("block.minecraft.bed", "Bed"); mappings.put("block.minecraft.bed", "Bed");
@ -35,6 +39,23 @@ public class TranslationMappings extends ComponentRewriter {
mappings.put("biome.minecraft.nether", "Nether"); 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 @Override
protected void handleTranslate(JsonObject object, String translate) { protected void handleTranslate(JsonObject object, String translate) {
// A few keys were removed - manually set the text of relevant ones // A few keys were removed - manually set the text of relevant ones