mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-19 11:59:25 +00:00
1.19 - Remove GlobalLootModifierSerializer and move to Codecs (#8721)
* convert GLM serializer class to codec * cleanup * GLM list needs to be sorted * datagen * simplify serialization * fix test mods (oops) * properly use suppliers for codec as they are registry obj
This commit is contained in:
1 parent
1134fac54c
commit
0c030ee5e1
9 files changed
+174
-199
No files matched your search
@@ -7,18 +7,16 @@ package net.minecraftforge.common.data;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
|
||||
import net.minecraft.data.CachedOutput;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.HashCache;
|
||||
import net.minecraft.data.DataProvider;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.util.Tuple;
|
||||
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
|
||||
import net.minecraftforge.common.loot.IGlobalLootModifier;
|
||||
import net.minecraftforge.common.loot.LootModifier;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
@@ -29,7 +27,7 @@ import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Provider for forge's GlobalLootModifier system. See {@link LootModifier} and {@link GlobalLootModifierSerializer}.
|
||||
* Provider for forge's GlobalLootModifier system. See {@link LootModifier}
|
||||
*
|
||||
* This provider only requires implementing {@link #start()} and calling {@link #add} from it.
|
||||
*/
|
||||
@@ -38,7 +36,7 @@ public abstract class GlobalLootModifierProvider implements DataProvider
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final DataGenerator gen;
|
||||
private final String modid;
|
||||
private final Map<String, Tuple<GlobalLootModifierSerializer<?>, JsonObject>> toSerialize = new HashMap<>();
|
||||
private final Map<String, JsonElement> toSerialize = new HashMap<>();
|
||||
private boolean replace = false;
|
||||
|
||||
public GlobalLootModifierProvider(DataGenerator gen, String modid)
|
||||
@@ -69,14 +67,10 @@ public abstract class GlobalLootModifierProvider implements DataProvider
|
||||
String modPath = "data/" + modid + "/loot_modifiers/";
|
||||
List<ResourceLocation> entries = new ArrayList<>();
|
||||
|
||||
toSerialize.forEach(LamdbaExceptionUtils.rethrowBiConsumer((name, pair) ->
|
||||
toSerialize.forEach(LamdbaExceptionUtils.rethrowBiConsumer((name, json) ->
|
||||
{
|
||||
entries.add(new ResourceLocation(modid, name));
|
||||
Path modifierPath = gen.getOutputFolder().resolve(modPath + name + ".json");
|
||||
|
||||
JsonObject json = pair.getB();
|
||||
json.addProperty("type", ForgeRegistries.LOOT_MODIFIER_SERIALIZERS.get().getKey(pair.getA()).toString());
|
||||
|
||||
DataProvider.saveStable(cache, json, modifierPath);
|
||||
}));
|
||||
|
||||
@@ -91,11 +85,12 @@ public abstract class GlobalLootModifierProvider implements DataProvider
|
||||
* Passes in the data needed to create the file without any extra objects.
|
||||
*
|
||||
* @param modifier The name of the modifier, which will be the file name.
|
||||
* @param serializer The serializer of this modifier.
|
||||
* @param instance The instance to serialize
|
||||
*/
|
||||
public <T extends IGlobalLootModifier> void add(String modifier, GlobalLootModifierSerializer<T> serializer, T instance)
|
||||
public <T extends IGlobalLootModifier> void add(String modifier, T instance)
|
||||
{
|
||||
this.toSerialize.put(modifier, new Tuple<>(serializer, serializer.write(instance)));
|
||||
JsonElement json = IGlobalLootModifier.DIRECT_CODEC.encodeStart(JsonOps.INSTANCE, instance).getOrThrow(false, s -> {});
|
||||
this.toSerialize.put(modifier, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user