commoble.databuddy.data

## Class SimpleJsonDataManager<T>

- java.lang.Object
- - net.minecraft.client.resources.ReloadListener<java.util.Map<net.minecraft.util.ResourceLocation,com.google.gson.JsonElement>>
  - - net.minecraft.client.resources.JsonReloadListener
    - - commoble.databuddy.data.SimpleJsonDataManager<T>

All Implemented Interfaces:
  net.minecraft.resources.IFutureReloadListener

    public class SimpleJsonDataManager<T>
    extends net.minecraft.client.resources.JsonReloadListener

Class for loading JSON resources that don't require anything more complicated than a basic GSON parser

This will NOT merge jsons from mods or datapacks that define jsons with the same ID i.e. one json will be "overwritten" by the other according to datapack order, like loot tables or recipes

GSON user guide, reading this first is very recommended https://github.com/google/gson/blob/master/UserGuide.md

Under certain circumstances, we don't have to tell our GSON parser how to parse our jsons. If we have a GSON-friendly data class, we can just parse our json with the default GSON and it will just work! All we need to tell it is what type of class the json represents.

The very short version is that if our gson-friendly class only has fields of one of the following types:

- -A json primitive (number, string, boolean)
- -A gson-friendly class
- -An array of json primitives or gson-friendly class objects (a class can even have arrays of its own type!)
- -NO GENERIC TYPES

And the names of our fields match the keys in the given json object, then we don't have to tell GSON how to parse our json.

This class is intended to be used as a JsonReloadListener for json structures that represent such classes.

Usage example: Let's say we have a json that looks like this

    public class ImageData
    {
            private String texture; // the ResourceLocation of a texture file
            private int u;  // the x of the top-left coordinate to read the texture from
            private int v;  // the y of the top-left coordinate to read the texture from
            private int width;      // how much of the texture to draw in width
            private int height;     // how much of the texture to draw in height
    }

and we have a json at assets/mondobook/mondobookimages/fancy_banner.json

    {
            "texture": "mondobook:misc/fancy_banner",
            "width": 100,
            "height":18
    }

this class will parse this json into the above class, using 0/false/null for unspecified values.

We can register an instance of class to the server data listener in the ServerAboutToStartEvent, or to the client asset listener when our mod is constructed. e.g.

    public class AssetManagers
    {
    	public static final SimpleJsonDataManager<ImageData> IMAGE_DATA = new SimpleJsonDataManager<>("mondobookimages", ImageData.class);

    	// call from mod constructor if on client
    	public static void onClientInit()
    	{
    		IResourceManager manager = Minecraft.getInstance().getResourceManager();
    		if (manager instanceof IReloadableResourceManager)
    		{
    			IReloadableResourceManager reloader = (IReloadableResourceManager)manager;
    			reloader.addReloadListener(IMAGE_DATA);
    		}
    	}
    }

And this is literally it, once your data manager is registered as a reload listener, you can call yourDataManager.getData(ResourceLocation) to get the object represented by whatever json was at that location (assuming you set up everything properly and minecraft has loaded resources at least once). Remember that assets are loaded after your mod constructor, but before most client events, so be sure to register asset reload listeners when your mod instance is constructed!

Again, this won't work with data classes that have fields of generic types. You'll either A) define your reload listener (using GSON or otherwise), or B) make a data class that doesn't have generic types and convert it to a "finished" class that does, somehow

### Nested Class Summary

### Nested classes/interfaces inherited from interface net.minecraft.resources.IFutureReloadListener

`net.minecraft.resources.IFutureReloadListener.IStage`

### Field Summary

Fields
`protected java.util.Map<net.minecraft.util.ResourceLocation,T>` `data` The raw data that we parsed from json last time resources were reloaded

### Constructor Summary

Constructors
Constructor and Description
`SimpleJsonDataManager(java.lang.String folder, java.lang.Class<T> dataClass)`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`protected void` `apply(java.util.Map<net.minecraft.util.ResourceLocation,com.google.gson.JsonElement> jsons, net.minecraft.resources.IResourceManager manager, net.minecraft.profiler.IProfiler profiler)` Called on resource reload, the jsons have already been found for us and we just need to parse them in here
`T` `getData(net.minecraft.util.ResourceLocation id)` Get the data object represented by the json at the given resource location
`protected T` `getJsonAsData(com.google.gson.JsonElement json)` Use a json object (presumably one from an assets/modid/asset_type/ or data/modid/data_type/ folder) to generate a data object
`static <KEY,IN,OUT> java.util.Map<KEY,OUT>` `mapValues(java.util.Map<KEY,IN> inputs, java.util.function.Function<IN,OUT> mapper)` Converts all the values in a map to new values; the new map uses the same keys as the old map

### Methods inherited from class net.minecraft.client.resources.JsonReloadListener

`getPreparedPath, prepare`

### Methods inherited from class net.minecraft.client.resources.ReloadListener

`reload`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface net.minecraft.resources.IFutureReloadListener

`getSimpleName`

### Field Detail

#### data

    protected java.util.Map<net.minecraft.util.ResourceLocation,T> data

The raw data that we parsed from json last time resources were reloaded

### Constructor Detail

#### SimpleJsonDataManager

    public SimpleJsonDataManager(java.lang.String folder,
                                 java.lang.Class<T> dataClass)

Parameters:
  `folder` - This is the name of the folders that the resource loader looks in, e.g. assets/modid/FOLDER
  `dataClass` - the class to convert jsons into

### Method Detail

#### getData

    public T getData(net.minecraft.util.ResourceLocation id)

Get the data object represented by the json at the given resource location

Parameters:
  `id` - The key for the object
Returns:
  The object, or null if there is no object associated with the given key

#### apply

    protected void apply(java.util.Map<net.minecraft.util.ResourceLocation,com.google.gson.JsonElement> jsons,
                         net.minecraft.resources.IResourceManager manager,
                         net.minecraft.profiler.IProfiler profiler)

Called on resource reload, the jsons have already been found for us and we just need to parse them in here

Specified by:
  `apply` in class `net.minecraft.client.resources.ReloadListener<java.util.Map<net.minecraft.util.ResourceLocation,com.google.gson.JsonElement>>`

#### getJsonAsData

    protected T getJsonAsData(com.google.gson.JsonElement json)

Use a json object (presumably one from an assets/modid/asset_type/ or data/modid/data_type/ folder) to generate a data object

Parameters:
  `json` - The json element that GSON parsed a json into
Returns:
  the finalized object

#### mapValues

    public static <KEY,IN,OUT> java.util.Map<KEY,OUT> mapValues(java.util.Map<KEY,IN> inputs,
                                                                java.util.function.Function<IN,OUT> mapper)

Converts all the values in a map to new values; the new map uses the same keys as the old map

Type Parameters:
  `KEY` - The type of the maps' keys
  `IN` - The type of the input map's values
  `OUT` - The type of the output map's values
Parameters:
  `inputs` - The input map
  `mapper` - A function that converts the input map's values to the output map's values
Returns:
  A map with the same keys as the input map but whose values have been transformed