fix: make the events work with new adventure and improve SNBTUtilities a bit

This commit is contained in:
ChomeNS
2025-05-19 18:39:18 +07:00
parent b4f0958202
commit 0f28e8c900
10 changed files with 42 additions and 84 deletions

View File

@@ -1,6 +1,7 @@
package me.chayapak1.chomens_bot.chomeNSMod;
import io.netty.buffer.ByteBuf;
import me.chayapak1.chomens_bot.util.SNBTUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
@@ -46,7 +47,7 @@ public class Types {
}
public static void writeComponent (final ByteBuf buf, final Component component) {
final String stringJSON = GsonComponentSerializer.gson().serialize(component);
final String stringJSON = SNBTUtilities.fromComponent(false, component);
writeString(buf, stringJSON);
}

View File

@@ -65,7 +65,7 @@ public class BotBossBar extends BossBar {
this.title = title;
final String serialized = SNBTUtilities.fromComponent(bot, title);
final String serialized = SNBTUtilities.fromComponent(bot.options.useSNBTComponents, title);
bot.core.run("minecraft:bossbar set " + id + " name " + serialized);

View File

@@ -2,8 +2,8 @@ package me.chayapak1.chomens_bot.evalFunctions;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.data.eval.EvalFunction;
import me.chayapak1.chomens_bot.util.SNBTUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@@ -27,6 +27,12 @@ public class CoreFunction extends EvalFunction {
final CompletableFuture<Component> future = bot.core.runTracked(command);
return new Output(GsonComponentSerializer.gson().serialize(future.get(1, TimeUnit.SECONDS)), true);
return new Output(
SNBTUtilities.fromComponent(
false,
future.get(1, TimeUnit.SECONDS)
),
true
);
}
}

View File

@@ -3,8 +3,8 @@ package me.chayapak1.chomens_bot.evalFunctions;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.data.eval.EvalFunction;
import me.chayapak1.chomens_bot.data.listener.Listener;
import me.chayapak1.chomens_bot.util.SNBTUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
public class GetLatestChatMessageFunction extends EvalFunction implements Listener {
private String latestMessage = "";
@@ -17,7 +17,7 @@ public class GetLatestChatMessageFunction extends EvalFunction implements Listen
@Override
public boolean onSystemMessageReceived (final Component component, final String string, final String ansi) {
latestMessage = GsonComponentSerializer.gson().serialize(component);
latestMessage = SNBTUtilities.fromComponent(false, component);
return true;
}

View File

@@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.data.eval.EvalFunction;
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import me.chayapak1.chomens_bot.util.SNBTUtilities;
import java.util.List;
@@ -26,7 +26,7 @@ public class GetPlayerListFunction extends EvalFunction {
object.addProperty("uuid", entry.profile.getIdAsString());
object.addProperty("username", entry.profile.getName());
if (entry.displayName != null)
object.addProperty("displayName", GsonComponentSerializer.gson().serialize(entry.displayName));
object.addProperty("displayName", SNBTUtilities.fromComponent(false, entry.displayName));
array.add(object);
}

View File

@@ -5,8 +5,8 @@ import me.chayapak1.chomens_bot.data.bossbar.BossBar;
import me.chayapak1.chomens_bot.data.bossbar.BotBossBar;
import me.chayapak1.chomens_bot.data.listener.Listener;
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
import net.kyori.adventure.text.Component;
import me.chayapak1.chomens_bot.util.SNBTUtilities;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import org.geysermc.mcprotocollib.network.packet.Packet;
@@ -196,7 +196,7 @@ public class BossbarManagerPlugin implements Listener {
final Component title = bossBar.secret;
final String stringTitle = SNBTUtilities.fromComponent(bot, title);
final String stringTitle = SNBTUtilities.fromComponent(bot.options.useSNBTComponents, title);
bot.core.run("minecraft:bossbar add " + name + " " + stringTitle);

View File

@@ -391,7 +391,7 @@ public class ChatPlugin implements Listener {
final String stringified = ComponentUtilities.stringifyLegacy(component).replace("§", "&");
send(stringified);
} else {
bot.core.run("minecraft:tellraw " + targets + " " + SNBTUtilities.fromComponent(bot, component));
bot.core.run("minecraft:tellraw " + targets + " " + SNBTUtilities.fromComponent(bot.options.useSNBTComponents, component));
}
}
@@ -401,7 +401,7 @@ public class ChatPlugin implements Listener {
public void actionBar (final Component component, final String targets) {
if (bot.options.useChat) return;
bot.core.run("minecraft:title " + targets + " actionbar " + SNBTUtilities.fromComponent(bot, component));
bot.core.run("minecraft:title " + targets + " actionbar " + SNBTUtilities.fromComponent(bot.options.useSNBTComponents, component));
}
public void actionBar (final Component component, final UUID uuid) { actionBar(component, UUIDUtilities.selector(uuid)); }

View File

@@ -141,7 +141,7 @@ public class ScreensharePlugin {
}
for (int i = 0; i < names.size(); i++) {
bot.core.run("minecraft:data merge entity @e[tag=" + tags.get(i) + ",limit=1] {text:" + SNBTUtilities.fromComponent(bot, names.get(i)) + "}");
bot.core.run("minecraft:data merge entity @e[tag=" + tags.get(i) + ",limit=1] {text:" + SNBTUtilities.fromComponent(bot.options.useSNBTComponents, names.get(i)) + "}");
}
}

View File

@@ -1,13 +1,13 @@
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;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.json.JSONOptions;
import java.util.Map;
import java.util.Set;
// for 1.21.5 SNBT components, not sure how performant this is
// since i'm not good at writing performant code, but this should
@@ -16,6 +16,15 @@ import java.util.Map;
// 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 GsonComponentSerializer SERIALIZER_1_21_4 =
GsonComponentSerializer.builder()
.options(JSONOptions.byDataVersion().at(4174)) // 24w44a, 1.21.4
.build();
private static final GsonComponentSerializer SERIALIZER_1_21_5 =
GsonComponentSerializer.builder()
.options(JSONOptions.byDataVersion().at(4298)) // 25w02a, 1.21.5, uses snake case for events
.build();
private static final String QUOTE = "'";
private static final String COMMA = ",";
@@ -27,12 +36,9 @@ public class SNBTUtilities {
private static final String BEGIN_ARRAY = "[";
private static final String END_ARRAY = "]";
public static String fromComponent (final Bot bot, final Component component) {
if (!bot.options.useSNBTComponents) return GsonComponentSerializer.gson().serialize(component);
final JsonElement json = GsonComponentSerializer.gson().serializeToTree(component);
return fromJson(json);
public static String fromComponent (final boolean useSNBTComponents, final Component component) {
if (!useSNBTComponents) return SERIALIZER_1_21_4.serialize(component);
else return fromJson(SERIALIZER_1_21_5.serializeToTree(component));
}
// RIPPED from https://minecraft.wiki/w/NBT_format#Conversion_from_JSON
@@ -67,71 +73,16 @@ public class SNBTUtilities {
} else if (json.isJsonObject()) {
final StringBuilder stringBuilder = new StringBuilder(BEGIN_OBJECT);
boolean notEmpty = false;
final Set<Map.Entry<String, JsonElement>> entries = json.getAsJsonObject().entrySet();
for (final Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
notEmpty = true;
// 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();
final JsonObject fixedObject = new JsonObject();
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(entry.getKey()) // no escape :O (optional for adventure)
.append(COLON)
.append(fromJson(entry.getValue()))
.append(COMMA);
}
for (final Map.Entry<String, JsonElement> entry : entries) {
stringBuilder.append(entry.getKey()) // no escape :O (optional for adventure)
.append(COLON)
.append(fromJson(entry.getValue()))
.append(COMMA);
}
if (notEmpty) stringBuilder.deleteCharAt(stringBuilder.length() - 1); // removes comma
if (!entries.isEmpty()) stringBuilder.deleteCharAt(stringBuilder.length() - 1); // removes comma
stringBuilder.append(END_OBJECT);