mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-08 21:14:41 -04:00
API for brewing recipes which use Ingredients instead of Items (#2670)
* API for brewing recipes which use Ingredients instead of Items * Improve comments and JavaDoc * Update mixin compatibility level * Replace accessor mixin with access widener * Fix double space Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com> Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com>
This commit is contained in:
parent
08b73de4d1
commit
7c6cd14d84
6 changed files with 152 additions and 1 deletions
fabric-content-registries-v0/src
main
java/net/fabricmc/fabric/api/registry
resources
testmod
java/net/fabricmc/fabric/test
resources
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* 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 java.util.Objects;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.PotionItem;
|
||||
import net.minecraft.potion.Potion;
|
||||
import net.minecraft.recipe.BrewingRecipeRegistry;
|
||||
import net.minecraft.recipe.Ingredient;
|
||||
|
||||
/**
|
||||
* Counterpart of {@link BrewingRecipeRegistry} with methods that allow adding recipes which use Ingredients instead of Items.
|
||||
*/
|
||||
public final class FabricBrewingRecipeRegistry {
|
||||
private FabricBrewingRecipeRegistry() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a recipe for brewing one potion type into another (e.g. regular to splash).
|
||||
* Only one recipe is necessary for all potions of the input type to be brewable into the output type using the ingredient.
|
||||
* Use {@link BrewingRecipeRegistry#registerPotionType(Item)} to register new potion types.
|
||||
* @param input the input potion type (e.g. regular potion)
|
||||
* @param ingredient the required ingredient (e.g. gunpowder)
|
||||
* @param output the output type (e.g. splash potion)
|
||||
* @see BrewingRecipeRegistry#registerItemRecipe(Item, Item, Item)
|
||||
*/
|
||||
public static void registerItemRecipe(PotionItem input, Ingredient ingredient, PotionItem output) {
|
||||
Objects.requireNonNull(input, "Input cannot be null!");
|
||||
Objects.requireNonNull(ingredient, "Ingredient cannot be null!");
|
||||
Objects.requireNonNull(output, "Output cannot be null!");
|
||||
|
||||
BrewingRecipeRegistry.ITEM_RECIPES.add(new BrewingRecipeRegistry.Recipe<>(input, ingredient, output));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a recipe for converting from one potion to another (e.g. awkward to instant health).
|
||||
* This does not automatically create long or strong versions of the output potion.
|
||||
* They require separate recipes.
|
||||
* @param input input potion (e.g. awkward)
|
||||
* @param ingredient the required ingredient (e.g. glistering melon)
|
||||
* @param output output potion (e.g. instant health)
|
||||
* @see BrewingRecipeRegistry#registerPotionRecipe(Potion, Item, Potion)
|
||||
*/
|
||||
public static void registerPotionRecipe(Potion input, Ingredient ingredient, Potion output) {
|
||||
Objects.requireNonNull(input, "Input cannot be null!");
|
||||
Objects.requireNonNull(ingredient, "Ingredient cannot be null!");
|
||||
Objects.requireNonNull(output, "Output cannot be null");
|
||||
|
||||
BrewingRecipeRegistry.POTION_RECIPES.add(new BrewingRecipeRegistry.Recipe<>(input, ingredient, output));
|
||||
}
|
||||
}
|
|
@ -2,3 +2,7 @@ accessWidener v1 named
|
|||
|
||||
accessible method net/minecraft/block/entity/AbstractFurnaceBlockEntity addFuel (Ljava/util/Map;Lnet/minecraft/tag/TagKey;I)V
|
||||
accessible method net/minecraft/block/entity/AbstractFurnaceBlockEntity addFuel (Ljava/util/Map;Lnet/minecraft/item/ItemConvertible;I)V
|
||||
|
||||
accessible class net/minecraft/recipe/BrewingRecipeRegistry$Recipe
|
||||
accessible field net/minecraft/recipe/BrewingRecipeRegistry POTION_RECIPES Ljava/util/List;
|
||||
accessible field net/minecraft/recipe/BrewingRecipeRegistry ITEM_RECIPES Ljava/util/List;
|
||||
|
|
|
@ -19,6 +19,13 @@ package net.fabricmc.fabric.test.content.registry;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.PotionItem;
|
||||
import net.minecraft.potion.Potions;
|
||||
import net.minecraft.recipe.Ingredient;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.block.AbstractBlock;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
|
@ -41,6 +48,7 @@ import net.minecraft.world.event.GameEvent;
|
|||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.registry.CompostingChanceRegistry;
|
||||
import net.fabricmc.fabric.api.registry.FabricBrewingRecipeRegistry;
|
||||
import net.fabricmc.fabric.api.registry.FlammableBlockRegistry;
|
||||
import net.fabricmc.fabric.api.registry.FlattenableBlockRegistry;
|
||||
import net.fabricmc.fabric.api.registry.FuelRegistry;
|
||||
|
@ -51,6 +59,7 @@ import net.fabricmc.fabric.api.registry.StrippableBlockRegistry;
|
|||
import net.fabricmc.fabric.api.registry.TillableBlockRegistry;
|
||||
import net.fabricmc.fabric.api.registry.VillagerInteractionRegistries;
|
||||
import net.fabricmc.fabric.api.registry.VillagerPlantableRegistry;
|
||||
import net.fabricmc.fabric.test.mixin.content.registry.BrewingRecipeRegistryAccessor;
|
||||
|
||||
public final class ContentRegistryTest implements ModInitializer {
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(ContentRegistryTest.class);
|
||||
|
@ -76,6 +85,8 @@ public final class ContentRegistryTest implements ModInitializer {
|
|||
// - villagers can now collect and plant oak saplings
|
||||
// - 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
|
||||
// - 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);
|
||||
FlammableBlockRegistry.getDefaultInstance().add(Blocks.DIAMOND_BLOCK, 4, 4);
|
||||
|
@ -153,6 +164,15 @@ public final class ContentRegistryTest implements ModInitializer {
|
|||
// expected behavior
|
||||
LOGGER.info("SculkSensorFrequencyRegistry test passed!");
|
||||
}
|
||||
|
||||
FabricBrewingRecipeRegistry.registerPotionRecipe(Potions.AWKWARD, Ingredient.fromTag(ItemTags.SMALL_FLOWERS), Potions.HEALING);
|
||||
var dirtyPotion = new DirtyPotionItem(new Item.Settings().maxCount(1).group(ItemGroup.BREWING));
|
||||
Registry.register(Registry.ITEM, new Identifier("fabric-content-registries-v0-testmod", "dirty_potion"),
|
||||
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 */
|
||||
BrewingRecipeRegistryAccessor.callRegisterPotionType(dirtyPotion);
|
||||
FabricBrewingRecipeRegistry.registerItemRecipe((PotionItem) Items.POTION, Ingredient.fromTag(ItemTags.DIRT), dirtyPotion);
|
||||
}
|
||||
|
||||
public static class TestEventBlock extends Block {
|
||||
|
@ -167,4 +187,15 @@ public final class ContentRegistryTest implements ModInitializer {
|
|||
return ActionResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
public static class DirtyPotionItem extends PotionItem {
|
||||
public DirtyPotionItem(Settings settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Text getName(ItemStack stack) {
|
||||
return Text.literal("Dirty ").append(Items.POTION.getName(stack));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* 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.test.mixin.content.registry;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Invoker;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.recipe.BrewingRecipeRegistry;
|
||||
|
||||
@Mixin(BrewingRecipeRegistry.class)
|
||||
public interface BrewingRecipeRegistryAccessor {
|
||||
/* Required for the testmod
|
||||
* The TAW cannot be used due to current limitations of Loom
|
||||
* TODO review when upgrading to Loom 1.1
|
||||
*/
|
||||
@Invoker("registerPotionType")
|
||||
static void callRegisterPotionType(Item item) {
|
||||
throw new AssertionError("Untransformed @Invoker");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"required": true,
|
||||
"package": "net.fabricmc.fabric.test.mixin.content.registry",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"mixins": [
|
||||
"BrewingRecipeRegistryAccessor"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
}
|
|
@ -15,5 +15,8 @@
|
|||
"fabric-gametest": [
|
||||
"net.fabricmc.fabric.test.content.registry.FlammableTest"
|
||||
]
|
||||
}
|
||||
},
|
||||
"mixins": [
|
||||
"fabric-content-registries-v0-testmod.mixins.json"
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue