Package commoble.databuddy.data

# Class CodecJsonDataManager<T>

java.lang.Object
net.minecraft.server.packs.resources.SimplePreparableReloadListener<Map<net.minecraft.resources.ResourceLocation,com.google.gson.JsonElement>>
net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener
commoble.databuddy.data.CodecJsonDataManager<T>

Type Parameters:
  `T` - The type of the objects that the codec is parsing jsons as

All Implemented Interfaces:
  `net.minecraft.server.packs.resources.PreparableReloadListener`

public class CodecJsonDataManager<T> extends net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener
Codec-based data manager for loading data. This works best if initialized during your mod's construction. After creating the manager, subscribeAsSyncable can optionally be called on it to subscribe the manager to the forge events necessary for syncing datapack data to clients.

## Nested Class Summary

## Nested classes/interfaces inherited from interface net.minecraft.server.packs.resources.PreparableReloadListener

`net.minecraft.server.packs.resources.PreparableReloadListener.PreparationBarrier`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected Map<net.minecraft.resources.ResourceLocation,T>`
`data`
The raw data that we parsed from json last time resources were reloaded

## Constructor Summary

Constructors
Constructor
Description
`CodecJsonDataManager(String folderName, com.mojang.serialization.Codec<T> codec, org.apache.logging.log4j.Logger logger)`
Creates a data manager with a standard gson parser
`CodecJsonDataManager(String folderName, com.mojang.serialization.Codec<T> codec, org.apache.logging.log4j.Logger logger, com.google.gson.Gson gson)`
As above but with a custom GSON

## Method Summary

Modifier and Type
Method
Description
`protected void`
`apply(Map<net.minecraft.resources.ResourceLocation,com.google.gson.JsonElement> jsons, net.minecraft.server.packs.resources.ResourceManager resourceManager, net.minecraft.util.profiling.ProfilerFiller profiler)`
`T`
`getData(net.minecraft.resources.ResourceLocation id)`
Get the data object for the given key
`<PACKET> CodecJsonDataManager<T>`
`subscribeAsSyncable(net.minecraftforge.network.simple.SimpleChannel channel, Function<Map<net.minecraft.resources.ResourceLocation,T>,PACKET> packetFactory)`
This should be called at most once, during construction of your mod (static init of your main mod class is fine) (FMLCommonSetupEvent *may* work as well) Calling this method automatically subscribes a packet-sender to `OnDatapackSyncEvent`.

### Methods inherited from class net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener

`getPreparedPath, prepare`

### Methods inherited from class net.minecraft.server.packs.resources.SimplePreparableReloadListener

`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.server.packs.resources.PreparableReloadListener

`getName`

## Field Details

### data

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

## Constructor Details

### CodecJsonDataManager

public CodecJsonDataManager(String folderName, com.mojang.serialization.Codec<T> codec, org.apache.logging.log4j.Logger logger)
Creates a data manager with a standard gson parser

Parameters:
  `folderName` - The name of the data folder that we will load from, vanilla folderNames are "recipes", "loot_tables", etc
  Jsons will be read from data/all_modids/folderName/all_jsons
  folderName can include subfolders, e.g. "some_mod_that_adds_lots_of_data_loaders/cheeses"
  `codec` - A codec to deserialize the json into your T, see javadocs above class
  `logger` - A logger that will log json parsing problems when they are caught.

### CodecJsonDataManager

public CodecJsonDataManager(String folderName, com.mojang.serialization.Codec<T> codec, org.apache.logging.log4j.Logger logger, com.google.gson.Gson gson)
As above but with a custom GSON

Parameters:
  `folderName` - The name of the data folder that we will load from, vanilla folderNames are "recipes", "loot_tables", etc
  Jsons will be read from data/all_modids/folderName/all_jsons
  folderName can include subfolders, e.g. "some_mod_that_adds_lots_of_data_loaders/cheeses"
  `codec` - A codec to deserialize the json into your T, see javadocs above class
  `logger` - A logger that will log json parsing problems when they are caught.
  `gson` - A gson for parsing the raw json data into JsonElements. JsonElement-to-T conversion will be done by the codec, so gson type adapters shouldn't be necessary here

## Method Details

### getData

@Nullable public T getData(net.minecraft.resources.ResourceLocation id)
Get the data object for the given key

Parameters:
  `id` - A resourcelocation identifying a json; e.g. a json at data/some_modid/folderName/some_json.json has id "some_modid:some_json"
Returns:
  The java object that was deserializd from the json with the given ID, or null if no such object is associated with that ID

### apply

protected void apply(Map<net.minecraft.resources.ResourceLocation,com.google.gson.JsonElement> jsons, net.minecraft.server.packs.resources.ResourceManager resourceManager, net.minecraft.util.profiling.ProfilerFiller profiler)

Specified by:
  `apply` in class `net.minecraft.server.packs.resources.SimplePreparableReloadListener<Map<net.minecraft.resources.ResourceLocation,com.google.gson.JsonElement>>`

### subscribeAsSyncable

public <PACKET> CodecJsonDataManager<T> subscribeAsSyncable(net.minecraftforge.network.simple.SimpleChannel channel, Function<Map<net.minecraft.resources.ResourceLocation,T>,PACKET> packetFactory)
This should be called at most once, during construction of your mod (static init of your main mod class is fine) (FMLCommonSetupEvent *may* work as well) Calling this method automatically subscribes a packet-sender to `OnDatapackSyncEvent`.

Type Parameters:
  `PACKET` - the packet type that will be sent on the given channel
Parameters:
  `channel` - The networking channel of your mod
  `packetFactory` - A packet constructor or factory method that converts the given map to a packet object to send on the given channel
Returns:
  this manager object