mirror of
https://github.com/ViaVersion/ViaBackwards.git
synced 2025-02-14 01:31:37 +01:00
Refactor/optimize chat component rewriter
This commit is contained in:
parent
c2fb40b4e1
commit
aec46ecd38
@ -1,16 +1,15 @@
|
||||
package nl.matsv.viabackwards.api.rewriters;
|
||||
|
||||
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.ViaBackwards;
|
||||
import nl.matsv.viabackwards.api.BackwardsProtocol;
|
||||
import nl.matsv.viabackwards.api.data.VBMappingDataLoader;
|
||||
import us.myles.ViaVersion.api.remapper.PacketRemapper;
|
||||
import us.myles.ViaVersion.api.type.Type;
|
||||
import us.myles.ViaVersion.packets.State;
|
||||
import us.myles.ViaVersion.util.GsonUtil;
|
||||
import us.myles.viaversion.libs.gson.JsonElement;
|
||||
import us.myles.viaversion.libs.gson.JsonObject;
|
||||
import us.myles.viaversion.libs.gson.JsonPrimitive;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -50,7 +49,7 @@ public class TranslatableRewriter {
|
||||
protocol.out(State.LOGIN, 0x00, 0x00, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING))));
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING))));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -59,7 +58,7 @@ public class TranslatableRewriter {
|
||||
protocol.out(State.PLAY, oldId, newId, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING))));
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING))));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -68,7 +67,7 @@ public class TranslatableRewriter {
|
||||
protocol.out(State.PLAY, oldId, newId, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING))));
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING))));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -82,7 +81,7 @@ public class TranslatableRewriter {
|
||||
handler(wrapper -> {
|
||||
int action = wrapper.get(Type.VAR_INT, 0);
|
||||
if (action == 0 || action == 3) {
|
||||
wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING)));
|
||||
wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING)));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -95,7 +94,7 @@ public class TranslatableRewriter {
|
||||
public void registerMap() {
|
||||
map(Type.UNSIGNED_BYTE); // Id
|
||||
map(Type.STRING); // Window Type
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING))));
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING))));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -106,7 +105,7 @@ public class TranslatableRewriter {
|
||||
public void registerMap() {
|
||||
map(Type.VAR_INT); // Id
|
||||
map(Type.VAR_INT); // Window Type
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING))));
|
||||
handler(wrapper -> wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING))));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -119,7 +118,7 @@ public class TranslatableRewriter {
|
||||
if (wrapper.passthrough(Type.VAR_INT) == 2) {
|
||||
wrapper.passthrough(Type.VAR_INT);
|
||||
wrapper.passthrough(Type.INT);
|
||||
wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING)));
|
||||
wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING)));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -133,7 +132,7 @@ public class TranslatableRewriter {
|
||||
handler(wrapper -> {
|
||||
int action = wrapper.passthrough(Type.VAR_INT);
|
||||
if (action >= 0 && action <= 2) {
|
||||
wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING)));
|
||||
wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING)));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -145,45 +144,84 @@ public class TranslatableRewriter {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
handler(wrapper -> {
|
||||
wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING)));
|
||||
wrapper.write(Type.STRING, processTranslate(wrapper.read(Type.STRING)));
|
||||
wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING)));
|
||||
wrapper.write(Type.STRING, processText(wrapper.read(Type.STRING)));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public 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);
|
||||
public String processText(String value) {
|
||||
JsonElement root = GsonUtil.getJsonParser().parse(value);
|
||||
processText(root);
|
||||
return root.toString();
|
||||
}
|
||||
|
||||
protected void processTranslate(BaseComponent component) {
|
||||
if (component == null) return;
|
||||
if (component instanceof TranslatableComponent) {
|
||||
TranslatableComponent translatableComponent = (TranslatableComponent) component;
|
||||
String oldTranslate = translatableComponent.getTranslate();
|
||||
String newTranslate = newTranslatables.get(oldTranslate);
|
||||
if (newTranslate != null) {
|
||||
translatableComponent.setTranslate(newTranslate);
|
||||
}
|
||||
if (translatableComponent.getWith() != null) {
|
||||
for (BaseComponent baseComponent : translatableComponent.getWith()) {
|
||||
processTranslate(baseComponent);
|
||||
}
|
||||
protected void processText(JsonElement element) {
|
||||
if (element == null || element.isJsonNull()) return;
|
||||
if (element.isJsonArray()) {
|
||||
processAsArray(element);
|
||||
return;
|
||||
}
|
||||
if (element.isJsonPrimitive()) {
|
||||
handleText(element.getAsJsonPrimitive());
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject object = element.getAsJsonObject();
|
||||
JsonPrimitive text = object.getAsJsonPrimitive("text");
|
||||
if (text != null) {
|
||||
handleText(text);
|
||||
}
|
||||
|
||||
JsonElement translate = object.get("translate");
|
||||
if (translate != null) {
|
||||
handleTranslate(object, translate.getAsString());
|
||||
|
||||
JsonElement with = object.get("with");
|
||||
if (with != null) {
|
||||
processAsArray(with);
|
||||
}
|
||||
}
|
||||
if (component.getHoverEvent() != null) {
|
||||
for (BaseComponent baseComponent : component.getHoverEvent().getValue()) {
|
||||
processTranslate(baseComponent);
|
||||
|
||||
JsonElement extra = object.get("extra");
|
||||
if (extra != null) {
|
||||
processAsArray(extra);
|
||||
}
|
||||
|
||||
JsonObject hoverEvent = object.getAsJsonObject("hoverEvent");
|
||||
if (hoverEvent != null) {
|
||||
handleHoverEvent(hoverEvent);
|
||||
}
|
||||
}
|
||||
|
||||
protected void handleText(JsonPrimitive text) {
|
||||
// In case this is needed in the future
|
||||
}
|
||||
|
||||
protected void handleTranslate(JsonObject root, String translate) {
|
||||
String newTranslate = newTranslatables.get(translate);
|
||||
if (newTranslate != null) {
|
||||
root.addProperty("translate", newTranslate);
|
||||
}
|
||||
}
|
||||
|
||||
protected void handleHoverEvent(JsonObject hoverEvent) {
|
||||
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
|
||||
if (action.equals("show_text")) {
|
||||
JsonElement value = hoverEvent.get("value");
|
||||
processText(value != null ? value : hoverEvent.get("contents"));
|
||||
} else if (action.equals("show_entity")) {
|
||||
JsonObject contents = hoverEvent.getAsJsonObject("contents");
|
||||
if (contents != null) {
|
||||
processText(contents.get("name"));
|
||||
}
|
||||
}
|
||||
if (component.getExtra() != null) {
|
||||
for (BaseComponent baseComponent : component.getExtra()) {
|
||||
processTranslate(baseComponent);
|
||||
}
|
||||
}
|
||||
|
||||
private void processAsArray(JsonElement element) {
|
||||
for (JsonElement jsonElement : element.getAsJsonArray()) {
|
||||
processText(jsonElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,6 @@
|
||||
|
||||
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 nl.matsv.viabackwards.ViaBackwards;
|
||||
import nl.matsv.viabackwards.api.BackwardsProtocol;
|
||||
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
|
||||
@ -33,6 +31,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper;
|
||||
import us.myles.ViaVersion.packets.State;
|
||||
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
|
||||
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
|
||||
import us.myles.viaversion.libs.gson.JsonObject;
|
||||
|
||||
public class Protocol1_12_2To1_13 extends BackwardsProtocol {
|
||||
|
||||
@ -53,30 +52,10 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol {
|
||||
|
||||
TranslatableRewriter translatableRewriter = new TranslatableRewriter(this) {
|
||||
@Override
|
||||
protected void processTranslate(BaseComponent component) {
|
||||
if (component == null) return;
|
||||
if (component instanceof TranslatableComponent) {
|
||||
TranslatableComponent translatableComponent = (TranslatableComponent) component;
|
||||
String oldTranslate = translatableComponent.getTranslate();
|
||||
String newTranslate = newTranslatables.get(oldTranslate);
|
||||
if (newTranslate != null || (newTranslate = BackwardsMappings.translateMappings.get(oldTranslate)) != 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);
|
||||
}
|
||||
protected void handleTranslate(JsonObject root, String translate) {
|
||||
String newTranslate = newTranslatables.get(translate);
|
||||
if (newTranslate != null || (newTranslate = BackwardsMappings.translateMappings.get(translate)) != null) {
|
||||
root.addProperty("translate", newTranslate);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -102,7 +102,7 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol {
|
||||
public void handle(PacketWrapper wrapper) throws Exception {
|
||||
int action = wrapper.get(Type.VAR_INT, 0);
|
||||
if (action == 0 || action == 3) {
|
||||
wrapper.write(Type.STRING, translatableRewriter.processTranslate(wrapper.read(Type.STRING)));
|
||||
wrapper.write(Type.STRING, translatableRewriter.processText(wrapper.read(Type.STRING)));
|
||||
if (action == 0) {
|
||||
wrapper.passthrough(Type.FLOAT);
|
||||
wrapper.passthrough(Type.VAR_INT);
|
||||
|
@ -44,7 +44,7 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol {
|
||||
registerOutgoing(State.PLAY, 0x0F, 0x0F, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
handler(wrapper -> wrapper.write(Type.STRING, translatableRewriter.processTranslate(wrapper.read(Type.STRING))));
|
||||
handler(wrapper -> wrapper.write(Type.STRING, translatableRewriter.processText(wrapper.read(Type.STRING))));
|
||||
map(Type.BYTE);
|
||||
map(Type.UUID, Type.NOTHING); // Sender
|
||||
}
|
||||
@ -56,7 +56,7 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol {
|
||||
public void registerMap() {
|
||||
map(Type.VAR_INT); // Window Id
|
||||
map(Type.VAR_INT); // Window Type
|
||||
handler(wrapper -> wrapper.write(Type.STRING, translatableRewriter.processTranslate(wrapper.read(Type.STRING))));
|
||||
handler(wrapper -> wrapper.write(Type.STRING, translatableRewriter.processText(wrapper.read(Type.STRING))));
|
||||
handler(wrapper -> {
|
||||
int windowType = wrapper.get(Type.VAR_INT, 1);
|
||||
if (windowType == 20) { // Smithing table
|
||||
|
@ -3,7 +3,6 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.chat;
|
||||
import nl.matsv.viabackwards.api.BackwardsProtocol;
|
||||
import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter;
|
||||
import us.myles.ViaVersion.util.GsonUtil;
|
||||
import us.myles.viaversion.libs.gson.JsonArray;
|
||||
import us.myles.viaversion.libs.gson.JsonElement;
|
||||
import us.myles.viaversion.libs.gson.JsonObject;
|
||||
import us.myles.viaversion.libs.gson.JsonPrimitive;
|
||||
@ -34,63 +33,22 @@ public class TranslatableRewriter1_16 extends TranslatableRewriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processTranslate(String value) {
|
||||
public String processText(String value) {
|
||||
JsonElement root = GsonUtil.getJsonParser().parse(value);
|
||||
if (!root.isJsonObject()) {
|
||||
return super.processTranslate(value);
|
||||
return super.processText(value);
|
||||
}
|
||||
|
||||
processTranslate(root);
|
||||
return super.processTranslate(root.toString());
|
||||
processText(root);
|
||||
return super.processText(root.toString());
|
||||
}
|
||||
|
||||
private void processTranslate(JsonElement value) {
|
||||
protected void processText(JsonElement value) {
|
||||
super.processText(value);
|
||||
|
||||
if (!value.isJsonObject()) return;
|
||||
|
||||
// Iterate all sub components
|
||||
JsonObject object = value.getAsJsonObject();
|
||||
JsonArray with = object.getAsJsonArray("with");
|
||||
if (with != null) {
|
||||
for (JsonElement element : with) {
|
||||
processTranslate(element);
|
||||
}
|
||||
}
|
||||
JsonArray extra = object.getAsJsonArray("extra");
|
||||
if (extra != null) {
|
||||
for (JsonElement element : extra) {
|
||||
processTranslate(element);
|
||||
}
|
||||
}
|
||||
|
||||
// Hoverevent structure changed
|
||||
JsonObject hoverEvent = object.getAsJsonObject("hoverEvent");
|
||||
if (hoverEvent != null) {
|
||||
JsonElement contentsElement = hoverEvent.remove("contents");
|
||||
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
|
||||
if (contentsElement != null) {
|
||||
// show_text as chat component
|
||||
// show_entity and show_item serialized as nbt
|
||||
if (action.equals("show_text")) {
|
||||
processTranslate(contentsElement);
|
||||
hoverEvent.add("value", contentsElement);
|
||||
} else if (action.equals("show_item")) {
|
||||
JsonObject item = contentsElement.getAsJsonObject();
|
||||
JsonElement count = item.remove("count");
|
||||
item.addProperty("Count", count != null ? count.getAsByte() : 1);
|
||||
|
||||
hoverEvent.addProperty("value", TagSerializer.toString(item));
|
||||
} else if (action.equals("show_entity")) {
|
||||
JsonObject entity = contentsElement.getAsJsonObject();
|
||||
if (entity.has("name")) {
|
||||
entity.addProperty("name", entity.getAsJsonObject("name").toString());
|
||||
}
|
||||
|
||||
JsonObject hoverObject = new JsonObject();
|
||||
hoverObject.addProperty("text", TagSerializer.toString(entity));
|
||||
hoverEvent.add("value", hoverObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// c o l o r s
|
||||
JsonPrimitive color = object.getAsJsonPrimitive("color");
|
||||
@ -104,6 +62,42 @@ public class TranslatableRewriter1_16 extends TranslatableRewriter {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHoverEvent(JsonObject hoverEvent) {
|
||||
// Don't call super, convert and process contents here
|
||||
JsonElement contentsElement = hoverEvent.remove("contents");
|
||||
if (contentsElement == null) return;
|
||||
|
||||
// show_text as chat component
|
||||
// show_entity and show_item serialized as nbt
|
||||
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
|
||||
switch (action) {
|
||||
case "show_text":
|
||||
processText(contentsElement);
|
||||
hoverEvent.add("value", contentsElement);
|
||||
break;
|
||||
case "show_item":
|
||||
JsonObject item = contentsElement.getAsJsonObject();
|
||||
JsonElement count = item.remove("count");
|
||||
item.addProperty("Count", count != null ? count.getAsByte() : 1);
|
||||
|
||||
hoverEvent.addProperty("value", TagSerializer.toString(item));
|
||||
break;
|
||||
case "show_entity":
|
||||
JsonObject entity = contentsElement.getAsJsonObject();
|
||||
JsonObject name = entity.getAsJsonObject("name");
|
||||
if (name != null) {
|
||||
processText(name);
|
||||
entity.addProperty("name", name.toString());
|
||||
}
|
||||
|
||||
JsonObject hoverObject = new JsonObject();
|
||||
hoverObject.addProperty("text", TagSerializer.toString(entity));
|
||||
hoverEvent.add("value", hoverObject);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private String getClosestChatColor(int rgb) {
|
||||
int r = (rgb >> 16) & 0xFF;
|
||||
int g = (rgb >> 8) & 0xFF;
|
||||
|
Loading…
Reference in New Issue
Block a user