Skip to content

Commit

Permalink
Added Beautiful NBT for some reason
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Mar 25, 2024
1 parent b45130b commit e0d74e1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
76 changes: 76 additions & 0 deletions src/main/java/net/pedroricardo/commander/BeautifulNbt.java
Original file line number Diff line number Diff line change
@@ -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<Tag<?>>)o)) {
if (comma) result.append(", ");
result.append(element(element.getValue()));
comma = true;
}
}
return result.append("]").toString();
}
}
12 changes: 0 additions & 12 deletions src/main/java/net/pedroricardo/commander/Commander.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/pedroricardo/commander/NbtHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/commander.accesswidener
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e0d74e1

Please sign in to comment.