====== Adding Potions ======
==== Introduction ====
Adding a custom potions to your mod follows a similar process to [[tutorial:items|adding an item]]. However, in the case of creating new potions, it is easiest to create a custom class rather than creating an instance of the ''Potion'' class under ''ModInitializer''. It is also necessary to create a mixin for the custom potion recipes. See [[tutorial:mixin_introduction|Mixin Introduction]] for more information on mixins.
Minecraft automatically creates splash and lingering potions of any potion you register, so you only need to make the base potion. Minecraft also automatically creates textures for your potion, so you don't need to worry about that either.
=== The BrewingRecipeRegistryMixin ===
As of 1.20.1, Fabric API makes the methods for registering custom brewing recipes public. Therefore you can call `BrewingRecipeRegistry.registerPotionRecipe` directly, and do not need this mixin.
For older versions, this is a mixin you will need to add to your mod in order for your custom potion recipes to register correctly.
@Mixin(BrewingRecipeRegistry.class)
public interface BrewingRecipeRegistryMixin {
@Invoker("registerPotionRecipe")
static void invokeRegisterPotionRecipe(Potion input, Item item, Potion output){
}
}
==== Creating and Registering a Potion ====
Here is an example of a custom potion class, called ''ModPotions'', in which we create a basic potion which gives the Wither effect.
public class ModPotions {
public static final Potion EXAMPLE_POTION =
Registry.register(Registry.POTION, new Identifier("tutorial", "example_potion"),
new Potion(new StatusEffectInstance(StatusEffects.WITHER, 3600, 0)));
public static void registerPotions(){
}
public static void registerPotionsRecipes(){
BrewingRecipeRegistryMixin.invokeRegisterPotionRecipe(Potions.AWKWARD, Items.WITHER_ROSE, ModPotions.EXAMPLE_POTION);
}
}
"**public static final Potion**" is where we identify, declare, and register our potion. The most important information is contained within the "new StatusEffectInstance()". Firstly, we declare our status effect, ''WITHER''. Next, we declare the potions duration in ticks. 3600 ticks is 3 minutes. Finally, we declare the amplifier of the potion. This number only really matters when there are enhanced versions of the effect. For example, if there were such a thing as a Wither II effect, this number would be 1. If there were a Wither III effect, this number would be 2.
"**public static void registerPotions()**" will allow us to call our custom potions in the ''ModInitializer''.
"**public static void registerPotionsRecipes()**" allows us to create brewing recipes for our custom potions. The recipe requires three parts; the input potion, the input item, and the output potion. In this example, we use a standard Awkward Potion as our input potion, and a Wither Rose as our input item. The result is our Example Potion, which causes the Wither effect.
Once you have created your custom potion instance, you can call the potion and its recipe is ''ModInitializer'', like so:
public class ExampleMod implements ModInitializer {
@Override
public void onInitialize() {
ModPotions.registerPotions();
ModPotions.registerPotionRecipes();
}
==== Creating a Custom Extended Potion ====
Let's say you want to create an extended version of your custom potion. The process is very similar, with a few important changes.
public class ModPotions {
public static final Potion LONG_EXAMPLE_POTION =
Registry.register(Registry.POTION, new Identifier("tutorial", "example_potion"),
new Potion(new StatusEffectInstance(StatusEffects.WITHER, 9600, 0)));
public static void registerPotions(){
}
public static void registerPotionsRecipes(){
BrewingRecipeRegistryMixin.invokeRegisterPotionRecipe(ModPotions.EXAMPLE_POTION, Items.REDSTONE, ModPotions.LONG_EXAMPLE_POTION);
}
}
In this example, I changed the duration of the potion to 9600 ticks, which is 8 minutes. This is the standard length of extended potions. Our recipe has also changed. The input potion is now the first custom potion we created. The input item of redstone is the typical item used to brew extended potions. The output potion is the extended custom potion.
==== Creating a Potion with Custom Effects ====
Creating a custom potion out of a custom effect is much the same. For creating custom effects, see [[tutorial:status_effects|Adding Status Effects]].
public class ModPotions {
public static final Potion CUSTOM_POTION =
Registry.register(Registry.POTION, new Identifier("tutorial", "custom_potion"),
new Potion(new StatusEffectInstance(ModEffects."CUSTOM_EFFECT", 3600, 0)));
public static void registerPotions(){
}
public static void registerPotionsRecipes(){
BrewingRecipeRegistryMixin.invokeRegisterPotionRecipe(Potions.AWKWARD, Items.FEATHER, ModPotions.CUSTOM_POTION);
}
}