User Tools

Site Tools


tutorial:potions

Adding Potions

Introduction

Adding a custom potions to your mod follows a similar process to 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 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.

  1. @Mixin(BrewingRecipeRegistry.class)
  2. public interface BrewingRecipeRegistryMixin {
  3.  
  4. @Invoker("registerPotionRecipe")
  5. static void invokeRegisterPotionRecipe(Potion input, Item item, Potion output){
  6. }
  7.  
  8. }

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.

  1. public class ModPotions {
  2.  
  3. public static final Potion EXAMPLE_POTION =
  4. Registry.register(Registry.POTION, new Identifier("tutorial", "example_potion"),
  5. new Potion(new StatusEffectInstance(StatusEffects.WITHER, 3600, 0)));
  6.  
  7. public static void registerPotions(){
  8.  
  9. }
  10.  
  11. public static void registerPotionsRecipes(){
  12. BrewingRecipeRegistryMixin.invokeRegisterPotionRecipe(Potions.AWKWARD, Items.WITHER_ROSE, ModPotions.EXAMPLE_POTION);
  13. }
  14. }

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:

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. @Override
  4. public void onInitialize() {
  5. ModPotions.registerPotions();
  6. ModPotions.registerPotionRecipes();
  7.  
  8. }

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.

  1. public class ModPotions {
  2.  
  3. public static final Potion LONG_EXAMPLE_POTION =
  4. Registry.register(Registry.POTION, new Identifier("tutorial", "example_potion"),
  5. new Potion(new StatusEffectInstance(StatusEffects.WITHER, 9600, 0)));
  6.  
  7. public static void registerPotions(){
  8.  
  9. }
  10.  
  11. public static void registerPotionsRecipes(){
  12. BrewingRecipeRegistryMixin.invokeRegisterPotionRecipe(ModPotions.EXAMPLE_POTION, Items.REDSTONE, ModPotions.LONG_EXAMPLE_POTION);
  13. }
  14. }

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 Adding Status Effects.

  1. public class ModPotions {
  2.  
  3. public static final Potion CUSTOM_POTION =
  4. Registry.register(Registry.POTION, new Identifier("tutorial", "custom_potion"),
  5. new Potion(new StatusEffectInstance(ModEffects."CUSTOM_EFFECT", 3600, 0)));
  6.  
  7. public static void registerPotions(){
  8.  
  9. }
  10.  
  11. public static void registerPotionsRecipes(){
  12. BrewingRecipeRegistryMixin.invokeRegisterPotionRecipe(Potions.AWKWARD, Items.FEATHER, ModPotions.CUSTOM_POTION);
  13. }
  14. }
tutorial/potions.txt · Last modified: 2023/09/10 07:08 by drakonkinst