fix: full temporary fix for the events + final changes before merging to master
This commit is contained in:
@@ -1 +1 @@
|
||||
2908
|
||||
2919
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.chayapak1.chomens_bot.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -11,6 +12,9 @@ import java.util.Map;
|
||||
// for 1.21.5 SNBT components, not sure how performant this is
|
||||
// since i'm not good at writing performant code, but this should
|
||||
// reduce the size of the result being sent to the server by quite a bit
|
||||
|
||||
// with some tests on my laptop, the output of help command can take as much as 43 ms (truly advanced)
|
||||
// but some small components can be as low as 8764 nanoseconds
|
||||
public class SNBTUtilities {
|
||||
private static final String QUOTE = "'";
|
||||
|
||||
@@ -32,13 +36,13 @@ public class SNBTUtilities {
|
||||
}
|
||||
|
||||
// RIPPED from https://minecraft.wiki/w/NBT_format#Conversion_from_JSON
|
||||
public static String fromJson (final JsonElement json) {
|
||||
// this is for jsons that are made by adventure's GsonComponentSerializer ONLY.
|
||||
// i cannot guarantee if all json stuff will work, it's just the basics
|
||||
private static String fromJson (final JsonElement json) {
|
||||
if (json.isJsonPrimitive()) {
|
||||
final JsonPrimitive primitive = json.getAsJsonPrimitive();
|
||||
|
||||
if (primitive.isString()) return
|
||||
// don't wrap the string with quotes when not needed
|
||||
// like {abc:def} instead of {abc:'def'}
|
||||
needQuotes(primitive.getAsString())
|
||||
? QUOTE + escapeString(primitive.getAsString()) + QUOTE
|
||||
: primitive.getAsString();
|
||||
@@ -68,23 +72,63 @@ public class SNBTUtilities {
|
||||
for (final Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
|
||||
notEmpty = true;
|
||||
|
||||
// FIXME: only works in 1.21.5 adventure, at the time of writing this,
|
||||
// the 1.21.5 PR is still not merged yet, so i am doing
|
||||
// a lazy fix for the hover event `contents` being replaced with
|
||||
// `value` and the hoverEvent and clickEvent being snake case,
|
||||
// so some things work
|
||||
// FIXME: remove all these events fixer after adventure updates to 1.21.5, this is a SUPER lazy and temporary fix
|
||||
if (entry.getKey().equals("hoverEvent") && entry.getValue().isJsonObject()) {
|
||||
final JsonObject object = entry.getValue().getAsJsonObject();
|
||||
final String action = object.getAsJsonPrimitive("action").getAsString();
|
||||
|
||||
if (entry.getKey().equals("contents")) {
|
||||
stringBuilder.append("value");
|
||||
final JsonObject fixedObject = new JsonObject();
|
||||
|
||||
// no click_event replacement yet
|
||||
fixedObject.addProperty("action", action);
|
||||
|
||||
switch (action) {
|
||||
case "show_text" -> fixedObject.add("value", object.get("contents"));
|
||||
case "show_item" -> {
|
||||
final JsonObject contents = object.get("contents").getAsJsonObject();
|
||||
fixedObject.add("id", contents.get("id"));
|
||||
fixedObject.add("count", contents.get("count"));
|
||||
fixedObject.add("components", contents.get("components"));
|
||||
}
|
||||
case "show_entity" -> {
|
||||
final JsonObject contents = object.get("contents").getAsJsonObject();
|
||||
fixedObject.add("name", contents.get("name"));
|
||||
fixedObject.add("id", contents.get("id"));
|
||||
fixedObject.add("uuid", contents.get("uuid"));
|
||||
}
|
||||
}
|
||||
|
||||
stringBuilder.append("hover_event")
|
||||
.append(COLON)
|
||||
.append(fromJson(fixedObject))
|
||||
.append(COMMA);
|
||||
} else if (entry.getKey().equals("clickEvent") && entry.getValue().isJsonObject()) {
|
||||
final JsonObject object = entry.getValue().getAsJsonObject();
|
||||
|
||||
final String action = object.getAsJsonPrimitive("action").getAsString();
|
||||
final JsonElement value = object.getAsJsonPrimitive("value");
|
||||
|
||||
final JsonObject fixedObject = new JsonObject();
|
||||
|
||||
fixedObject.addProperty("action", action);
|
||||
|
||||
switch (action) {
|
||||
case "open_url" -> fixedObject.add("url", value);
|
||||
case "open_file" -> fixedObject.add("path", value);
|
||||
case "run_command", "suggest_command" -> fixedObject.add("command", value);
|
||||
case "change_page" -> fixedObject.add("page", value);
|
||||
case "copy_to_clipboard" -> fixedObject.add("value", value);
|
||||
}
|
||||
|
||||
stringBuilder.append("click_event")
|
||||
.append(COLON)
|
||||
.append(fromJson(fixedObject))
|
||||
.append(COMMA);
|
||||
} else {
|
||||
stringBuilder.append(convertCamelCaseToSnake(entry.getKey()));
|
||||
stringBuilder.append(entry.getKey()) // no escape :O (optional for adventure)
|
||||
.append(COLON)
|
||||
.append(fromJson(entry.getValue()))
|
||||
.append(COMMA);
|
||||
}
|
||||
stringBuilder.append(COLON);
|
||||
stringBuilder.append(fromJson(entry.getValue()));
|
||||
stringBuilder.append(COMMA);
|
||||
}
|
||||
|
||||
if (notEmpty) stringBuilder.deleteCharAt(stringBuilder.length() - 1); // removes comma
|
||||
@@ -101,6 +145,9 @@ public class SNBTUtilities {
|
||||
return string
|
||||
.replace("\\", "\\\\")
|
||||
.replace("'", "\\'")
|
||||
|
||||
// acts like gson, even though it increases size
|
||||
// i still want it to behave like this
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r")
|
||||
.replace("\t", "\\t")
|
||||
@@ -108,9 +155,16 @@ public class SNBTUtilities {
|
||||
.replace("\f", "\\f");
|
||||
}
|
||||
|
||||
// should this be in StringUtilities?
|
||||
// don't wrap the string with quotes when not needed
|
||||
// like {abc:def} instead of {abc:'def'}
|
||||
public static boolean needQuotes (final String string) {
|
||||
if (string.isBlank()) return true;
|
||||
if (
|
||||
string.isBlank()
|
||||
// booleans can get interpreted too
|
||||
|| string.equalsIgnoreCase("true")
|
||||
|| string.equalsIgnoreCase("false")
|
||||
) return true;
|
||||
|
||||
for (int i = 0; i < string.length(); i++) {
|
||||
final char c = string.charAt(i);
|
||||
// even though we can do like `abc123.among.us__69` without quotes
|
||||
@@ -119,22 +173,7 @@ public class SNBTUtilities {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// https://www.baeldung.com/java-camel-snake-case-conversion
|
||||
// 3. Manual Approach
|
||||
// FIXME: this is optional after adventure 1.21.5, remove this afterward
|
||||
// it's why this is here instead of in StringUtilities
|
||||
public static String convertCamelCaseToSnake (final String input) {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
for (final char c : input.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
result.append("_").append(Character.toLowerCase(c));
|
||||
} else {
|
||||
result.append(c);
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user