Fix Value mess

This commit is contained in:
altrisi committed 2020-12-27 15:53:44 +01:00
1 parent aabff1623b
commit 929c17da36
5 files changed
+69 -63

No files matched your search

@@ -18,6 +18,8 @@ import carpet.script.value.NumericValue;
import carpet.script.value.StringValue; import carpet.script.value.StringValue;
import carpet.script.value.Value; import carpet.script.value.Value;
import carpet.utils.Messenger; import carpet.utils.Messenger;
import com.google.gson.JsonElement;
import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
@@ -720,7 +722,7 @@ public class CarpetScriptHost extends ScriptHost
return Module.listFile(main, resource, type, isShared); return Module.listFile(main, resource, type, isShared);
} }
public Value readJsonFile(String resource, String type, boolean isShared) public JsonElement readJsonFile(String resource, String type, boolean isShared)
{ {
if (getName() == null && !isShared) return null; if (getName() == null && !isShared) return null;
return Module.readJsonFile(main, resource, type, isShared); return Module.readJsonFile(main, resource, type, isShared);
@@ -35,9 +35,6 @@ import carpet.script.value.StringValue;
import carpet.script.value.Value; import carpet.script.value.Value;
import carpet.script.value.ValueConversions; import carpet.script.value.ValueConversions;
import carpet.utils.Messenger; import carpet.utils.Messenger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
@@ -73,6 +70,10 @@ import net.minecraft.world.World;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@@ -821,7 +822,8 @@ public class Auxiliary {
} }
else if (fdesc.getMiddle().equals("json")) else if (fdesc.getMiddle().equals("json"))
{ {
Value parsedJson = ((CarpetScriptHost) c.host).readJsonFile(fdesc.getLeft(), fdesc.getMiddle(), fdesc.getRight()); JsonElement json = ((CarpetScriptHost) c.host).readJsonFile(fdesc.getLeft(), fdesc.getMiddle(), fdesc.getRight());
Value parsedJson = gson.fromJson(json, Value.class);
if (parsedJson == null) if (parsedJson == null)
retVal = Value.NULL; retVal = Value.NULL;
else else
@@ -25,8 +25,9 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Stream; import java.util.stream.Stream;
import carpet.script.api.Auxiliary; import com.google.gson.JsonElement;
import carpet.script.value.Value; import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
public class FileModule extends Module public class FileModule extends Module
{ {
@@ -180,10 +181,11 @@ public class FileModule extends Module
} }
} }
public static Value readJsonContent(Path filePath) public static JsonElement readJsonContent(Path filePath)
{ {
try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) { try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8))
return Auxiliary.gson.fromJson(reader, Value.class); {
return new JsonParser().parse(new JsonReader(reader));
} }
catch (IOException e) catch (IOException e)
{ {
@@ -1,12 +1,12 @@
package carpet.script.bundled; package carpet.script.bundled;
import carpet.CarpetServer; import carpet.CarpetServer;
import carpet.script.value.Value;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
import net.minecraft.util.WorldSavePath; import net.minecraft.util.WorldSavePath;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@@ -80,7 +80,7 @@ public abstract class Module
synchronized (writeIOSync) { return FileModule.listFileContent(dataFile); } synchronized (writeIOSync) { return FileModule.listFileContent(dataFile); }
} }
public static Value readJsonFile(Module module, String resourceName, String type, boolean isShared) { public static JsonElement readJsonFile(Module module, String resourceName, String type, boolean isShared) {
Path dataFile = resolveResource(module, resourceName, supportedTypes.get(type), isShared); Path dataFile = resolveResource(module, resourceName, supportedTypes.get(type), isShared);
if (dataFile == null) return null; if (dataFile == null) return null;
if (!dataFile.toFile().exists()) return null; if (!dataFile.toFile().exists()) return null;
@@ -22,57 +22,57 @@ import carpet.script.value.Value;
public class ScarpetJsonDeserializer implements JsonDeserializer<Value>{ public class ScarpetJsonDeserializer implements JsonDeserializer<Value>{
@Override @Override
public Value deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) public Value deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException { throws JsonParseException {
return parseElement(json); return parseElement(json);
} }
private Value parseElement(JsonElement element) throws JsonParseException { private Value parseElement(JsonElement element) throws JsonParseException {
if (element.isJsonObject()) if (element.isJsonObject())
{ {
return parseMap(element.getAsJsonObject()); return parseMap(element.getAsJsonObject());
} }
else if (element.isJsonArray()) else if (element.isJsonArray())
{ {
return parseList(element.getAsJsonArray()); return parseList(element.getAsJsonArray());
} }
else if (element.isJsonPrimitive()) else if (element.isJsonPrimitive())
{ {
return parsePrimitive(element.getAsJsonPrimitive()); return parsePrimitive(element.getAsJsonPrimitive());
} }
return Value.NULL; return Value.NULL;
} }
private Value parseMap(JsonObject jsonMap) throws JsonParseException { private Value parseMap(JsonObject jsonMap) throws JsonParseException {
Map<Value, Value> map = new HashMap<Value, Value>(); Map<Value, Value> map = new HashMap<Value, Value>();
jsonMap.entrySet().forEach(entry -> { jsonMap.entrySet().forEach(entry -> {
map.put(new StringValue(entry.getKey()), parseElement(entry.getValue())); map.put(new StringValue(entry.getKey()), parseElement(entry.getValue()));
}); });
return MapValue.wrap(map); return MapValue.wrap(map);
} }
private Value parseList(JsonArray jsonList) throws JsonParseException { private Value parseList(JsonArray jsonList) throws JsonParseException {
List<Value> list = new ArrayList<Value>(); List<Value> list = new ArrayList<Value>();
jsonList.forEach(elem -> { jsonList.forEach(elem -> {
list.add(parseElement(elem)); list.add(parseElement(elem));
}); });
return new ListValue(list); return new ListValue(list);
} }
private Value parsePrimitive(JsonPrimitive primitive) throws JsonParseException { private Value parsePrimitive(JsonPrimitive primitive) throws JsonParseException {
if (primitive.isString()) if (primitive.isString())
{ {
return new StringValue(primitive.getAsString()); return new StringValue(primitive.getAsString());
} }
else if (primitive.isBoolean()) else if (primitive.isBoolean())
{ {
return primitive.getAsBoolean() ? Value.TRUE : Value.FALSE; return primitive.getAsBoolean() ? Value.TRUE : Value.FALSE;
} }
else if (primitive.isNumber()) else if (primitive.isNumber())
{ {
return NumericValue.of(primitive.getAsNumber()); return NumericValue.of(primitive.getAsNumber());
} }
return Value.NULL; return Value.NULL;
} }
} }