2021-10-29 22:50:42 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.gson;
|
2021-10-27 22:00:08 +02:00
|
|
|
|
|
|
|
import com.google.gson.*;
|
2021-10-29 22:50:42 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.model.mojang.MinecraftArgument;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.mojang.Rules;
|
2021-10-27 22:00:08 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class MinecraftArgumentDeserializer implements JsonDeserializer<MinecraftArgument> {
|
|
|
|
@Override
|
|
|
|
public MinecraftArgument deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
|
|
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
|
|
|
|
return new MinecraftArgument(Set.of(json.getAsString()));
|
|
|
|
}
|
|
|
|
else if (json.isJsonObject()) {
|
|
|
|
JsonObject jo = json.getAsJsonObject();
|
|
|
|
if (jo.size() != 2 || !jo.has("rules") || !jo.has("value"))
|
|
|
|
throw new JsonParseException("Not a valid minecraft argument");
|
|
|
|
Rules r = context.deserialize(jo.get("rules"), Rules.class);
|
|
|
|
if (!r.allow()) return new MinecraftArgument(Set.of());
|
|
|
|
Set<String> sel = new LinkedHashSet<>();
|
2021-10-28 20:19:09 +02:00
|
|
|
JsonElement value = jo.get("value");
|
|
|
|
if (value.isJsonArray()) {
|
|
|
|
for (JsonElement val : value.getAsJsonArray()) {
|
|
|
|
sel.add(val.getAsString());
|
|
|
|
}
|
2021-10-27 22:00:08 +02:00
|
|
|
}
|
2021-10-28 20:19:09 +02:00
|
|
|
else if (value.isJsonPrimitive())
|
|
|
|
sel.add(value.getAsString());
|
|
|
|
else throw new JsonParseException("Unexpected value type");
|
2021-10-27 22:00:08 +02:00
|
|
|
return new MinecraftArgument(Set.copyOf(sel));
|
|
|
|
}
|
|
|
|
else throw new JsonParseException("Not a valid minecraft argument");
|
|
|
|
}
|
|
|
|
}
|