mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-13 23:04:08 +00:00
Support Byte Array
This commit is contained in:
1 parent
cda6ac4f55
commit
f1ed8fb66f
1 file changed
+77
-17
@@ -54,7 +54,6 @@ public class NBTJsonUtil {
|
||||
compound.setTag(key, base);
|
||||
if (json.startsWith(","))
|
||||
FillCompound(compound, json);
|
||||
|
||||
}
|
||||
|
||||
public static NBTBase ReadValue(JsonFile json) throws JsonException {
|
||||
@@ -65,13 +64,11 @@ public class NBTJsonUtil {
|
||||
throw new JsonException("Expected }", json);
|
||||
}
|
||||
json.cut(1);
|
||||
|
||||
return compound;
|
||||
}
|
||||
if (json.startsWith("[")) {
|
||||
json.cut(1);
|
||||
NBTTagList list = new NBTTagList();
|
||||
|
||||
NBTBase value = ReadValue(json);
|
||||
while (value != null) {
|
||||
list.appendTag(value);
|
||||
@@ -84,7 +81,6 @@ public class NBTJsonUtil {
|
||||
throw new JsonException("Expected ]", json);
|
||||
}
|
||||
json.cut(1);
|
||||
|
||||
if (list.func_150303_d() == 3) {
|
||||
int[] arr = new int[list.tagCount()];
|
||||
for (int i = 0; list.tagCount() > 0; i++) {
|
||||
@@ -99,7 +95,6 @@ public class NBTJsonUtil {
|
||||
}
|
||||
return new NBTTagByteArray(arr);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
if (json.startsWith("\"")) {
|
||||
@@ -153,11 +148,26 @@ public class NBTJsonUtil {
|
||||
private static JsonLine ReadTag(String name, NBTBase base, List<JsonLine> list) {
|
||||
if (!name.isEmpty())
|
||||
name = "\"" + name + "\": ";
|
||||
if (base.getId() == 8) {//NBTTagString
|
||||
if (base.getId() == 8) { // NBTTagString
|
||||
String data = ((NBTTagString) base).func_150285_a_();
|
||||
data = data.replace("\"", "\\\""); //replace " with \"
|
||||
data = data.replace("\"", "\\\""); // Escape quotes
|
||||
list.add(new JsonLine(name + "\"" + data + "\""));
|
||||
} else if (base.getId() == 9) {//NBTTagList
|
||||
} else if (base.getId() == 7) { // NBTTagByteArray
|
||||
byte[] arr = ((NBTTagByteArray) base).func_150292_c();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[");
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
sb.append(arr[i]).append("b");
|
||||
if (i < arr.length - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
sb.append("]");
|
||||
list.add(new JsonLine(name + sb.toString()));
|
||||
JsonLine line = list.get(list.size() - 1);
|
||||
line.line += ",";
|
||||
return line;
|
||||
} else if (base.getId() == 9) { // NBTTagList
|
||||
list.add(new JsonLine(name + "["));
|
||||
NBTTagList tags = (NBTTagList) base;
|
||||
JsonLine line = null;
|
||||
@@ -167,18 +177,16 @@ public class NBTJsonUtil {
|
||||
if (line != null)
|
||||
line.removeComma();
|
||||
list.add(new JsonLine("]"));
|
||||
} else if (base.getId() == 10) { //NBTTagCompound
|
||||
} else if (base.getId() == 10) { // NBTTagCompound
|
||||
list.add(new JsonLine(name + "{"));
|
||||
NBTTagCompound compound = (NBTTagCompound) base;
|
||||
JsonLine line = null;
|
||||
for (Object key : compound.func_150296_c())
|
||||
line = ReadTag(key.toString(), compound.getTag(key.toString()), list);
|
||||
|
||||
if (line != null)
|
||||
line.removeComma();
|
||||
|
||||
list.add(new JsonLine("}"));
|
||||
} else if (base.getId() == 11) {//NBTTagList
|
||||
} else if (base.getId() == 11) { // NBTTagIntArray
|
||||
list.add(new JsonLine(name + base.toString().replaceFirst(",]", "]")));
|
||||
} else {
|
||||
list.add(new JsonLine(name + base));
|
||||
@@ -198,7 +206,6 @@ public class NBTJsonUtil {
|
||||
json += " ";
|
||||
}
|
||||
json += tag + "\n";
|
||||
|
||||
if (tag.increaseTab())
|
||||
tab++;
|
||||
}
|
||||
@@ -277,14 +284,12 @@ public class NBTJsonUtil {
|
||||
int currentPos = lengthOr - lengthCur;
|
||||
String done = original.substring(0, currentPos);
|
||||
String[] lines = done.split("\r\n|\r|\n");
|
||||
|
||||
int pos = 0;
|
||||
String line = "";
|
||||
if (lines.length > 0) {
|
||||
pos = lines[lines.length - 1].length();
|
||||
line = original.split("\r\n|\r|\n")[lines.length - 1].trim();
|
||||
}
|
||||
|
||||
return "Line: " + lines.length + ", Pos: " + pos + ", Text: " + line;
|
||||
}
|
||||
|
||||
@@ -300,7 +305,6 @@ public class NBTJsonUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static NBTTagCompound LoadFile(File file) throws IOException, JsonException {
|
||||
return Convert(Files.toString(file, Charsets.UTF_8));
|
||||
}
|
||||
@@ -311,10 +315,66 @@ public class NBTJsonUtil {
|
||||
try {
|
||||
writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
|
||||
writer.write(json);
|
||||
|
||||
} finally {
|
||||
if (writer != null)
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// Create a test compound with various NBT types
|
||||
NBTTagCompound comp = new NBTTagCompound();
|
||||
NBTTagCompound comp2 = new NBTTagCompound();
|
||||
|
||||
// ByteArray and IntArray
|
||||
comp2.setByteArray("test", new byte[]{0, 0, 1, 1, 0});
|
||||
comp2.setIntArray("intArray", new int[]{0, 0, 1, 1, 0});
|
||||
|
||||
// Primitive types
|
||||
comp2.setByte("byte", (byte)7);
|
||||
comp2.setShort("short", (short)123);
|
||||
comp2.setInteger("int", 456);
|
||||
comp2.setLong("long", 789L);
|
||||
comp2.setFloat("float", 3.14F);
|
||||
comp2.setDouble("double", 2.71828);
|
||||
comp2.setString("string", "Testing");
|
||||
|
||||
// A list containing strings
|
||||
NBTTagList list = new NBTTagList();
|
||||
list.appendTag(new NBTTagString("jim"));
|
||||
list.appendTag(new NBTTagString("foo"));
|
||||
comp2.setTag("list", list);
|
||||
|
||||
// A nested compound
|
||||
NBTTagCompound innerComp = new NBTTagCompound();
|
||||
innerComp.setString("innerString", "innerValue");
|
||||
innerComp.setInteger("innerInt", 10);
|
||||
innerComp.setByte("innerByte", (byte) 11);
|
||||
comp2.setTag("innerComp", innerComp);
|
||||
|
||||
// Add our compound to the root compound.
|
||||
comp.setTag("comp", comp2);
|
||||
|
||||
// Convert NBT to JSON string (writing)
|
||||
String json = Convert(comp);
|
||||
System.out.println("Generated JSON:");
|
||||
System.out.println(json);
|
||||
|
||||
// Read back from JSON string (reading)
|
||||
NBTTagCompound compFromJson = Convert(json);
|
||||
String jsonAfterRead = Convert(compFromJson);
|
||||
System.out.println("\nRe-converted JSON from read NBT:");
|
||||
System.out.println(jsonAfterRead);
|
||||
|
||||
// Basic check: compare the JSON outputs to verify consistency.
|
||||
if (json.equals(jsonAfterRead)) {
|
||||
System.out.println("\nSUCCESS: Read and write operations are consistent for all types.");
|
||||
} else {
|
||||
System.out.println("\nWARNING: There is a mismatch in read and write results for some types.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user