From e0d74e1b5f0c1d5459aa4b8bc1009532217569b0 Mon Sep 17 00:00:00 2001 From: Pedro Date: Sun, 24 Mar 2024 23:21:41 -0300 Subject: [PATCH] Added Beautiful NBT for some reason --- .../commander.accesswidener | 2 +- .../pedroricardo/commander/Commander.class | Bin 3366 -> 2348 bytes .../pedroricardo/commander/BeautifulNbt.java | 76 ++++++++++++++++++ .../net/pedroricardo/commander/Commander.java | 12 --- .../net/pedroricardo/commander/NbtHelper.java | 14 ++-- src/main/resources/commander.accesswidener | 2 +- 6 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 src/main/java/net/pedroricardo/commander/BeautifulNbt.java diff --git a/output/production/commander-bta.main/commander.accesswidener b/output/production/commander-bta.main/commander.accesswidener index 4887c74..6a64719 100644 --- a/output/production/commander-bta.main/commander.accesswidener +++ b/output/production/commander-bta.main/commander.accesswidener @@ -1,3 +1,3 @@ -accessWidener v1 named +accessWidener v2 named accessible method net/minecraft/client/gui/Gui drawRect (IIIII)V accessible method net/minecraft/client/gui/Gui drawRectWidthHeight (IIIII)V \ No newline at end of file diff --git a/output/production/commander-bta.main/net/pedroricardo/commander/Commander.class b/output/production/commander-bta.main/net/pedroricardo/commander/Commander.class index fc5775e17d28010808921cbce2e9b717e84d8023..a7643df0162b4ab0d20c61e3fd485933a9a491ae 100644 GIT binary patch delta 594 zcmYL`%~BIl5QV>+z|3SaDozPX)IueQ^5;euy0ZiX1Q8<2kBE^ZL(n8jf`U~POCR9A z11mQ$Wr1471Mmzy0$VpMEPF0g*!1ap`}8?|`);Lv`0l~K-}}IQ*0+zmNz2!Vmd41?yv2w(1mh`cc zR?E6AainM6fz5y-Hm!VM4#h{AI>w-!lX#LRqKOQOwLvj5DAIzhT;sa*l_ZZ*ra|&X zEQuAlxhJRu8@qN<+lKN&wc~W{b-n8n z)21eTmG7_R8w%|O4&;(rN&-my0Z2$(xN+mk9f<>C_BkOra6l`~%$u1v^ZU&k?P}!f zzR*v9fBz$ZB)(b;opHB3X?p<*mA=Rx$xR#&G;fR<%@75r?CWAu0U}nywrD{3I(g*^C%VNzrsN5tE|wNKBsP zk)$$nEVExML`A7EyBxvTsLoHRrA!EUO!;vpjI(%>Vc&lsXYkh?9(t9i9qw@BtWOmd zwK*#@bT)u<3?iqNUoh&HBFQGlJzljGIo*=v1V7Rw%GHw6Pe66!VE=LeGYlL2-Z8__ zr|Jz&u}JV_$1|$xrlu4f+3__kO_8KRRB)!pdkKn=63vdoX_u7bl-N1u3~DAw>6V;K zNMbrE<~sgS-B2w>kYi#}Or@paV#l2|8Usw?9QV*wu zRu4f(#gA$j25R=SC-19=VZyT4JiWflVLXc^`xnnZ_j9yLTGc9B%~%itTn%Fx*X*Eo z$oG60D|o>k@gDcR6voSV#a{8I9(Z+oAsbd%kMp@Wd_%Ynw;ylVzj_DQTlQbxKXOcC z7|HD>{r@><8202Xb?)MXTANY}y8XLvto1tHgde?l8`r?_4*dmODATzMVX}h`JFbZu z*F=PCf=;>@2XG(VdzWk%-h+p1FMK55Cr_L-Bkeu10k+0dY?4kG*oO~D9tw9d@F6~; zBg4nrw^;^mstK&xKl_WV+sH;vZX&miCmeHn$9!tXe0s-xrel7CJ93nNbRC6tXlvA< zh#R;`ukVI~2``w_w+?7;xhc@RhO5EKkz9EUNDA)Ln%w2Fx05+24?#OPJx#4kaG zWGW@%79@O2e@}i*5M#hT6E~X`>NowsB<>tu|i# r0oTuM;I%gToF0r1p^r)c-L$DaG=%-Q3w^}n0DbpU#tn+OT=?=Iz6gX^ diff --git a/src/main/java/net/pedroricardo/commander/BeautifulNbt.java b/src/main/java/net/pedroricardo/commander/BeautifulNbt.java new file mode 100644 index 0000000..c8bacc1 --- /dev/null +++ b/src/main/java/net/pedroricardo/commander/BeautifulNbt.java @@ -0,0 +1,76 @@ +package net.pedroricardo.commander; + +import com.mojang.nbt.*; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class BeautifulNbt { + public static String toBeautifulNbt(@NotNull CompoundTag nbt) { + StringBuilder builder = new StringBuilder("{"); + boolean comma = false; + for (Tag tag : nbt.getValue().values()) { + if (comma) builder.append(", "); + builder.append(tag.getTagName()).append(": ").append(element(tag instanceof CompoundTag ? tag : tag.getValue())); + comma = true; + } + return builder.append("}").toString(); + } + + private static String element(Object o) { + if (o instanceof byte[] || o instanceof short[] || o instanceof double[] || o instanceof List) { + return collection(o); + } + if (o instanceof Byte) { + return o + "b"; + } + if (o instanceof Short) { + return o + "s"; + } + if (o instanceof Long) { + return o + "l"; + } + if (o instanceof Float) { + return o + "f"; + } + if (o instanceof String) { + return "\"" + o + "\""; + } + return o instanceof CompoundTag ? toBeautifulNbt((CompoundTag) o) : o.toString(); + } + + private static String collection(Object o) { + StringBuilder result = new StringBuilder("["); + boolean comma = false; + if (o instanceof byte[]) { + result.append("B; "); + for (double element : ((byte[])o)) { + if (comma) result.append(", "); + result.append(element(element)); + comma = true; + } + } else if (o instanceof short[]) { + result.append("S; "); + for (double element : ((short[])o)) { + if (comma) result.append(", "); + result.append(element(element)); + comma = true; + } + } else if (o instanceof double[]) { + result.append("D; "); + for (double element : ((double[])o)) { + if (comma) result.append(", "); + result.append(element(element)); + comma = true; + } + } else if (o instanceof List) { + //noinspection unchecked + for (Tag element : ((List>)o)) { + if (comma) result.append(", "); + result.append(element(element.getValue())); + comma = true; + } + } + return result.append("]").toString(); + } +} diff --git a/src/main/java/net/pedroricardo/commander/Commander.java b/src/main/java/net/pedroricardo/commander/Commander.java index c8ebb52..a34ad54 100644 --- a/src/main/java/net/pedroricardo/commander/Commander.java +++ b/src/main/java/net/pedroricardo/commander/Commander.java @@ -1,7 +1,6 @@ package net.pedroricardo.commander; import com.google.gson.JsonObject; -import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.api.ModInitializer; import net.minecraft.core.net.command.TextFormatting; @@ -10,9 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import turniplabs.halplibe.helper.NetworkHelper; -import turniplabs.halplibe.util.GameStartEntrypoint; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; @@ -40,15 +37,6 @@ public void onInitialize() { ARGUMENT_STYLES.add(TextFormatting.ORANGE.toString()); NetworkHelper.register(CommandManagerPacket.class, false, true); NetworkHelper.register(RequestCommandManagerPacket.class, true, false); -// try { -// System.out.println(NbtHelper.parseNbt("{Data1:16,Data2:\"hello\",Data3:[D;1.52,940.2,38.3],Data4:[B;1b,12b]}")); -// System.out.println(NbtHelper.parseNbt("{DataShort:23s,DataInt:39230,DataString:\"Hello!\"}")); -// System.out.println(NbtHelper.parseNbt("{DataBoolTrue:true,DataBoolFalse:false,DataByte:22b, DataShort : 51s, DataInt:42351, DataLong: 2934021941L, DataFloat: 3.1415926f, DataDouble: 22.543d, DataDoubleNoSuffix: 2942.43290}")); -// System.out.println(NbtHelper.parseNbt("{DataByteArray:[B;6b,12b,24b] ,DataShortArray:[S ; 20s , 40s , 63s ] , DataDoubleArray:[D ; 22.53, 239.43d, 39.4D]}")); -// System.out.println(NbtHelper.parseNbt("{DataList:[6b,12b,24b]}")); -// } catch (CommandSyntaxException e) { -// System.out.println(e.getMessage()); -// } } @Override diff --git a/src/main/java/net/pedroricardo/commander/NbtHelper.java b/src/main/java/net/pedroricardo/commander/NbtHelper.java index f379838..f0d8448 100644 --- a/src/main/java/net/pedroricardo/commander/NbtHelper.java +++ b/src/main/java/net/pedroricardo/commander/NbtHelper.java @@ -10,12 +10,12 @@ import java.util.regex.Pattern; public class NbtHelper { - private static final Pattern DOUBLE_PATTERN_NO_SUFFIX = Pattern.compile("[-+]?(?:[0-9]+[.]|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?", 2); - private static final Pattern DOUBLE_PATTERN = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?d", 2); - private static final Pattern FLOAT_PATTERN = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?f", 2); - private static final Pattern BYTE_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)b", 2); - private static final Pattern LONG_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)l", 2); - private static final Pattern SHORT_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)s", 2); + private static final Pattern DOUBLE_PATTERN_NO_SUFFIX = Pattern.compile("[-+]?(?:[0-9]+[.]|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?", Pattern.CASE_INSENSITIVE); + private static final Pattern DOUBLE_PATTERN = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?d", Pattern.CASE_INSENSITIVE); + private static final Pattern FLOAT_PATTERN = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?f", Pattern.CASE_INSENSITIVE); + private static final Pattern BYTE_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)b", Pattern.CASE_INSENSITIVE); + private static final Pattern LONG_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)l", Pattern.CASE_INSENSITIVE); + private static final Pattern SHORT_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)s", Pattern.CASE_INSENSITIVE); private static final Pattern INT_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)"); public static CompoundTag parseNbt(String string) throws CommandSyntaxException { @@ -53,7 +53,7 @@ public static CompoundTag parseNbt(StringReader reader) throws CommandSyntaxExce return tag; } - private static Tag parseTag(StringReader reader) throws CommandSyntaxException { + public static Tag parseTag(StringReader reader) throws CommandSyntaxException { if (!reader.canRead()) throw CommanderExceptions.incomplete().create(); if (reader.peek() == '{') { return parseNbt(reader); diff --git a/src/main/resources/commander.accesswidener b/src/main/resources/commander.accesswidener index 4887c74..6a64719 100644 --- a/src/main/resources/commander.accesswidener +++ b/src/main/resources/commander.accesswidener @@ -1,3 +1,3 @@ -accessWidener v1 named +accessWidener v2 named accessible method net/minecraft/client/gui/Gui drawRect (IIIII)V accessible method net/minecraft/client/gui/Gui drawRectWidthHeight (IIIII)V \ No newline at end of file