Revive ConditionalRecipe and ConditionalAdvancement.

Also directly add conditional support to all vanilla recipe data generators.
This commit is contained in:
LexManos committed 2023-10-06 21:01:03 -07:00
1 parent 90dafb0df0
commit 587b684035
49 files changed
+1826 -512

No files matched your search

@@ -0,0 +1,353 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.gameplay.crafting;
import net.minecraft.data.PackOutput;
import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.data.recipes.RecipeProvider;
import net.minecraft.data.recipes.ShapedRecipeBuilder;
import net.minecraft.data.recipes.ShapelessRecipeBuilder;
import net.minecraft.data.recipes.SimpleCookingRecipeBuilder;
import net.minecraft.data.recipes.SingleItemRecipeBuilder;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.Container;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.block.Blocks;
import net.minecraftforge.common.crafting.ConditionalRecipe;
import net.minecraftforge.common.crafting.SimpleCraftingContainer;
import net.minecraftforge.common.crafting.conditions.IConditionBuilder;
import net.minecraftforge.data.event.GatherDataEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.gametest.GameTestHolder;
import net.minecraftforge.test.BaseTestMod;
@GameTestHolder("forge." + ConditionalRecipeTest.MODID)
@Mod(ConditionalRecipeTest.MODID)
public class ConditionalRecipeTest extends BaseTestMod {
static final String MODID = "conditional_recipe";
@SubscribeEvent
public void gatherData(GatherDataEvent event) {
var gen = event.getGenerator();
gen.addProvider(event.includeServer(), new Recipes(gen.getPackOutput()));
}
private static ResourceLocation rl(String path) {
return new ResourceLocation(MODID, path);
}
private static <C extends Container, T extends Recipe<C>> void assertFalse(GameTestHelper helper, RecipeType<T> type, C container) {
var recipe = helper.getLevel().getRecipeManager().getRecipeFor(type, container, helper.getLevel());
helper.assertTrue(recipe.isEmpty(), () -> "Found crafting recipe when unexpected: " + recipe.get().id());
helper.succeed();
}
private static <C extends Container, T extends Recipe<C>> void assertTrue(GameTestHelper helper, RecipeType<T> type, C container) {
var recipe = helper.getLevel().getRecipeManager().getRecipeFor(type, container, helper.getLevel());
helper.assertTrue(recipe.isPresent(), "Did not find crafting recipe when expected!");
helper.assertTrue(recipe.get().id().getNamespace().equals(MODID), "It wasn't our recipe: " + recipe.get().id());
helper.succeed();
}
@GameTest(template = "forge:empty3x3x3")
public static void shaped_false_conditions(GameTestHelper helper) {
assertFalse(helper, RecipeType.CRAFTING, SimpleCraftingContainer.builder()
.pattern(
"XXX",
"XXX",
"XXX"
)
.define('X', Blocks.DIRT)
.build()
);
}
@GameTest(template = "forge:empty3x3x3")
public static void shaped_true_conditions(GameTestHelper helper) {
assertTrue(helper, RecipeType.CRAFTING, SimpleCraftingContainer.builder()
.pattern(
"XXX",
"XXX",
"XXX"
)
.define('X', Blocks.OAK_LOG)
.build()
);
}
@GameTest(template = "forge:empty3x3x3")
public static void shapeless_false_conditions(GameTestHelper helper) {
assertFalse(helper, RecipeType.CRAFTING, SimpleCraftingContainer.builder()
.pattern("XXX")
.define('X', Blocks.REDSTONE_ORE)
.build()
);
}
@GameTest(template = "forge:empty3x3x3")
public static void shapeless_true_conditions(GameTestHelper helper) {
assertTrue(helper, RecipeType.CRAFTING, SimpleCraftingContainer.builder()
.pattern("XXXX")
.define('X', Blocks.REDSTONE_ORE)
.build()
);
}
@GameTest(template = "forge:empty3x3x3")
public static void cooking_false_conditions(GameTestHelper helper) {
assertFalse(helper, RecipeType.SMELTING, SimpleCraftingContainer.builder()
.pattern("X")
.define('X', Blocks.DIRT)
.build()
);
}
@GameTest(template = "forge:empty3x3x3")
public static void cooking_true_conditions(GameTestHelper helper) {
assertTrue(helper, RecipeType.SMELTING, SimpleCraftingContainer.builder()
.pattern("X")
.define('X', Blocks.BEE_NEST)
.build()
);
}
@GameTest(template = "forge:empty3x3x3")
public static void single_item_false_conditions(GameTestHelper helper) {
assertFalse(helper, RecipeType.STONECUTTING, SimpleCraftingContainer.builder()
.pattern("X")
.define('X', Blocks.DIRT)
.build()
);
}
@GameTest(template = "forge:empty3x3x3")
public static void single_item_true_conditions(GameTestHelper helper) {
assertTrue(helper, RecipeType.STONECUTTING, SimpleCraftingContainer.builder()
.pattern("X")
.define('X', Blocks.REDSTONE_ORE)
.build()
);
}
@GameTest(template = "forge:empty3x3x3")
public static void conditional_recipe_choice(GameTestHelper helper) {
assertFalse(helper, RecipeType.CRAFTING, SimpleCraftingContainer.builder()
.pattern("XXX")
.pattern("XX ")
.define('X', Blocks.DIRT)
.build()
);
assertTrue(helper, RecipeType.CRAFTING, SimpleCraftingContainer.builder()
.pattern("XXX")
.pattern("XX ")
.define('X', Blocks.OAK_LOG)
.build()
);
}
public static class Recipes extends RecipeProvider implements IConditionBuilder {
public Recipes(PackOutput gen) {
super(gen);
}
@Override
protected void buildRecipes(RecipeOutput out) {
// TODO: Move this to a Condition test instead of using recipe encoders.
ShapedRecipeBuilder.shaped(RecipeCategory.MISC, Blocks.DIAMOND_BLOCK, 64)
.pattern("XXX")
.pattern("XXX")
.pattern("XXX")
.define('X', Blocks.DIRT)
.unlockedBy(getHasName(Blocks.DIRT), has(Blocks.DIRT))
.condition(
and (
or (
not(modLoaded("minecraft")),
itemExists("minecraft", "dirt"),
TRUE(),
tagEmpty(ItemTags.DIRT)
),
FALSE()
)
)
.save(out, rl("test_encode_all_conditions"));
ShapedRecipeBuilder.shaped(RecipeCategory.MISC, Blocks.DIAMOND_BLOCK, 64)
.pattern("XXX")
.pattern("XXX")
.pattern("XXX")
.define('X', Blocks.DIRT)
.unlockedBy(getHasName(Blocks.DIRT), has(Blocks.DIRT))
.condition(FALSE())
.save(out, rl("shaped_false_conditions"));
ShapedRecipeBuilder.shaped(RecipeCategory.MISC, Blocks.DIAMOND_BLOCK, 64)
.pattern("XXX")
.pattern("XXX")
.pattern("XXX")
.define('X', Blocks.OAK_LOG)
.unlockedBy(getHasName(Blocks.OAK_LOG), has(Blocks.OAK_LOG))
.condition(TRUE())
.save(out, rl("shaped_true_conditions"));
ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, Blocks.DIAMOND_BLOCK)
.requires(Blocks.REDSTONE_ORE)
.requires(Blocks.REDSTONE_ORE)
.requires(Blocks.REDSTONE_ORE)
.unlockedBy(getHasName(Blocks.REDSTONE_ORE), has(Blocks.REDSTONE_ORE))
.condition(FALSE())
.save(out, rl("shapeless_false_conditions"));
ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, Blocks.DIAMOND_BLOCK)
.requires(Blocks.REDSTONE_ORE)
.requires(Blocks.REDSTONE_ORE)
.requires(Blocks.REDSTONE_ORE)
.requires(Blocks.REDSTONE_ORE)
.unlockedBy(getHasName(Blocks.REDSTONE_ORE), has(Blocks.REDSTONE_ORE))
.condition(TRUE())
.save(out, rl("shapeless_true_conditions"));
SimpleCookingRecipeBuilder.smelting(Ingredient.of(Blocks.DIRT), RecipeCategory.MISC, Blocks.DIAMOND_BLOCK, 0.1F, 200)
.unlockedBy(getHasName(Blocks.DIRT), has(Blocks.DIRT))
.condition(FALSE())
.save(out, rl("cooking_false_conditions"));
SimpleCookingRecipeBuilder.smelting(Ingredient.of(Blocks.BEE_NEST), RecipeCategory.MISC, Blocks.DIAMOND_BLOCK, 0.1F, 200)
.unlockedBy(getHasName(Blocks.BEE_NEST), has(Blocks.BEE_NEST))
.condition(TRUE())
.save(out, rl("cooking_true_conditions"));
SingleItemRecipeBuilder.stonecutting(Ingredient.of(Blocks.DIRT), RecipeCategory.MISC, Blocks.DIAMOND_BLOCK)
.unlockedBy(getHasName(Blocks.DIRT), has(Blocks.DIRT))
.condition(FALSE())
.save(out, rl("single_item_false_conditions"));
SingleItemRecipeBuilder.stonecutting(Ingredient.of(Blocks.REDSTONE_ORE), RecipeCategory.MISC, Blocks.DIAMOND_BLOCK)
.unlockedBy(getHasName(Blocks.REDSTONE_ORE), has(Blocks.REDSTONE_ORE))
.condition(TRUE())
.save(out, rl("singe_item_true_conditions"));
// TODO SmithingTransformRecipeBuilder
// TODO SmithingTrimRecipeBuilder
ConditionalRecipe.builder()
.condition(FALSE())
.recipe(
ShapedRecipeBuilder.shaped(RecipeCategory.MISC, Blocks.DIAMOND_BLOCK, 64)
.pattern("XXX")
.pattern("XX ")
.define('X', Blocks.DIRT)
.unlockedBy(getHasName(Blocks.DIRT), has(Blocks.DIRT))
::save
)
.condition(TRUE())
.recipe(
ShapedRecipeBuilder.shaped(RecipeCategory.MISC, Blocks.DIAMOND_BLOCK, 64)
.pattern("XXX")
.pattern("XX ")
.define('X', Blocks.OAK_LOG)
.unlockedBy(getHasName(Blocks.OAK_LOG), has(Blocks.OAK_LOG))
::save
)
.save(out, rl("conditional_recipe_choice"));
ConditionalRecipe.builder()
.condition(FALSE())
.recipe(
ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, Blocks.DIAMOND_BLOCK, 64)
.requires(Blocks.DIRT)
.unlockedBy(getHasName(Blocks.DIRT), has(Blocks.DIRT))
::save
)
.save(out, rl("conditional_doesnt_load_empty"));
/*
// ingredient tests
// strict NBT match - should match an unnamed iron pickaxe that lost 3 durability
Ingredient nbtIngredient = StrictNBTIngredient.of(Util.make(() ->
{
ItemStack stack = new ItemStack(Items.IRON_PICKAXE);
stack.setDamageValue(3);
return stack;
}));
ShapelessRecipeBuilder.shapeless(RecipeCategory.TOOLS, Items.GOLDEN_PICKAXE)
.requires(nbtIngredient)
.unlockedBy("has_pickaxe", has(Items.IRON_PICKAXE))
.save(consumer, new ResourceLocation("data_gen_test", "exact_nbt_ingredient"));
// ingredient tests
// contains NBT match - should match a stone pickaxe that lost 3 durability, regardless of setting its name
ShapelessRecipeBuilder.shapeless(RecipeCategory.TOOLS, Items.IRON_PICKAXE)
.requires(PartialNBTIngredient.of(Items.STONE_PICKAXE, Util.make(() ->
{
CompoundTag nbt = new CompoundTag();
nbt.putInt(ItemStack.TAG_DAMAGE, 3);
return nbt;
})))
.unlockedBy("has_pickaxe", has(Items.STONE_PICKAXE))
.save(consumer, new ResourceLocation("data_gen_test", "contains_nbt_ingredient_single_item"));
// contains NBT match - should match a wood, stone, or iron pickaxe that was named "Diamond Pickaxe", regardless of damage
ShapelessRecipeBuilder.shapeless(RecipeCategory.TOOLS, Items.DIAMOND_PICKAXE)
.requires(PartialNBTIngredient.of(Util.make(() ->
{
CompoundTag nbt = new CompoundTag();
CompoundTag display = new CompoundTag();
display.putString(ItemStack.TAG_DISPLAY_NAME, "{\"text\":\"Diamond Pickaxe\"}");
nbt.put(ItemStack.TAG_DISPLAY, display);
return nbt;
}), Items.WOODEN_PICKAXE, Items.STONE_PICKAXE, Items.IRON_PICKAXE))
.unlockedBy("has_pickaxe", has(Items.WOODEN_PICKAXE))
.save(consumer, new ResourceLocation("data_gen_test", "contains_nbt_ingredient_item_set"));
// intersection - should match all non-flammable planks
ShapedRecipeBuilder.shaped(RecipeCategory.BUILDING_BLOCKS, Blocks.NETHERRACK)
.pattern("###")
.pattern("###")
.pattern(" # ")
.define('#', IntersectionIngredient.of(Ingredient.of(ItemTags.PLANKS), Ingredient.of(ItemTags.NON_FLAMMABLE_WOOD)))
.unlockedBy("has_planks", has(Items.CRIMSON_PLANKS))
.save(consumer, new ResourceLocation("data_gen_test", "intersection_ingredient"));
// difference - should match all flammable fences
ShapedRecipeBuilder.shaped(RecipeCategory.TOOLS, Items.FLINT_AND_STEEL)
.pattern(" # ")
.pattern("###")
.pattern(" # ")
.define('#', DifferenceIngredient.of(Ingredient.of(ItemTags.FENCES), Ingredient.of(ItemTags.NON_FLAMMABLE_WOOD)))
.unlockedBy("has_fence", has(Items.CRIMSON_FENCE))
.save(consumer, new ResourceLocation("data_gen_test", "difference_ingredient"));
// compound - should match planks, logs, or bedrock
ShapedRecipeBuilder.shaped(RecipeCategory.BUILDING_BLOCKS, Blocks.DIRT)
.pattern("###")
.pattern(" # ")
.define('#', CompoundIngredient.of(Ingredient.of(ItemTags.PLANKS), Ingredient.of(ItemTags.LOGS), Ingredient.of(Blocks.BEDROCK)))
.unlockedBy("has_planks", has(Items.CRIMSON_PLANKS))
.save(consumer, new ResourceLocation("data_gen_test", "compound_ingredient_only_vanilla"));
// compound - should match planks, logs, or a stone pickaxe with 3 damage
ShapedRecipeBuilder.shaped(RecipeCategory.BUILDING_BLOCKS, Blocks.GOLD_BLOCK)
.pattern("#")
.pattern("#")
.define('#', CompoundIngredient.of(Ingredient.of(ItemTags.PLANKS), Ingredient.of(ItemTags.LOGS), nbtIngredient))
.unlockedBy("has_planks", has(Items.CRIMSON_PLANKS))
.save(consumer, new ResourceLocation("data_gen_test", "compound_ingredient_custom_types"));
*/
}
}
}
@@ -28,7 +28,6 @@ import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.CreativeModeTab.TabVisibility;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.LootParams;
@@ -44,11 +43,9 @@ import net.minecraftforge.common.loot.LootModifier;
import net.minecraftforge.common.loot.LootTableIdCondition;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.registries.RegistryObject;
import net.minecraftforge.test.Empty;
import net.minecraftforge.test.BaseTestMod;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.data.event.GatherDataEvent;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.gametest.GameTestHolder;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.registries.DeferredRegister;
@@ -61,45 +58,33 @@ import java.util.stream.Collectors;
@GameTestHolder("forge.global_loot_modifiers")
@Mod(GlobalLootModifiersTest.MODID)
public class GlobalLootModifiersTest {
public class GlobalLootModifiersTest extends BaseTestMod {
public static final String MODID = "global_loot_test";
public static final boolean ENABLE = true;
public GlobalLootModifiersTest() {
if (!ENABLE)
return;
var modBus = FMLJavaModLoadingContext.get().getModEventBus();
GLM.register(modBus);
ENCHANTS.register(modBus);
modBus.register(this);
}
private static final DeferredRegister<Codec<? extends IGlobalLootModifier>> GLM = DeferredRegister.create(ForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, MODID);
private static final DeferredRegister<Enchantment> ENCHANTS = DeferredRegister.create(ForgeRegistries.ENCHANTMENTS, MODID);
static {
GLM.register("dungeon_loot", DungeonLootEnhancerModifier.CODEC);
GLM.register("smelting", SmeltingEnchantmentModifier.CODEC);
GLM.register("wheat_harvest", WheatSeedsConverterModifier.CODEC);
GLM.register("silk_touch_bamboo", SilkTouchTestModifier.CODEC);
}
public static final RegistryObject<Codec<DungeonLootEnhancerModifier>> DUNGEON_LOOT = GLM.register("dungeon_loot", DungeonLootEnhancerModifier.CODEC);
public static final RegistryObject<Codec<SmeltingEnchantmentModifier>> SMELTING = GLM.register("smelting", SmeltingEnchantmentModifier.CODEC);
public static final RegistryObject<Codec<WheatSeedsConverterModifier>> WHEATSEEDS = GLM.register("wheat_harvest", WheatSeedsConverterModifier.CODEC);
public static final RegistryObject<Codec<SilkTouchTestModifier>> SILKTOUCH = GLM.register("silk_touch_bamboo", SilkTouchTestModifier.CODEC);
public static final RegistryObject<Enchantment> SMELT = ENCHANTS.register("smelt", () -> new SmelterEnchantment(Rarity.UNCOMMON, EnchantmentCategory.DIGGER, EquipmentSlot.MAINHAND));
private static final DeferredRegister<Enchantment> ENCHANTS = DeferredRegister.create(ForgeRegistries.ENCHANTMENTS, MODID);
private static final RegistryObject<Enchantment> SMELT = ENCHANTS.register("smelt", () -> new SmelterEnchantment(Rarity.UNCOMMON, EnchantmentCategory.DIGGER, EquipmentSlot.MAINHAND));
public GlobalLootModifiersTest() {
testItem(() -> {
var smelt = new ItemStack(Items.IRON_AXE);
EnchantmentHelper.setEnchantments(Map.of(SMELT.get(), 1), smelt);
return smelt;
});
}
@SubscribeEvent
public void runData(GatherDataEvent event) {
event.getGenerator().addProvider(event.includeServer(), new DataProvider(event.getGenerator().getPackOutput(), MODID));
}
@SubscribeEvent
public void onCreativeModeTabBuildContents(BuildCreativeModeTabContentsEvent event) {
var entries = event.getEntries();
var vis = TabVisibility.PARENT_AND_SEARCH_TABS;
if (event.getTabKey() == Empty.TAB) {
var smelt = new ItemStack(Items.IRON_AXE);
EnchantmentHelper.setEnchantments(Map.of(SMELT.get(), 1), smelt);
entries.put(smelt, vis);
}
}
@GameTest(template = "forge:empty3x3x3")
public static void test_smelting_modifier(GameTestHelper helper) {
var center = new BlockPos(1, 1, 1);
@@ -0,0 +1,72 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.test;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.CreativeModeTab.TabVisibility;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
public abstract class BaseTestMod {
private List<Supplier<ItemStack>> testItems = new ArrayList<>();
protected BaseTestMod() {
// TODO: Some form of enable flag?
var modBus = FMLJavaModLoadingContext.get().getModEventBus();
modBus.register(this);
Class<?> cls = getClass();
while (cls != BaseTestMod.class) {
for (var field : cls.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) && field.getType() == DeferredRegister.class) {
field.setAccessible(true);
DeferredRegister<?> dr = getField(field, null);
dr.register(modBus);
}
}
cls = cls.getSuperclass();
}
}
protected Supplier<ItemStack> testItem(Supplier<ItemStack> supplier) {
this.testItems.add(supplier);
return supplier;
}
@SubscribeEvent
protected void onCreativeModeTabBuildContents(BuildCreativeModeTabContentsEvent event) {
var entries = event.getEntries();
if (event.getTabKey() == TestHelperMod.TAB) {
for (var s : testItems)
entries.put(s.get(), TabVisibility.PARENT_AND_SEARCH_TABS);
}
}
@SuppressWarnings("unchecked")
protected static <R> R getField(Field field, Object instance) {
try {
return (R)field.get(instance);
} catch (IllegalArgumentException | IllegalAccessException e) {
return sneak(e);
}
}
@SuppressWarnings("unchecked")
private static <E extends Throwable, R> R sneak(Throwable e) throws E {
throw (E)e;
}
}
@@ -21,20 +21,14 @@ import net.minecraft.world.item.Items;
import net.minecraftforge.data.event.GatherDataEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.RegisterEvent;
/* A place where I can put common utility stuff for now. Until I re-write the test codebase. */
@Mod(Empty.MOD_ID)
public class Empty {
public static final String MOD_ID = "empty";
@Mod(TestHelperMod.MOD_ID)
public class TestHelperMod extends BaseTestMod {
public static final String MOD_ID = "test_helper_mod";
public static final ResourceKey<CreativeModeTab> TAB = ResourceKey.create(Registries.CREATIVE_MODE_TAB, new ResourceLocation(MOD_ID, "test_items"));
public Empty() {
var modBus = FMLJavaModLoadingContext.get().getModEventBus();
modBus.register(this);
}
@SubscribeEvent
public void onRegister(RegisterEvent event) {
event.register(Registries.CREATIVE_MODE_TAB, h -> {
@@ -0,0 +1,22 @@
{
"type": "minecraft:crafting_shapeless",
"category": "misc",
"forge:condition": {
"type": "forge:this_is_unknown_it_should_not_error_in_log_but_just_fail",
"value": {}
},
"ingredients": [
{
"item": "minecraft:redstone_ore"
},
{
"item": "minecraft:redstone_ore"
},
{
"item": "minecraft:redstone_ore"
}
],
"result": {
"item": "minecraft:diamond_block"
}
}