2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2024-01-01 12:39:45 +01:00
|
|
|
* Copyright (C) 2016-2024 ViaVersion and contributors
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2024-03-09 10:58:42 +01:00
|
|
|
package com.viaversion.viaversion.protocols.protocol1_12to1_11_1.rewriter;
|
2017-10-11 22:12:02 +02:00
|
|
|
|
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;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.Via;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.data.AchievementTranslationMapping;
|
2023-02-04 18:29:22 +01:00
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
|
2021-04-27 13:41:39 +02:00
|
|
|
import com.viaversion.viaversion.rewriter.ComponentRewriter;
|
2024-04-18 20:45:19 +02:00
|
|
|
import com.viaversion.viaversion.util.SerializerVersion;
|
2023-11-20 19:56:48 +01:00
|
|
|
import java.util.logging.Level;
|
2017-10-11 22:12:02 +02:00
|
|
|
|
|
|
|
public class TranslateRewriter {
|
2020-06-05 23:06:00 +02:00
|
|
|
|
2023-10-18 05:26:09 +02:00
|
|
|
private static final ComponentRewriter<ClientboundPackets1_9_3> ACHIEVEMENT_TEXT_REWRITER = new ComponentRewriter<ClientboundPackets1_9_3>(null, ComponentRewriter.ReadType.JSON) {
|
2020-08-12 16:38:21 +02:00
|
|
|
@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();
|
2020-08-12 17:16:50 +02:00
|
|
|
if (!action.equals("show_achievement")) {
|
|
|
|
super.handleHoverEvent(hoverEvent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:45:19 +02:00
|
|
|
String textValue = SerializerVersion.V1_9.toComponent(hoverEvent.get("value")).asUnformattedString();
|
2020-10-12 13:32:06 +02:00
|
|
|
|
|
|
|
if (AchievementTranslationMapping.get(textValue) == null) {
|
2020-08-12 17:16:50 +02:00
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-10-12 13:32:06 +02:00
|
|
|
if (textValue.startsWith("achievement")) {
|
|
|
|
namePart.addProperty("translate", textValue);
|
|
|
|
namePart.addProperty("color", AchievementTranslationMapping.isSpecial(textValue) ? "dark_purple" : "green");
|
2020-08-12 17:16:50 +02:00
|
|
|
typePart.addProperty("translate", "stats.tooltip.type.achievement");
|
|
|
|
JsonObject description = new JsonObject();
|
|
|
|
typePart.addProperty("italic", true);
|
2024-04-18 20:45:19 +02:00
|
|
|
description.addProperty("translate", textValue + ".desc");
|
2020-08-12 17:16:50 +02:00
|
|
|
baseArray.add(newLine);
|
|
|
|
baseArray.add(description);
|
2020-10-12 13:32:06 +02:00
|
|
|
} else if (textValue.startsWith("stat")) {
|
|
|
|
namePart.addProperty("translate", textValue);
|
2020-08-12 17:16:50 +02:00
|
|
|
namePart.addProperty("color", "gray");
|
|
|
|
typePart.addProperty("translate", "stats.tooltip.type.statistic");
|
|
|
|
typePart.addProperty("italic", true);
|
2020-08-12 16:38:21 +02:00
|
|
|
}
|
2020-08-12 17:16:50 +02:00
|
|
|
hoverEvent.addProperty("action", "show_text");
|
|
|
|
hoverEvent.add("value", baseArray);
|
|
|
|
} catch (Exception e) {
|
2024-04-18 20:45:19 +02:00
|
|
|
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
|
|
|
|
Via.getPlatform().getLogger().log(Level.WARNING, "Error rewriting show_achievement: " + hoverEvent, e);
|
|
|
|
}
|
2020-08-12 17:16:50 +02:00
|
|
|
JsonObject invalidText = new JsonObject();
|
|
|
|
invalidText.addProperty("text", "Invalid statistic/achievement!");
|
|
|
|
invalidText.addProperty("color", "red");
|
|
|
|
hoverEvent.addProperty("action", "show_text");
|
|
|
|
hoverEvent.add("value", invalidText);
|
2020-08-12 16:38:21 +02:00
|
|
|
}
|
|
|
|
super.handleHoverEvent(hoverEvent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-09 10:58:42 +01:00
|
|
|
public static void toClient(JsonElement element) {
|
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")) {
|
2023-02-04 18:29:22 +01:00
|
|
|
ACHIEVEMENT_TEXT_REWRITER.processText(obj);
|
2020-08-12 16:38:21 +02:00
|
|
|
}
|
2017-10-11 22:12:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-12 16:38:21 +02:00
|
|
|
|
2024-04-18 20:45:19 +02:00
|
|
|
}
|