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:
Cyborgmas authored and GitHub committed 2022-07-09 11:51:41 -04:00
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
@@ -1,48 +0,0 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.common.loot;
import com.google.gson.JsonObject;
import net.minecraft.advancements.critereon.SerializationContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.resources.ResourceLocation;
/**
* Abstract base deserializer for LootModifiers. Takes care of Forge registry things.<br/>
* Modders should extend this class to return their modifier and implement the abstract
* <code>read</code> method to deserialize from json.
* @param <T> the Type to deserialize
*/
public abstract class GlobalLootModifierSerializer<T extends IGlobalLootModifier> {
/**
* Most mods will likely not need more than<br/>
* <code>return new MyModifier(conditionsIn)</code><br/>
* but any additional properties that are needed will need to be deserialized here.
* @param location The resource location (if needed)
* @param object The full json object (including ILootConditions)
* @param ailootcondition An already deserialized list of ILootConditions
*/
public abstract T read(ResourceLocation location, JsonObject object, LootItemCondition[] ailootcondition);
/**
* Write the serializer to json.
*
* Most serializers won't have to do anything else than {@link #makeConditions}
* Which simply creates the JsonObject from an array of ILootConditions.
*/
public abstract JsonObject write(T instance);
/**
* Helper to create the json object from the conditions.
* Add any extra properties to the returned json.
*/
public JsonObject makeConditions(LootItemCondition[] conditions) {
JsonObject json = new JsonObject();
json.add("conditions", SerializationContext.INSTANCE.serializeConditions(conditions));
return json;
}
}
@@ -5,17 +5,69 @@
package net.minecraftforge.common.loot;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.JsonOps;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
/**
* Implementation that defines what a global loot modifier must implement in order to be functional.
* {@link LootModifier} Supplies base functionality; most modders should only need to extend that.<br/>
* Requires an {@link GlobalLootModifierSerializer} to be registered via json (see forge:loot_modifiers/global_loot_modifiers).
* Requires a {@link Codec} to be registered: {@link ForgeRegistries#LOOT_MODIFIER_SERIALIZERS}, and returned in {@link #codec()}
* Individual instances of modifiers must be registered via json, see forge:loot_modifiers/global_loot_modifiers
*/
public interface IGlobalLootModifier {
Codec<IGlobalLootModifier> DIRECT_CODEC = ExtraCodecs.lazyInitializedCodec(() -> ForgeRegistries.LOOT_MODIFIER_SERIALIZERS.get().getCodec())
.dispatch(IGlobalLootModifier::codec, Function.identity());
Codec<LootItemCondition[]> LOOT_CONDITIONS_CODEC = Codec.PASSTHROUGH.flatXmap(
d ->
{
try
{
LootItemCondition[] conditions = LootModifierManager.GSON_INSTANCE.fromJson(getJson(d), LootItemCondition[].class);
return DataResult.success(conditions);
}
catch (JsonSyntaxException e)
{
LootModifierManager.LOGGER.warn("Unable to decode loot conditions", e);
return DataResult.error(e.getMessage());
}
},
conditions ->
{
try
{
JsonElement element = LootModifierManager.GSON_INSTANCE.toJsonTree(conditions);
return DataResult.success(new Dynamic<>(JsonOps.INSTANCE, element));
}
catch (JsonSyntaxException e)
{
LootModifierManager.LOGGER.warn("Unable to encode loot conditions", e);
return DataResult.error(e.getMessage());
}
}
);
@SuppressWarnings("unchecked")
static <U> JsonElement getJson(Dynamic<?> dynamic) {
Dynamic<U> typed = (Dynamic<U>) dynamic;
return typed.getValue() instanceof JsonElement ?
(JsonElement) typed.getValue() :
typed.getOps().convertTo(JsonOps.INSTANCE, typed.getValue());
}
/**
* Applies the modifier to the list of generated loot. This function needs to be responsible for
* checking ILootConditions as well.
@@ -25,4 +77,9 @@ public interface IGlobalLootModifier {
*/
@NotNull
ObjectArrayList<ItemStack> apply(ObjectArrayList<ItemStack> generatedLoot, LootContext context);
/**
* Returns the registered codec for this modifier
*/
Codec<? extends IGlobalLootModifier> codec();
}
@@ -7,8 +7,11 @@ package net.minecraftforge.common.loot;
import java.util.function.Predicate;
import com.mojang.datafixers.Products;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.levelgen.feature.trunkplacers.BendingTrunkPlacer;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditions;
@@ -16,25 +19,42 @@ import org.jetbrains.annotations.NotNull;
/**
* A base implementation of a Global Loot Modifier for modders to extend.
* Takes care of ILootCondition matching and comes with a base serializer
* implementation that takes care of Forge registry things.
* Takes care of ILootCondition matching and comes with the base codec to extend.
*/
public abstract class LootModifier implements IGlobalLootModifier {
public abstract class LootModifier implements IGlobalLootModifier
{
protected final LootItemCondition[] conditions;
private final Predicate<LootContext> combinedConditions;
/**
* Simplifies codec creation, especially if no other fields are added:
* <p>
* {@code
* public static final Codec<MyLootModifier> CODEC = RecordCodecBuilder.create(inst -> codecStart(inst).apply(inst, MyLootModifier::new));
* }
* </p>
* Otherwise can follow this with #and() to add more fields.
* Examples: Forge Test Subclasses or {@link BendingTrunkPlacer#CODEC}
*/
protected static <T extends LootModifier> Products.P1<RecordCodecBuilder.Mu<T>, LootItemCondition[]> codecStart(RecordCodecBuilder.Instance<T> instance)
{
return instance.group(LOOT_CONDITIONS_CODEC.fieldOf("conditions").forGetter(lm -> lm.conditions));
}
/**
* Constructs a LootModifier.
* @param conditionsIn the ILootConditions that need to be matched before the loot is modified.
*/
protected LootModifier(LootItemCondition[] conditionsIn) {
protected LootModifier(LootItemCondition[] conditionsIn)
{
this.conditions = conditionsIn;
this.combinedConditions = LootItemConditions.andConditions(conditionsIn);
}
@NotNull
@Override
public final ObjectArrayList<ItemStack> apply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
public final ObjectArrayList<ItemStack> apply(ObjectArrayList<ItemStack> generatedLoot, LootContext context)
{
return this.combinedConditions.test(context) ? this.doApply(generatedLoot, context) : generatedLoot;
}
@@ -6,19 +6,17 @@
package net.minecraftforge.common.loot;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.*;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import net.minecraft.world.level.storage.loot.Deserializers;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -39,7 +37,7 @@ import net.minecraftforge.registries.ForgeRegistries;
public class LootModifierManager extends SimpleJsonResourceReloadListener {
public static final Logger LOGGER = LogManager.getLogger();
private static final Gson GSON_INSTANCE = Deserializers.createFunctionSerializer().create();
public static final Gson GSON_INSTANCE = Deserializers.createFunctionSerializer().create();
private Map<ResourceLocation, IGlobalLootModifier> registeredLootModifiers = ImmutableMap.of();
private static final String folder = "loot_modifiers";
@@ -51,22 +49,7 @@ public class LootModifierManager extends SimpleJsonResourceReloadListener {
@Override
protected void apply(Map<ResourceLocation, JsonElement> resourceList, ResourceManager resourceManagerIn, ProfilerFiller profilerIn) {
Builder<ResourceLocation, IGlobalLootModifier> builder = ImmutableMap.builder();
//old way (for reference)
/*Map<IGlobalLootModifier, ResourceLocation> toLocation = new HashMap<IGlobalLootModifier, ResourceLocation>();
resourceList.forEach((location, object) -> {
try {
IGlobalLootModifier modifier = deserializeModifier(location, object);
builder.put(location, modifier);
toLocation.put(modifier, location);
} catch (Exception exception) {
LOGGER.error("Couldn't parse loot modifier {}", location, exception);
}
});
builder.orderEntriesByValue((x,y) -> {
return toLocation.get(x).compareTo(toLocation.get(y));
});*/
//new way
ArrayList<ResourceLocation> finalLocations = new ArrayList<ResourceLocation>();
List<ResourceLocation> finalLocations = new ArrayList<>();
ResourceLocation resourcelocation = new ResourceLocation("forge","loot_modifiers/global_loot_modifiers.json");
//read in all data files from forge:loot_modifiers/global_loot_modifiers in order to do layering
for(Resource iresource : resourceManagerIn.getResourceStack(resourcelocation)) {
@@ -75,13 +58,13 @@ public class LootModifierManager extends SimpleJsonResourceReloadListener {
) {
JsonObject jsonobject = GsonHelper.fromJson(GSON_INSTANCE, reader, JsonObject.class);
boolean replace = jsonobject.get("replace").getAsBoolean();
if(replace) finalLocations.clear();
if (replace)
finalLocations.clear();
JsonArray entryList = jsonobject.get("entries").getAsJsonArray();
for(JsonElement entry : entryList) {
String loc = entry.getAsString();
ResourceLocation res = new ResourceLocation(loc);
if(finalLocations.contains(res)) finalLocations.remove(res);
finalLocations.add(res);
ResourceLocation loc = new ResourceLocation(entry.getAsString());
finalLocations.remove(loc); //remove and re-add if needed, to update the ordering.
finalLocations.add(loc);
}
}
@@ -90,31 +73,25 @@ public class LootModifierManager extends SimpleJsonResourceReloadListener {
}
}
//use layered config to fetch modifier data files (modifiers missing from config are disabled)
finalLocations.forEach(location -> {
try {
IGlobalLootModifier modifier = deserializeModifier(location, resourceList.get(location));
if(modifier != null)
builder.put(location, modifier);
} catch (Exception exception) {
LOGGER.error("Couldn't parse loot modifier {}", location, exception);
for (ResourceLocation location : finalLocations)
{
Codec<? extends IGlobalLootModifier> codec = ForgeRegistries.LOOT_MODIFIER_SERIALIZERS.get().getValue(location);
if (codec == null)
{
LOGGER.warn("Unknown or unregistered codec for GlobalLootModifier: {}", location);
continue;
}
});
ImmutableMap<ResourceLocation, IGlobalLootModifier> immutablemap = builder.build();
this.registeredLootModifiers = immutablemap;
}
private IGlobalLootModifier deserializeModifier(ResourceLocation location, JsonElement element) {
if (!element.isJsonObject()) return null;
JsonObject object = element.getAsJsonObject();
LootItemCondition[] lootConditions = GSON_INSTANCE.fromJson(object.get("conditions"), LootItemCondition[].class);
ResourceLocation serializer = new ResourceLocation(GsonHelper.getAsString(object, "type"));
return ForgeRegistries.LOOT_MODIFIER_SERIALIZERS.get().getValue(serializer).read(location, object, lootConditions);
}
public static GlobalLootModifierSerializer<?> getSerializerForName(ResourceLocation resourcelocation) {
return ForgeRegistries.LOOT_MODIFIER_SERIALIZERS.get().getValue(resourcelocation);
JsonElement json = resourceList.get(location);
IGlobalLootModifier modifier = codec.decode(JsonOps.INSTANCE, json).get()
.map(Pair::getFirst, partial ->
{
LOGGER.warn("Could not decode GlobalLootModifier with serializer: {} - error: {}", location, partial.message());
return null;
});
if(modifier != null)
builder.put(location, modifier);
}
this.registeredLootModifiers = builder.build();
}
/**
@@ -41,7 +41,7 @@ import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacerType;
import net.minecraft.world.level.levelgen.feature.treedecorators.TreeDecoratorType;
import net.minecraftforge.common.Tags;
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
import net.minecraftforge.common.loot.IGlobalLootModifier;
import net.minecraftforge.common.world.BiomeModifier;
import net.minecraftforge.fluids.FluidType;
@@ -97,8 +97,8 @@ public class ForgeRegistries
// Custom forge registries
static final DeferredRegister<EntityDataSerializer<?>> DEFERRED_DATA_SERIALIZERS = DeferredRegister.create(Keys.DATA_SERIALIZERS, Keys.DATA_SERIALIZERS.location().getNamespace());
public static final Supplier<IForgeRegistry<EntityDataSerializer<?>>> DATA_SERIALIZERS = DEFERRED_DATA_SERIALIZERS.makeRegistry(GameData::getDataSerializersRegistryBuilder);
static final DeferredRegister<GlobalLootModifierSerializer<?>> DEFERRED_LOOT_MODIFIER_SERIALIZERS = DeferredRegister.create(Keys.LOOT_MODIFIER_SERIALIZERS, Keys.LOOT_MODIFIER_SERIALIZERS.location().getNamespace());
public static final Supplier<IForgeRegistry<GlobalLootModifierSerializer<?>>> LOOT_MODIFIER_SERIALIZERS = DEFERRED_LOOT_MODIFIER_SERIALIZERS.makeRegistry(GameData::getGLMSerializersRegistryBuilder);
static final DeferredRegister<Codec<? extends IGlobalLootModifier>> DEFERRED_LOOT_MODIFIER_SERIALIZERS = DeferredRegister.create(Keys.LOOT_MODIFIER_SERIALIZERS, Keys.LOOT_MODIFIER_SERIALIZERS.location().getNamespace());
public static final Supplier<IForgeRegistry<Codec<? extends IGlobalLootModifier>>> LOOT_MODIFIER_SERIALIZERS = DEFERRED_LOOT_MODIFIER_SERIALIZERS.makeRegistry(GameData::getGLMSerializersRegistryBuilder);
static final DeferredRegister<Codec<? extends BiomeModifier>> DEFERRED_BIOME_MODIFIER_SERIALIZERS = DeferredRegister.create(Keys.BIOME_MODIFIER_SERIALIZERS, Keys.BIOME_MODIFIER_SERIALIZERS.location().getNamespace());
public static final Supplier<IForgeRegistry<Codec<? extends BiomeModifier>>> BIOME_MODIFIER_SERIALIZERS = DEFERRED_BIOME_MODIFIER_SERIALIZERS.makeRegistry(() -> new RegistryBuilder<Codec<? extends BiomeModifier>>().disableSaving().disableSync());
static final DeferredRegister<Codec<? extends StructureModifier>> DEFERRED_STRUCTURE_MODIFIER_SERIALIZERS = DeferredRegister.create(Keys.STRUCTURE_MODIFIER_SERIALIZERS, Keys.STRUCTURE_MODIFIER_SERIALIZERS.location().getNamespace());
@@ -148,7 +148,7 @@ public class ForgeRegistries
// Forge
public static final ResourceKey<Registry<EntityDataSerializer<?>>> DATA_SERIALIZERS = key("forge:data_serializers");
public static final ResourceKey<Registry<GlobalLootModifierSerializer<?>>> LOOT_MODIFIER_SERIALIZERS = key("forge:loot_modifier_serializers");
public static final ResourceKey<Registry<Codec<? extends IGlobalLootModifier>>> LOOT_MODIFIER_SERIALIZERS = key("forge:loot_modifier_serializers");
public static final ResourceKey<Registry<Codec<? extends BiomeModifier>>> BIOME_MODIFIER_SERIALIZERS = key("forge:biome_modifier_serializers");
public static final ResourceKey<Registry<Codec<? extends StructureModifier>>> STRUCTURE_MODIFIER_SERIALIZERS = key("forge:structure_modifier_serializers");
public static final ResourceKey<Registry<FluidType>> FLUID_TYPES = key("forge:fluid_type");
@@ -11,6 +11,7 @@ import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.mojang.serialization.Codec;
import com.mojang.serialization.Lifecycle;
import java.util.LinkedHashSet;
import net.minecraft.core.DefaultedRegistry;
@@ -37,7 +38,7 @@ import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
import net.minecraftforge.common.loot.IGlobalLootModifier;
import net.minecraftforge.common.util.LogMessageAdapter;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fml.ModLoader;
@@ -142,7 +143,7 @@ public class GameData
return makeRegistry(DATA_SERIALIZERS, 256 /*vanilla space*/, MAX_VARINT).disableSaving().disableOverrides();
}
static RegistryBuilder<GlobalLootModifierSerializer<?>> getGLMSerializersRegistryBuilder()
static RegistryBuilder<Codec<? extends IGlobalLootModifier>> getGLMSerializersRegistryBuilder()
{
return makeRegistry(LOOT_MODIFIER_SERIALIZERS).disableSaving().disableSync();
}