-
Notifications
You must be signed in to change notification settings - Fork 17
Json support #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop/1.13
Are you sure you want to change the base?
Json support #134
Changes from all commits
89f6066
5bbab8e
cfc5090
6d2214d
0cdef44
97f621a
bb61932
3934630
1ddcec7
b88c235
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,3 +58,4 @@ Icon | |
| Network Trash Folder | ||
| Temporary Items | ||
| .apdisk | ||
| /classes/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package codersafterdark.reskillable.base.configs.json; | ||
|
|
||
| import codersafterdark.reskillable.api.data.LockKey; | ||
| import codersafterdark.reskillable.api.data.RequirementHolder; | ||
|
|
||
| public class LockJson { | ||
| private RequirementHolder requirements; | ||
| private LockKey lockKey; | ||
|
|
||
| public LockJson(RequirementHolder requirements, LockKey lockKey) { | ||
| this.requirements = requirements; | ||
| this.lockKey = lockKey; | ||
| } | ||
|
|
||
| public RequirementHolder getRequirements() { | ||
| return requirements; | ||
| } | ||
|
|
||
| public LockKey getLockKey() { | ||
| return lockKey; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "LockJson{" + | ||
| "requirements=" + requirements + | ||
| ", lockKey=" + lockKey + | ||
| '}'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package codersafterdark.reskillable.base.configs.json; | ||
|
|
||
| import codersafterdark.reskillable.api.data.ItemInfo; | ||
| import codersafterdark.reskillable.base.configs.json.parsers.CustomItemInfoJson; | ||
| import codersafterdark.reskillable.base.configs.json.parsers.CustomItemJson; | ||
| import codersafterdark.reskillable.base.configs.json.parsers.CustomLockJson; | ||
| import codersafterdark.reskillable.base.configs.json.parsers.CustomNBTJson; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonDeserializer; | ||
| import net.minecraft.item.Item; | ||
| import net.minecraft.nbt.NBTTagCompound; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class LockTypeJsonFactory { | ||
| private static Map<Type, JsonDeserializer> deserializerMap = new HashMap<>(); | ||
|
|
||
| static { | ||
| registerDeserializer(LockJson.class, new CustomLockJson()); | ||
| registerDeserializer(Item.class, new CustomItemJson()); | ||
| registerDeserializer(ItemInfo.class, new CustomItemInfoJson()); | ||
| registerDeserializer(NBTTagCompound.class, new CustomNBTJson()); | ||
| } | ||
|
|
||
| public static void registerDeserializer(Type type, JsonDeserializer deserializer) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be accessible through the api somehow?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am leaving this up to pup, as I am not sure how he wants the API to look like.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is probably fine given the JSON stuff will probably be for a larger version such as 2.0 which will allow for refactoring a bunch of stuff without worrying about breaking API things. Given a bunch of the stuff in LevelLockHandler should probably be moved from the base package to an API package. The only reason it is in LevelLockHandler to begin with is that it basically grew as I was fixing and then expanding on things making them more generic and supporting custom registrations. |
||
| deserializerMap.put(type, deserializer); | ||
| } | ||
|
|
||
| public static Gson constructGSON() { | ||
| GsonBuilder builder = new GsonBuilder() | ||
| .setPrettyPrinting(); | ||
|
|
||
| deserializerMap.forEach(builder::registerTypeAdapter); | ||
|
|
||
| return builder.create(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package codersafterdark.reskillable.base.configs.json.parsers; | ||
|
|
||
| import codersafterdark.reskillable.api.data.ItemInfo; | ||
| import com.google.gson.*; | ||
| import net.minecraft.item.Item; | ||
| import net.minecraft.nbt.NBTTagCompound; | ||
| import net.minecraftforge.oredict.OreDictionary; | ||
|
|
||
| import java.lang.reflect.Type; | ||
|
|
||
| public class CustomItemInfoJson implements JsonDeserializer<ItemInfo> { | ||
| @Override | ||
| public ItemInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
| JsonObject obj = json.getAsJsonObject(); | ||
| JsonElement itemElem = obj.get("item"); | ||
|
|
||
| if (itemElem == null) | ||
| throw new JsonParseException("No key 'item' given in ItemInfo json."); | ||
|
|
||
| Item item = context.deserialize(itemElem, Item.class); | ||
| int meta = !obj.has("metadata") ? 0 : obj.get("metadata").getAsString().equals("*") ? OreDictionary.WILDCARD_VALUE : obj.get("metadata").getAsInt(); | ||
| JsonElement nbtJson = obj.get("nbt"); | ||
| NBTTagCompound tagCompound = null; | ||
| if (nbtJson != null) { | ||
| tagCompound = context.deserialize(nbtJson, NBTTagCompound.class); | ||
| } | ||
|
|
||
| return new ItemInfo(item, meta, tagCompound); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package codersafterdark.reskillable.base.configs.json.parsers; | ||
|
|
||
| import com.google.gson.JsonDeserializationContext; | ||
| import com.google.gson.JsonDeserializer; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonParseException; | ||
| import net.minecraft.item.Item; | ||
| import net.minecraft.util.ResourceLocation; | ||
| import net.minecraftforge.fml.common.registry.ForgeRegistries; | ||
|
|
||
| import java.lang.reflect.Type; | ||
|
|
||
| public class CustomItemJson implements JsonDeserializer<Item> { | ||
| @Override | ||
| public Item deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
| return ForgeRegistries.ITEMS.getValue(new ResourceLocation(json.getAsString())); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't this code for pulling from the config file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into how we are doing it currently and the setupLocks method is called in
FMLPostInitializationEventso we use it to register the lock keys, and then if the config has locks we register them. We previously loaded the strings here https://github.com/Coders-After-Dark/Reskillable/blob/develop/1.12.2/src/main/java/codersafterdark/reskillable/base/ConfigHandler.java#L53 Looking at the new code the locks are loaded from json here (which makes sense as all custom locks should have been registered by now from addons). I think this is fine however I think the linked line andLevelLockHandler#loadFromConfigandprivate static String[] configLocks;should probably be removed as they are no longer used. That or an automated converter should be made to port existing config files to the JSON format. I will mark through the review the lines that should be able to be deleted.