Skip to content

Commit

Permalink
Add support for Ingredient in FabricBrewingRecipeRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Apr 12, 2024
1 parent b87e67c commit 1ebd820
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 57 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<FabricBrewingRecipeRegistryBuilder.BuildCallback> 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<Potion> input, Ingredient ingredient, RegistryEntry<Potion> output, FeatureFlag... requiredFeatures) {
throw new AssertionError("Must be implemented via interface injection");
}

default void registerRecipes(Ingredient ingredient, RegistryEntry<Potion> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BrewingRecipeRegistry.Recipe<Item>> itemRecipes;

import net.fabricmc.fabric.api.registry.BrewingRecipeRegistryBuilderCallback;
@Shadow
@Final
private List<BrewingRecipeRegistry.Recipe<Potion>> 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<BrewingRecipeRegistry> 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<Potion> input, Ingredient ingredient, RegistryEntry<Potion> 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> potion, FeatureFlag... requiredFeatures) {
if (potion.value().isEnabled(this.enabledFeatures)) {
this.registerPotionRecipe(Potions.WATER, ingredient, Potions.MUNDANE, requiredFeatures);
this.registerPotionRecipe(Potions.AWKWARD, ingredient, potion, requiredFeatures);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
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;
import net.minecraft.registry.RegistryKeys;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit 1ebd820

Please sign in to comment.