2017-10-11 22:12:02 +02:00
|
|
|
package us.myles.ViaVersion.protocols.protocol1_12to1_11_1;
|
|
|
|
|
2020-08-12 16:38:21 +02:00
|
|
|
import com.google.gson.JsonArray;
|
2017-10-11 22:12:02 +02:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
2020-08-12 16:38:21 +02:00
|
|
|
import us.myles.ViaVersion.api.Via;
|
2017-10-11 22:12:02 +02:00
|
|
|
import us.myles.ViaVersion.api.data.UserConnection;
|
2020-08-12 16:38:21 +02:00
|
|
|
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
|
|
|
|
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.data.AchievementTranslationMapping;
|
2017-10-11 22:12:02 +02:00
|
|
|
|
|
|
|
public class TranslateRewriter {
|
2020-06-05 23:06:00 +02:00
|
|
|
|
2020-08-12 16:38:21 +02:00
|
|
|
private final static ComponentRewriter achievementTextRewriter = new ComponentRewriter() {
|
|
|
|
@Override
|
|
|
|
protected void handleTranslate(JsonObject object, String translate) {
|
|
|
|
String text = AchievementTranslationMapping.get(translate);
|
|
|
|
if (text != null) {
|
|
|
|
object.addProperty("translate", text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void handleHoverEvent(JsonObject hoverEvent) {
|
|
|
|
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
|
|
|
|
if (action.equals("show_achievement")) {
|
|
|
|
String value = hoverEvent.getAsJsonPrimitive("value").getAsString();
|
|
|
|
if (AchievementTranslationMapping.get(value) != null) {
|
|
|
|
try {
|
|
|
|
JsonObject newLine = new JsonObject();
|
|
|
|
newLine.addProperty("text", "\n");
|
|
|
|
JsonArray baseArray = new JsonArray();
|
|
|
|
baseArray.add("");
|
|
|
|
JsonObject namePart = new JsonObject();
|
|
|
|
JsonObject typePart = new JsonObject();
|
|
|
|
baseArray.add(namePart);
|
|
|
|
baseArray.add(newLine);
|
|
|
|
baseArray.add(typePart);
|
|
|
|
if (value.startsWith("achievement")) {
|
|
|
|
namePart.addProperty("translate", value);
|
|
|
|
namePart.addProperty("color", AchievementTranslationMapping.isSpecial(value) ? "dark_purple" : "green");
|
|
|
|
typePart.addProperty("translate", "stats.tooltip.type.achievement");
|
|
|
|
JsonObject description = new JsonObject();
|
|
|
|
typePart.addProperty("italic", true);
|
|
|
|
description.addProperty("translate", value + ".desc");
|
|
|
|
baseArray.add(newLine);
|
|
|
|
baseArray.add(description);
|
|
|
|
} else if (value.startsWith("stat")) {
|
|
|
|
namePart.addProperty("translate", value);
|
|
|
|
namePart.addProperty("color", "gray");
|
|
|
|
typePart.addProperty("translate", "stats.tooltip.type.statistic");
|
|
|
|
typePart.addProperty("italic", true);
|
|
|
|
}
|
|
|
|
hoverEvent.addProperty("action", "show_text");
|
|
|
|
hoverEvent.add("value", baseArray);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Via.getPlatform().getLogger().warning("Error rewriting show_achievement: " + hoverEvent);
|
|
|
|
e.printStackTrace();
|
|
|
|
JsonObject invalidText = new JsonObject();
|
|
|
|
invalidText.addProperty("text", "Invalid statistic/achievement!");
|
|
|
|
invalidText.addProperty("color", "red");
|
|
|
|
hoverEvent.addProperty("action", "show_text");
|
|
|
|
hoverEvent.add("value", invalidText);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JsonObject invalidText = new JsonObject();
|
|
|
|
invalidText.addProperty("text", "Invalid statistic/achievement!");
|
|
|
|
invalidText.addProperty("color", "red");
|
|
|
|
hoverEvent.addProperty("action", "show_text");
|
|
|
|
hoverEvent.add("value", invalidText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.handleHoverEvent(hoverEvent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public static void toClient(JsonElement element, UserConnection user) {
|
2017-10-11 22:12:02 +02:00
|
|
|
if (element instanceof JsonObject) {
|
|
|
|
JsonObject obj = (JsonObject) element;
|
2020-06-05 23:06:00 +02:00
|
|
|
JsonElement translate = obj.get("translate");
|
|
|
|
if (translate != null) {
|
2020-08-12 16:38:21 +02:00
|
|
|
if (translate.getAsString().startsWith("chat.type.achievement")) {
|
|
|
|
achievementTextRewriter.processText(obj);
|
|
|
|
}
|
2017-10-11 22:12:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-12 16:38:21 +02:00
|
|
|
|
2017-10-11 22:12:02 +02:00
|
|
|
}
|