Rewrite Meteor system to be fully json (#914)
Also cleans up some other JSON stuff
This commit is contained in:
parent
0bb2fa6002
commit
d1455920ec
11 changed files with 261 additions and 196 deletions
|
@ -1,6 +0,0 @@
|
|||
package WayofTime.bloodmagic.gson;
|
||||
|
||||
public class Adapters
|
||||
{
|
||||
public static EnumFacingTypeAdapter adapter = new EnumFacingTypeAdapter();
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package WayofTime.bloodmagic.gson;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
public class EnumFacingTypeAdapter implements JsonDeserializer<EnumFacing>, JsonSerializer<EnumFacing>
|
||||
{
|
||||
@Override
|
||||
public EnumFacing deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
String str = json.getAsString();
|
||||
|
||||
return EnumFacing.byName(str);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(EnumFacing src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
// Not necessary, since this is only used for deserialization.
|
||||
return null;
|
||||
}
|
||||
}
|
22
src/main/java/WayofTime/bloodmagic/gson/SerializerBase.java
Normal file
22
src/main/java/WayofTime/bloodmagic/gson/SerializerBase.java
Normal file
|
@ -0,0 +1,22 @@
|
|||
package WayofTime.bloodmagic.gson;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public abstract class SerializerBase<T> implements JsonDeserializer<T>, JsonSerializer<T>
|
||||
{
|
||||
@Override
|
||||
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return context.deserialize(json, getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
return context.serialize(src);
|
||||
}
|
||||
|
||||
public abstract Class<T> getType();
|
||||
}
|
83
src/main/java/WayofTime/bloodmagic/gson/Serializers.java
Normal file
83
src/main/java/WayofTime/bloodmagic/gson/Serializers.java
Normal file
|
@ -0,0 +1,83 @@
|
|||
package WayofTime.bloodmagic.gson;
|
||||
|
||||
import com.google.gson.*;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class Serializers
|
||||
{
|
||||
// Serializers
|
||||
public static final SerializerBase<EnumFacing> FACING_SERIALIZER = new SerializerBase<EnumFacing>()
|
||||
{
|
||||
@Override
|
||||
public Class<EnumFacing> getType()
|
||||
{
|
||||
return EnumFacing.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumFacing deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return EnumFacing.byName(json.getAsString());
|
||||
}
|
||||
};
|
||||
public static final SerializerBase<ResourceLocation> RESOURCELOCATION_SERIALIZER = new SerializerBase<ResourceLocation>()
|
||||
{
|
||||
@Override
|
||||
public Class<ResourceLocation> getType()
|
||||
{
|
||||
return ResourceLocation.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
String domain = json.getAsJsonObject().get("domain").getAsString();
|
||||
String path = json.getAsJsonObject().get("path").getAsString();
|
||||
return new ResourceLocation(domain, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ResourceLocation src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("domain", src.getResourceDomain());
|
||||
object.addProperty("path", src.getResourcePath());
|
||||
return object;
|
||||
}
|
||||
};
|
||||
public static final SerializerBase<ItemStack> ITEMMETA_SERIALIZER = new SerializerBase<ItemStack>() {
|
||||
@Override
|
||||
public Class<ItemStack> getType() {
|
||||
return ItemStack.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
ResourceLocation registryName = context.deserialize(json.getAsJsonObject().get("registryName").getAsJsonObject(), ResourceLocation.class);
|
||||
int meta = json.getAsJsonObject().get("meta").getAsInt();
|
||||
return new ItemStack(ForgeRegistries.ITEMS.getValue(registryName), 1, meta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.add("registryName", context.serialize(src.getItem().getRegistryName()));
|
||||
jsonObject.addProperty("meta", src.getItemDamage());
|
||||
return jsonObject;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Gson GSON = new GsonBuilder()
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.disableHtmlEscaping()
|
||||
.registerTypeAdapter(FACING_SERIALIZER.getType(), FACING_SERIALIZER)
|
||||
.registerTypeAdapter(RESOURCELOCATION_SERIALIZER.getType(), RESOURCELOCATION_SERIALIZER)
|
||||
.registerTypeAdapter(ITEMMETA_SERIALIZER.getType(), ITEMMETA_SERIALIZER)
|
||||
.create();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue