From 1ebd820dc91d54f89f48f095ce0a7d723c0f13e5 Mon Sep 17 00:00:00 2001 From: apple502j <33279053+apple502j@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:06:49 +0900 Subject: [PATCH] Add support for Ingredient in FabricBrewingRecipeRegistry --- .../BrewingRecipeRegistryBuilderCallback.java | 43 ------------ .../FabricBrewingRecipeRegistryBuilder.java | 66 +++++++++++++++++++ .../BrewingRecipeRegistryBuilderMixin.java | 62 +++++++++++++++-- .../src/main/resources/fabric.mod.json | 5 +- .../content/registry/ContentRegistryTest.java | 16 +++-- .../fabric/test/item/CustomDamageTest.java | 5 +- 6 files changed, 140 insertions(+), 57 deletions(-) delete mode 100644 fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/api/registry/BrewingRecipeRegistryBuilderCallback.java create mode 100644 fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/api/registry/FabricBrewingRecipeRegistryBuilder.java diff --git a/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/api/registry/BrewingRecipeRegistryBuilderCallback.java b/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/api/registry/BrewingRecipeRegistryBuilderCallback.java deleted file mode 100644 index f167a77006..0000000000 --- a/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/api/registry/BrewingRecipeRegistryBuilderCallback.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.api.registry; - -import net.minecraft.recipe.BrewingRecipeRegistry; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; - -/** - * Use this event to register custom brewing recipes. - */ -public interface BrewingRecipeRegistryBuilderCallback { - /** - * An event that is called when the brewing recipe registry is being built. - */ - Event BUILD = EventFactory.createArrayBacked(BrewingRecipeRegistryBuilderCallback.class, listeners -> builder -> { - for (BrewingRecipeRegistryBuilderCallback listener : listeners) { - listener.build(builder); - } - }); - - /** - * Called when the brewing recipe registry is being built. - * - * @param builder the {@link BrewingRecipeRegistry} instance - */ - void build(BrewingRecipeRegistry.class_9665 builder); -} diff --git a/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/api/registry/FabricBrewingRecipeRegistryBuilder.java b/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/api/registry/FabricBrewingRecipeRegistryBuilder.java new file mode 100644 index 0000000000..61859013c1 --- /dev/null +++ b/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/api/registry/FabricBrewingRecipeRegistryBuilder.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.api.registry; + +import net.minecraft.item.Item; +import net.minecraft.potion.Potion; +import net.minecraft.recipe.BrewingRecipeRegistry; +import net.minecraft.recipe.Ingredient; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.resource.featuretoggle.FeatureFlag; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +/** + * An extension of {@link BrewingRecipeRegistry.Builder} to support ingredients. + */ +public interface FabricBrewingRecipeRegistryBuilder { + /** + * An event that is called when the brewing recipe registry is being built. + */ + Event BUILD = EventFactory.createArrayBacked(FabricBrewingRecipeRegistryBuilder.BuildCallback.class, listeners -> builder -> { + for (FabricBrewingRecipeRegistryBuilder.BuildCallback listener : listeners) { + listener.build(builder); + } + }); + + default void registerItemRecipe(Item input, Ingredient ingredient, Item output, FeatureFlag... requiredFeatures) { + throw new AssertionError("Must be implemented via interface injection"); + } + + default void registerPotionRecipe(RegistryEntry input, Ingredient ingredient, RegistryEntry output, FeatureFlag... requiredFeatures) { + throw new AssertionError("Must be implemented via interface injection"); + } + + default void registerRecipes(Ingredient ingredient, RegistryEntry potion, FeatureFlag... requiredFeatures) { + throw new AssertionError("Must be implemented via interface injection"); + } + + /** + * Use this event to register custom brewing recipes. + */ + @FunctionalInterface + interface BuildCallback { + /** + * Called when the brewing recipe registry is being built. + * + * @param builder the {@link BrewingRecipeRegistry} instance + */ + void build(BrewingRecipeRegistry.Builder builder); + } +} diff --git a/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/mixin/content/registry/BrewingRecipeRegistryBuilderMixin.java b/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/mixin/content/registry/BrewingRecipeRegistryBuilderMixin.java index cf5c0f0c46..64cd595e94 100644 --- a/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/mixin/content/registry/BrewingRecipeRegistryBuilderMixin.java +++ b/fabric-content-registries-v0/src/main/java/net/fabricmc/fabric/mixin/content/registry/BrewingRecipeRegistryBuilderMixin.java @@ -16,19 +16,71 @@ package net.fabricmc.fabric.mixin.content.registry; +import java.util.List; + +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import net.minecraft.item.Item; +import net.minecraft.potion.Potion; +import net.minecraft.potion.Potions; import net.minecraft.recipe.BrewingRecipeRegistry; +import net.minecraft.recipe.Ingredient; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.resource.featuretoggle.FeatureFlag; +import net.minecraft.resource.featuretoggle.FeatureFlags; +import net.minecraft.resource.featuretoggle.FeatureSet; + +import net.fabricmc.fabric.api.registry.FabricBrewingRecipeRegistryBuilder; + +@Mixin(BrewingRecipeRegistry.Builder.class) +public abstract class BrewingRecipeRegistryBuilderMixin implements FabricBrewingRecipeRegistryBuilder { + @Shadow + @Final + private FeatureSet enabledFeatures; + + @Shadow + private static void assertPotion(Item potionType) { + } + + @Shadow + @Final + private List> itemRecipes; -import net.fabricmc.fabric.api.registry.BrewingRecipeRegistryBuilderCallback; + @Shadow + @Final + private List> potionRecipes; -@Mixin(BrewingRecipeRegistry.class_9665.class) -public class BrewingRecipeRegistryBuilderMixin { - @Inject(method = "method_59701", at = @At("HEAD")) + @Inject(method = "build", at = @At("HEAD")) private void build(CallbackInfoReturnable cir) { - BrewingRecipeRegistryBuilderCallback.BUILD.invoker().build((BrewingRecipeRegistry.class_9665) (Object) this); + FabricBrewingRecipeRegistryBuilder.BUILD.invoker().build((BrewingRecipeRegistry.Builder) (Object) this); + } + + @Override + public void registerItemRecipe(Item input, Ingredient ingredient, Item output, FeatureFlag... requiredFeatures) { + if (input.isEnabled(this.enabledFeatures) && FeatureFlags.FEATURE_MANAGER.featureSetOf(requiredFeatures).isSubsetOf(this.enabledFeatures) && output.isEnabled(this.enabledFeatures)) { + assertPotion(input); + assertPotion(output); + this.itemRecipes.add(new BrewingRecipeRegistry.Recipe<>(input.getRegistryEntry(), ingredient, output.getRegistryEntry())); + } + } + + @Override + public void registerPotionRecipe(RegistryEntry input, Ingredient ingredient, RegistryEntry output, FeatureFlag... requiredFeatures) { + if (input.value().isEnabled(this.enabledFeatures) && FeatureFlags.FEATURE_MANAGER.featureSetOf(requiredFeatures).isSubsetOf(this.enabledFeatures) && output.value().isEnabled(this.enabledFeatures)) { + this.potionRecipes.add(new BrewingRecipeRegistry.Recipe<>(input, ingredient, output)); + } + } + + @Override + public void registerRecipes(Ingredient ingredient, RegistryEntry potion, FeatureFlag... requiredFeatures) { + if (potion.value().isEnabled(this.enabledFeatures)) { + this.registerPotionRecipe(Potions.WATER, ingredient, Potions.MUNDANE, requiredFeatures); + this.registerPotionRecipe(Potions.AWKWARD, ingredient, potion, requiredFeatures); + } } } diff --git a/fabric-content-registries-v0/src/main/resources/fabric.mod.json b/fabric-content-registries-v0/src/main/resources/fabric.mod.json index 3b6958e485..8820f2a842 100644 --- a/fabric-content-registries-v0/src/main/resources/fabric.mod.json +++ b/fabric-content-registries-v0/src/main/resources/fabric.mod.json @@ -27,6 +27,9 @@ ], "accessWidener" : "fabric-content-registries-v0.accesswidener", "custom": { - "fabric-api:module-lifecycle": "stable" + "fabric-api:module-lifecycle": "stable", + "loom:injected_interfaces": { + "net/minecraft/class_1845\u0024class_9665": ["net/fabricmc/fabric/api/registry/FabricBrewingRecipeRegistryBuilder"] + } } } diff --git a/fabric-content-registries-v0/src/testmod/java/net/fabricmc/fabric/test/content/registry/ContentRegistryTest.java b/fabric-content-registries-v0/src/testmod/java/net/fabricmc/fabric/test/content/registry/ContentRegistryTest.java index b42c7772e1..8e2d8f7a6b 100644 --- a/fabric-content-registries-v0/src/testmod/java/net/fabricmc/fabric/test/content/registry/ContentRegistryTest.java +++ b/fabric-content-registries-v0/src/testmod/java/net/fabricmc/fabric/test/content/registry/ContentRegistryTest.java @@ -30,6 +30,8 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.item.PotionItem; +import net.minecraft.potion.Potions; +import net.minecraft.recipe.Ingredient; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; @@ -37,6 +39,7 @@ import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.registry.tag.BlockTags; import net.minecraft.registry.tag.ItemTags; +import net.minecraft.resource.featuretoggle.FeatureFlags; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Identifier; @@ -47,8 +50,8 @@ import net.minecraft.world.event.GameEvent; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.registry.BrewingRecipeRegistryBuilderCallback; import net.fabricmc.fabric.api.registry.CompostingChanceRegistry; +import net.fabricmc.fabric.api.registry.FabricBrewingRecipeRegistryBuilder; import net.fabricmc.fabric.api.registry.FlammableBlockRegistry; import net.fabricmc.fabric.api.registry.FlattenableBlockRegistry; import net.fabricmc.fabric.api.registry.FuelRegistry; @@ -84,6 +87,7 @@ public void onInitialize() { // - assign a loot table to the nitwit villager type // - right-clicking a 'test_event' block will emit a 'test_event' game event, which will have a sculk sensor frequency of 2 // - instant health potions can be brewed from awkward potions with any item in the 'minecraft:small_flowers' tag + // - if Bundle experiment is enabled, luck potions can be brewed from awkward potions with a bundle // - dirty potions can be brewed by adding any item in the 'minecraft:dirt' tag to any standard potion CompostingChanceRegistry.INSTANCE.add(Items.OBSIDIAN, 0.5F); @@ -156,11 +160,11 @@ public void onInitialize() { dirtyPotion); /* Mods should use BrewingRecipeRegistry.registerPotionType(Item), which is access widened by fabric-transitive-access-wideners-v1 * This testmod uses an accessor due to Loom limitations that prevent TAWs from applying across Gradle subproject boundaries */ - BrewingRecipeRegistryBuilderCallback.BUILD.register(builder -> { - builder.method_59702(dirtyPotion); - // TODO 1.20.5 Ingredient.fromTag(ItemTags.DIRT) - builder.method_59703(Items.POTION, Items.DIRT, dirtyPotion); - // registerPotionRecipe(Potions.AWKWARD, Ingredient.fromTag(ItemTags.SMALL_FLOWERS), Potions.HEALING); + FabricBrewingRecipeRegistryBuilder.BUILD.register(builder -> { + builder.registerPotionType(dirtyPotion); + builder.registerItemRecipe(Items.POTION, Ingredient.fromTag(ItemTags.DIRT), dirtyPotion); + builder.registerPotionRecipe(Potions.AWKWARD, Ingredient.fromTag(ItemTags.SMALL_FLOWERS), Potions.HEALING); + builder.registerPotionRecipe(Potions.AWKWARD, Ingredient.ofItems(Items.BUNDLE), Potions.LUCK, FeatureFlags.BUNDLE); }); } diff --git a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/CustomDamageTest.java b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/CustomDamageTest.java index 1b39db7f48..80389272f7 100644 --- a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/CustomDamageTest.java +++ b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/CustomDamageTest.java @@ -26,6 +26,7 @@ import net.minecraft.item.PickaxeItem; import net.minecraft.item.ToolMaterials; import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.potion.Potions; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.text.Text; @@ -36,6 +37,7 @@ import net.fabricmc.fabric.api.item.v1.CustomDamageHandler; import net.fabricmc.fabric.api.item.v1.EnchantingContext; import net.fabricmc.fabric.api.item.v1.EnchantmentEvents; +import net.fabricmc.fabric.api.registry.FabricBrewingRecipeRegistryBuilder; import net.fabricmc.fabric.api.registry.FuelRegistry; import net.fabricmc.fabric.api.util.TriState; @@ -58,8 +60,7 @@ public class CustomDamageTest implements ModInitializer { public void onInitialize() { Registry.register(Registries.ITEM, new Identifier("fabric-item-api-v1-testmod", "weird_pickaxe"), WEIRD_PICK); FuelRegistry.INSTANCE.add(WEIRD_PICK, 200); - // TODO 1.20.5 - // FabricBrewingRecipeRegistry.registerPotionRecipe(Potions.WATER, Ingredient.ofItems(WEIRD_PICK), Potions.AWKWARD); + FabricBrewingRecipeRegistryBuilder.BUILD.register(builder -> builder.registerPotionRecipe(Potions.WATER, WEIRD_PICK, Potions.AWKWARD)); EnchantmentEvents.ALLOW_ENCHANTING.register(((enchantment, target, enchantingContext) -> { if (target.isOf(Items.DIAMOND_PICKAXE) && enchantment == Enchantments.SHARPNESS