Jump to content

[1.16.3] How to create a custom potion with a custom effect?


Peerius

Recommended Posts

Hello,

 

I tried to create a custom potion with a custom effect and looked at the vanilla code. Someone on YouTube also tried that and used invoke to make a private method accessable. Here is the code:

	private static void addMix(Potion start, Item ingredient, Potion result) {
		if(brewing_mixes == null) {
			brewing_mixes = ObfuscationReflectionHelper.findMethod(PotionBrewing.class, "addMix", Potion.class, Item.class, Potion.class);
			brewing_mixes.setAccessible(true);
		}
		try {
			brewing_mixes.invoke(null, start, ingredient, result);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		} 
	}
	
	public static void addBrewingRecipes() {
		addMix(Potions.AWKWARD, ItemList.nectar, PotionList.love_potion);
	}
	
	public static class ModEffect extends Effect{

		public ModEffect(EffectType typeIn, int liquidColorIn) {
			super(typeIn, liquidColorIn);
		}
		
	}

The addBrewingRecipes() method get's called during the FMLCommonSetupEvent.

But there is something wrong with it, because the invoke method throws an InvocationTargetException. So I guess I need to try it differently. Could someone please give me a hint where I have to start? :) 

 

Thanks in advance and stay healthy.

Link to comment
Share on other sites

On 10/13/2020 at 9:35 AM, diesieben07 said:

PotionUtils.addPotionToItemStack

Okay, that helps - thank you. I only have one problem left: register the potion and the effect the right way. Here is what I've done:

	public static void init (){
		EFFECTS.register(FMLJavaModLoadingContext.get().getModEventBus());
		POTIONS.register(FMLJavaModLoadingContext.get().getModEventBus());
		PotionList.addBrewingRecipes();
	}

	
	public static void addBrewingRecipes() {
		BrewingRecipeRegistry.addRecipe(Ingredient.fromStacks(PotionUtils.addPotionToItemStack(ItemStack.EMPTY, Potions.AWKWARD)), Ingredient.fromStacks(new ItemStack(ItemList.nectar)), PotionUtils.addPotionToItemStack(ItemStack.EMPTY, PotionList.LOVE.get()));
	}
	
	public static final DeferredRegister<Effect> EFFECTS = DeferredRegister.create(ForgeRegistries.POTIONS, DeepAffection.modid);
	public static final DeferredRegister<Potion> POTIONS = DeferredRegister.create(ForgeRegistries.POTION_TYPES, DeepAffection.modid);
	
	//Effects
	public static final RegistryObject<Effect> LOVE_EFFECT = EFFECTS.register("love", () -> new ModEffect(EffectType.BENEFICIAL, 0xc98fff).addAttributesModifier(Attributes.field_233821_d_, "55FCED67-E92A-486E-9800-B47F202C4386", (double)0.34F, AttributeModifier.Operation.MULTIPLY_TOTAL));
	
	//Potions
	public static final RegistryObject<Potion> LOVE = POTIONS.register("love", () -> new Potion(new EffectInstance(PotionList.LOVE_EFFECT.get(), 3600)));

When I start the game, I get a NullPointerException because the registry object deepaffection:love is not present. The init() method get's during the instantiation of DeepAffection.

 

Could you please tell me what I did wrong? :)

 

Link to comment
Share on other sites

On 10/18/2020 at 3:00 PM, diesieben07 said:

Use FMLCommonSetupEvent to register the recipes. Note that BrewingRecipeRegistry is not threadsafe, so you have to use enqueueWork.

Okay, now I did this:

	public static void init (){
		EFFECTS.register(FMLJavaModLoadingContext.get().getModEventBus());
		POTIONS.register(FMLJavaModLoadingContext.get().getModEventBus());
	}

	
	public static void addBrewingRecipes(FMLCommonSetupEvent event) {
		System.out.println("PotionList: Brewing Recipes called.");
		event.enqueueWork( () -> {
		BrewingRecipeRegistry.addRecipe(Ingredient.fromStacks(PotionUtils.addPotionToItemStack(ItemStack.EMPTY, Potions.AWKWARD)), Ingredient.fromStacks(new ItemStack(ItemList.nectar)), PotionUtils.addPotionToItemStack(ItemStack.EMPTY, PotionList.LOVE.get()));
		} );
	}
	
	public static final DeferredRegister<Effect> EFFECTS = DeferredRegister.create(ForgeRegistries.POTIONS, DeepAffection.modid);
	public static final DeferredRegister<Potion> POTIONS = DeferredRegister.create(ForgeRegistries.POTION_TYPES, DeepAffection.modid);
	
	//Effects
	public static final RegistryObject<Effect> LOVE_EFFECT = EFFECTS.register("love_effect", () -> new ModEffect(EffectType.BENEFICIAL, 13209599)
			.addAttributesModifier(Attributes.field_233821_d_, "55FCED67-E92A-486E-9800-B47F202C4386", (double)0.34F, AttributeModifier.Operation.MULTIPLY_TOTAL)
			.addAttributesModifier(Attributes.field_233825_h_,"AF8B6E3F-3328-4C0A-AA36-5BA2BB9DBEF3", (double)0.17F, AttributeModifier.Operation.MULTIPLY_TOTAL)
			.addAttributesModifier(Attributes.field_233818_a_, "5D6F0BA2-1186-46AC-B896-C61C5CEE99CC", 10.0D, AttributeModifier.Operation.ADDITION)
			.addAttributesModifier(Attributes.field_233828_k_, "03C3C89D-7037-4B42-869F-B146BCB64D2E", 1.0D, AttributeModifier.Operation.ADDITION));
	
	//Potions
	public static final RegistryObject<Potion> LOVE = POTIONS.register("love", () -> new Potion(new EffectInstance(PotionList.LOVE_EFFECT.get(), 6000)));

My potion works now, but I can't brew it yet. Did I do something wrong with the enqueueWork?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.