create presets module

This commit is contained in:
Serghei Borovetchi authored and Jason Mitchell committed 2022-02-10 08:43:16 -08:00
1 parent 49b22e0683
commit 5f1ed67a8b
12 files changed
+1382 -49

No files matched your search

+81 -31
View File
@@ -1,11 +1,8 @@
package codechicken.nei.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagByte;
@@ -20,26 +17,21 @@ import net.minecraft.nbt.NBTTagLong;
import net.minecraft.nbt.NBTTagShort;
import net.minecraft.nbt.NBTTagString;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
public class NBTJson {
private static final Pattern numberPattern = Pattern.compile("^([-+]?[\\d]+\\.?[0-9]*)([bBsSlLfFdD]?)$");
private static final JsonParser parser = new JsonParser();
private static final Gson gson = new GsonBuilder().create();
private static final Pattern numberPattern = Pattern.compile("^([-+]?\\d+\\.?\\d*E*\\d*)([bBsSlLfFdD]?)$");
public static String toJson(NBTTagCompound tag) {
return toJson(toJsonObject(tag));
}
public static String toJson(JsonElement json) {
final String jsonString = json.toString();
return gson.toJson(parser.parse(jsonString));
return json.toString();
}
@SuppressWarnings("unchecked")
@@ -50,9 +42,11 @@ public class NBTJson {
final Map<String, NBTBase> tagMap = (Map<String, NBTBase>)nbtTagCompound.tagMap;
JsonObject root = new JsonObject();
for (Map.Entry<String, NBTBase> nbtEntry : tagMap.entrySet() ) {
for (Map.Entry<String, NBTBase> nbtEntry : tagMap.entrySet()) {
root.add(nbtEntry.getKey(), toJsonObject(nbtEntry.getValue()));
}
return root;
} else if (nbt instanceof NBTTagByte) {
// Number (byte)
@@ -80,23 +74,48 @@ public class NBTJson {
return new JsonPrimitive(((NBTTagString) nbt).func_150285_a_());
} else if (nbt instanceof NBTTagList) {
// Tag List
JsonArray arr = new JsonArray();
((NBTTagList) nbt).tagList.forEach(c -> arr.add(toJsonObject((NBTBase) c)));
return arr;
final NBTTagList list = (NBTTagList) nbt;
if (list.tagList.isEmpty()) {
return createEmptyList(list);
} else {
JsonArray arr = new JsonArray();
list.tagList.forEach(c -> arr.add(toJsonObject((NBTBase) c)));
return arr;
}
} else if (nbt instanceof NBTTagIntArray) {
// Int Array
JsonArray arr = new JsonArray();
for(int i : ((NBTTagIntArray) nbt).func_150302_c()) {
arr.add(new JsonPrimitive(i));
final NBTTagIntArray list = (NBTTagIntArray) nbt;
if (list.func_150302_c().length == 0) {
return createEmptyList(list);
} else {
JsonArray arr = new JsonArray();
for (int i : list.func_150302_c()) {
arr.add(new JsonPrimitive(i));
}
return arr;
}
return arr;
} else if (nbt instanceof NBTTagByteArray) {
// Byte Array
JsonArray arr = new JsonArray();
for(int i : ((NBTTagByteArray) nbt).func_150292_c()) {
arr.add(new JsonPrimitive(i));
final NBTTagByteArray list = (NBTTagByteArray) nbt;
if (list.func_150292_c().length == 0) {
return createEmptyList(list);
} else {
JsonArray arr = new JsonArray();
for (byte i : list.func_150292_c()) {
arr.add(new JsonPrimitive(Byte.toString(i) + 'B'));
}
return arr;
}
return arr;
} else {
throw new IllegalArgumentException("Unsupported NBT Tag: " + NBTBase.NBTTypes[nbt.getId()] + " - " + nbt);
}
@@ -108,7 +127,7 @@ public class NBTJson {
final JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonElement;
final String jsonString = jsonPrimitive.getAsString();
final Matcher m = numberPattern.matcher(jsonString);
if(m.find()) {
if (m.find()) {
// Number
final String numberString = m.group(1);
if (m.groupCount() == 2 && m.group(2).length() > 0) {
@@ -152,7 +171,13 @@ public class NBTJson {
if (nbtList.stream().allMatch(n -> n instanceof NBTTagInt)) {
return new NBTTagIntArray(nbtList.stream().mapToInt(i -> ((NBTTagInt)i).func_150287_d() ).toArray());
} else if (nbtList.stream().allMatch(n -> n instanceof NBTTagByte)) {
return new NBTTagByteArray(toByteArray(nbtList.stream().mapToInt(i -> ((NBTTagInt)i).func_150287_d() )));
final byte[] abyte = new byte[nbtList.size()];
for (int i = 0; i < nbtList.size(); i++) {
abyte[i] = ((NBTTagByte) nbtList.get(i)).func_150290_f();
}
return new NBTTagByteArray(abyte);
} else {
NBTTagList nbtTagList = new NBTTagList();
nbtList.forEach(nbtTagList::appendTag);
@@ -163,18 +188,43 @@ public class NBTJson {
} else if (jsonElement instanceof JsonObject) {
// NBTTagCompound
final JsonObject jsonObject = (JsonObject) jsonElement;
NBTTagCompound nbtTagCompound = new NBTTagCompound();
for (Map.Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
nbtTagCompound.setTag(jsonEntry.getKey(), toNbt(jsonEntry.getValue()));
final NBTBase custom = restoreEmptyList(jsonObject);
if (custom == null) {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
for (Map.Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
nbtTagCompound.setTag(jsonEntry.getKey(), toNbt(jsonEntry.getValue()));
}
return nbtTagCompound;
} else {
return custom;
}
return nbtTagCompound;
}
throw new IllegalArgumentException("Unhandled element " + jsonElement);
}
public static byte[] toByteArray(IntStream stream) {
return stream.collect(ByteArrayOutputStream::new, (baos, i) -> baos.write((byte) i), (baos1, baos2) -> baos1.write(baos2.toByteArray(), 0, baos2.size()))
.toByteArray();
protected static JsonObject createEmptyList(Object obj)
{
JsonObject empty = new JsonObject();
empty.add("__custom_type", new JsonPrimitive(obj.getClass().getName()));
return empty;
}
protected static NBTBase restoreEmptyList(JsonObject obj)
{
if (obj.has("__custom_type")) {
try {
final String className = obj.get("__custom_type").getAsString();
return (NBTBase)Class.forName(className).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException th) {}
}
return null;
}
}