mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-13 16:54:22 +00:00
Add dataprovider for Global Loot Modifiers (#6960)
This commit is contained in:
1 parent
051416b823
commit
edf41bda19
7 files changed
+268
-26
No files matched your search
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2020.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.data;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.DirectoryCache;
|
||||
import net.minecraft.data.IDataProvider;
|
||||
import net.minecraft.util.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 java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Provider for forge's GlobalLootModifier system. See {@link LootModifier} and {@link GlobalLootModifierSerializer}.
|
||||
*
|
||||
* This provider only requires implementing {@link #start()} and calling {@link #add} from it.
|
||||
*/
|
||||
public abstract class GlobalLootModifierProvider implements IDataProvider
|
||||
{
|
||||
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 boolean replace = false;
|
||||
|
||||
public GlobalLootModifierProvider(DataGenerator gen, String modid)
|
||||
{
|
||||
this.gen = gen;
|
||||
this.modid = modid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the "replace" key in global_loot_modifiers to true.
|
||||
*/
|
||||
protected void replacing()
|
||||
{
|
||||
this.replace = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call {@link #add} here, which will pass in the necessary information to write the jsons.
|
||||
*/
|
||||
protected abstract void start();
|
||||
|
||||
@Override
|
||||
public void act(DirectoryCache cache) throws IOException
|
||||
{
|
||||
start();
|
||||
|
||||
Path forgePath = gen.getOutputFolder().resolve("data/forge/loot_modifiers/global_loot_modifiers.json");
|
||||
String modPath = "data/" + modid + "/loot_modifiers/";
|
||||
List<ResourceLocation> entries = new ArrayList<>();
|
||||
|
||||
toSerialize.forEach(LamdbaExceptionUtils.rethrowBiConsumer((name, pair) ->
|
||||
{
|
||||
entries.add(new ResourceLocation(modid, name));
|
||||
Path modifierPath = gen.getOutputFolder().resolve(modPath + name + ".json");
|
||||
|
||||
JsonObject json = pair.getB();
|
||||
json.addProperty("type", pair.getA().getRegistryName().toString());
|
||||
|
||||
IDataProvider.save(GSON, cache, json, modifierPath);
|
||||
}));
|
||||
|
||||
JsonObject forgeJson = new JsonObject();
|
||||
forgeJson.addProperty("replace", this.replace);
|
||||
forgeJson.add("entries", GSON.toJsonTree(entries.stream().map(ResourceLocation::toString).collect(Collectors.toList())));
|
||||
|
||||
IDataProvider.save(GSON, cache, forgeJson, forgePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 conditions The loot conditions before {@link LootModifier#doApply} is called.
|
||||
*/
|
||||
public <T extends IGlobalLootModifier> void add(String modifier, GlobalLootModifierSerializer<T> serializer, T instance)
|
||||
{
|
||||
this.toSerialize.put(modifier, new Tuple<>(serializer, serializer.write(instance)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return "Global Loot Modifiers : " + modid;
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ package net.minecraftforge.common.loot;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.loot.ConditionArraySerializer;
|
||||
import net.minecraft.loot.conditions.ILootCondition;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.registries.GameData;
|
||||
@@ -64,6 +65,24 @@ public abstract class GlobalLootModifierSerializer<T extends IGlobalLootModifier
|
||||
*/
|
||||
public abstract T read(ResourceLocation location, JsonObject object, ILootCondition[] 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(ILootCondition[] conditions) {
|
||||
JsonObject json = new JsonObject();
|
||||
json.add("conditions", ConditionArraySerializer.field_235679_a_.func_235681_a_(conditions));
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by Forge's registry system.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user