Add dataprovider for Global Loot Modifiers (#6960)
This commit is contained in:
parent
051416b823
commit
edf41bda19
7 changed files with 268 additions and 26 deletions
|
@ -349,7 +349,8 @@ project(':forge') {
|
|||
|
||||
args '--flat', '--all', '--validate',
|
||||
'--mod', 'data_gen_test',
|
||||
'--mod', 'piston_event_test',
|
||||
'--mod', 'piston_event_test',
|
||||
'--mod', 'global_loot_test',
|
||||
'--output', rootProject.file('src/generated_test/resources/'),
|
||||
'--existing', sourceSets.main.resources.srcDirs[0]
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"replace": false,
|
||||
"entries": [
|
||||
"global_loot_test:wheat_harvest",
|
||||
"global_loot_test:smelting"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "minecraft:match_tool",
|
||||
"predicate": {
|
||||
"enchantments": [
|
||||
{
|
||||
"enchantment": "global_loot_test:smelt",
|
||||
"levels": {
|
||||
"min": 1
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"type": "global_loot_test:smelting"
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "minecraft:match_tool",
|
||||
"predicate": {
|
||||
"item": "minecraft:shears"
|
||||
}
|
||||
},
|
||||
{
|
||||
"condition": "minecraft:block_state_property",
|
||||
"block": "minecraft:wheat"
|
||||
}
|
||||
],
|
||||
"numSeeds": 3,
|
||||
"seedItem": "minecraft:wheat_seeds",
|
||||
"replacement": "minecraft:wheat",
|
||||
"type": "global_loot_test:wheat_harvest"
|
||||
}
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -19,13 +19,12 @@
|
|||
|
||||
package net.minecraftforge.debug.gameplay.loot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.advancements.criterion.EnchantmentPredicate;
|
||||
import net.minecraft.advancements.criterion.ItemPredicate;
|
||||
import net.minecraft.advancements.criterion.MinMaxBounds;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.enchantment.Enchantment;
|
||||
import net.minecraft.enchantment.Enchantment.Rarity;
|
||||
import net.minecraft.enchantment.EnchantmentHelper;
|
||||
|
@ -35,51 +34,93 @@ import net.minecraft.inventory.EquipmentSlotType;
|
|||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.item.crafting.FurnaceRecipe;
|
||||
import net.minecraft.item.crafting.IRecipeType;
|
||||
import net.minecraft.loot.LootContext;
|
||||
import net.minecraft.loot.LootParameterSets;
|
||||
import net.minecraft.loot.LootParameters;
|
||||
import net.minecraft.loot.LootTable;
|
||||
import net.minecraft.loot.conditions.BlockStateProperty;
|
||||
import net.minecraft.loot.conditions.ILootCondition;
|
||||
import net.minecraft.loot.conditions.MatchTool;
|
||||
import net.minecraft.util.JSONUtils;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.data.GlobalLootModifierProvider;
|
||||
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
|
||||
import net.minecraftforge.common.loot.LootModifier;
|
||||
import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
|
||||
import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.registries.ObjectHolder;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Mod(GlobalLootModifiersTest.MODID)
|
||||
public class GlobalLootModifiersTest {
|
||||
public static final String MODID = "global_loot_test";
|
||||
public static final boolean ENABLE = true;
|
||||
@ObjectHolder(value = MODID)
|
||||
public static final Enchantment smelt = null;
|
||||
public GlobalLootModifiersTest() { }
|
||||
public static final boolean ENABLE = false;
|
||||
|
||||
public GlobalLootModifiersTest()
|
||||
{
|
||||
if(ENABLE)
|
||||
{
|
||||
GLM.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
ENCHANTS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
}
|
||||
}
|
||||
|
||||
private static final DeferredRegister<GlobalLootModifierSerializer<?>> GLM = DeferredRegister.create(ForgeRegistries.LOOT_MODIFIER_SERIALIZERS, MODID);
|
||||
private static final DeferredRegister<Enchantment> ENCHANTS = DeferredRegister.create(ForgeRegistries.ENCHANTMENTS, MODID);
|
||||
|
||||
private static final RegistryObject<SmeltingEnchantmentModifier.Serializer> SMELTING = GLM.register("smelting", SmeltingEnchantmentModifier.Serializer::new);
|
||||
private static final RegistryObject<WheatSeedsConverterModifier.Serializer> WHEATSEEDS = GLM.register("wheat_harvest", WheatSeedsConverterModifier.Serializer::new);
|
||||
private static final RegistryObject<SilkTouchTestModifier.Serializer> SILKTOUCH = GLM.register("silk_touch_bamboo", SilkTouchTestModifier.Serializer::new);
|
||||
private static final RegistryObject<Enchantment> SMELT = ENCHANTS.register("smelt", () -> new SmelterEnchantment(Rarity.UNCOMMON, EnchantmentType.DIGGER, EquipmentSlotType.MAINHAND));
|
||||
|
||||
@EventBusSubscriber(modid = MODID, bus = EventBusSubscriber.Bus.MOD)
|
||||
public static class EventHandlers {
|
||||
@SubscribeEvent
|
||||
public static void registerEnchantments(@Nonnull final RegistryEvent.Register<Enchantment> event) {
|
||||
if (ENABLE) {
|
||||
event.getRegistry().register(new SmelterEnchantment(Rarity.UNCOMMON, EnchantmentType.DIGGER, new EquipmentSlotType[] {EquipmentSlotType.MAINHAND}).setRegistryName(new ResourceLocation(MODID,"smelt")));
|
||||
}
|
||||
public static void runData(GatherDataEvent event)
|
||||
{
|
||||
if(ENABLE)
|
||||
event.getGenerator().addProvider(new DataProvider(event.getGenerator(), MODID));
|
||||
}
|
||||
}
|
||||
|
||||
private static class DataProvider extends GlobalLootModifierProvider
|
||||
{
|
||||
public DataProvider(DataGenerator gen, String modid)
|
||||
{
|
||||
super(gen, modid);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void registerModifierSerializers(@Nonnull final RegistryEvent.Register<GlobalLootModifierSerializer<?>> event) {
|
||||
if (ENABLE) {
|
||||
event.getRegistry().register(
|
||||
new WheatSeedsConverterModifier.Serializer().setRegistryName(new ResourceLocation(MODID,"wheat_harvest"))
|
||||
);
|
||||
event.getRegistry().register(new SmeltingEnchantmentModifier.Serializer().setRegistryName(new ResourceLocation(MODID,"smelting")));
|
||||
event.getRegistry().register(new SilkTouchTestModifier.Serializer().setRegistryName(new ResourceLocation(MODID,"silk_touch_bamboo")));
|
||||
}
|
||||
@Override
|
||||
protected void start()
|
||||
{
|
||||
add("smelting", SMELTING.get(), new SmeltingEnchantmentModifier(
|
||||
new ILootCondition[]{
|
||||
MatchTool.builder(
|
||||
ItemPredicate.Builder.create().enchantment(
|
||||
new EnchantmentPredicate(SMELT.get(), MinMaxBounds.IntBound.atLeast(1))))
|
||||
.build()
|
||||
})
|
||||
);
|
||||
|
||||
add("wheat_harvest", WHEATSEEDS.get(), new WheatSeedsConverterModifier(
|
||||
new ILootCondition[] {
|
||||
MatchTool.builder(ItemPredicate.Builder.create().item(Items.SHEARS)).build(),
|
||||
BlockStateProperty.builder(Blocks.WHEAT).build()
|
||||
},
|
||||
3, Items.WHEAT_SEEDS, Items.WHEAT)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -119,6 +160,11 @@ public class GlobalLootModifiersTest {
|
|||
public SmeltingEnchantmentModifier read(ResourceLocation name, JsonObject json, ILootCondition[] conditionsIn) {
|
||||
return new SmeltingEnchantmentModifier(conditionsIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject write(SmeltingEnchantmentModifier instance) {
|
||||
return makeConditions(instance.conditions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -151,6 +197,11 @@ public class GlobalLootModifiersTest {
|
|||
public SilkTouchTestModifier read(ResourceLocation name, JsonObject json, ILootCondition[] conditionsIn) {
|
||||
return new SilkTouchTestModifier(conditionsIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject write(SilkTouchTestModifier instance) {
|
||||
return makeConditions(instance.conditions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -201,6 +252,15 @@ public class GlobalLootModifiersTest {
|
|||
Item wheat = ForgeRegistries.ITEMS.getValue(new ResourceLocation(JSONUtils.getString(object, "replacement")));
|
||||
return new WheatSeedsConverterModifier(conditionsIn, numSeeds, seed, wheat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject write(WheatSeedsConverterModifier instance) {
|
||||
JsonObject json = makeConditions(instance.conditions);
|
||||
json.addProperty("numSeeds", instance.numSeedsToConvert);
|
||||
json.addProperty("seedItem", ForgeRegistries.ITEMS.getKey(instance.itemToCheck).toString());
|
||||
json.addProperty("replacement", ForgeRegistries.ITEMS.getKey(instance.itemReward).toString());
|
||||
return json;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue