Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

So it has come to my attention that I have no idea what I am doing when it comes to events.

I have one event, injectPotion, that is supposed to:

  1. Use the LivingHurtEvent to check if the true damage source is a player.
  2.  Get the item in their hand.
  3. Check to see if that item is my custom potion item, which extends PotionItem.
  4. Then search that item for its effects
  5. And then apply those effects to the victim of the damage.

However, this does not happen and I don't think the current issue is with how I am applying the effects, because I tried to replace the potion effect code with code that clears the player entity's inventory, and it still didn't work.

So in short, how to I fix this event and is there a way to improve the quality of the code in my main class?

 

Main Class:

package com.oberonpuck.minormagicks;
//All of imports are here but I commented them out for ease of reading

// The value here should match an entry in the META-INF/mods.toml file
@Mod("minormagicks")
public class MinorMagicks
{
	public static MinorMagicks instance;
    private static final Logger LOGGER = LogManager.getLogger();
    public static final String MOD_ID = "minormagicks";
    public MinorMagicks() 
    {
    	instance = this;
    	
    	final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
    	
        modEventBus.addListener(this::setup);
        modEventBus.addListener(this::doClientStuff);
        modEventBus.addListener(this::injectPotion);
       
        ItemRegistryHandler.ITEMS.register(modEventBus);
        PotionRegistryHandler.POTIONS.register(modEventBus);
        MinecraftForge.EVENT_BUS.register(this);
    	

    }

    private void setup(final FMLCommonSetupEvent event)
    {
    	PotionRegistryHandler.removeRecipes();
    	MagicksPotionBrewing.init();
    }
    private void doClientStuff(final FMLClientSetupEvent event) 
    {
    	
    }
    private void injectPotion (LivingHurtEvent event)
    {
		if(event.getSource().getTrueSource() instanceof PlayerEntity)
    	{
			PlayerEntity playerentity =  (PlayerEntity) event.getSource().getTrueSource();
    		LivingEntity entityLiving = event.getEntityLiving();
    		ItemStack stack = playerentity.getHeldItemMainhand();
    		World world = playerentity.getEntityWorld();
    		if(stack.getItem() instanceof GelledPotionItem)
    		{
    			if (playerentity instanceof ServerPlayerEntity) 
        	    {
        	       CriteriaTriggers.CONSUME_ITEM.trigger((ServerPlayerEntity)playerentity, stack);
        	    }

        	    if (!world.isRemote) 
        	    {
        	    	for(EffectInstance effectinstance : PotionUtils.getEffectsFromStack(stack))
        	    	{
        	    		if (effectinstance.getPotion().isInstant())
        	    		{
        	    			effectinstance.getPotion().affectEntity(playerentity, playerentity, entityLiving, effectinstance.getAmplifier(), 1.0D);
        	    		} 
        	    		else 
        	    		{
        	    			entityLiving.addPotionEffect(new EffectInstance(effectinstance));
        	    		}
        	    	}
        	    }

        	    if (playerentity != null) 
        	    {
        	    	playerentity.addStat(Stats.ITEM_USED.get(ItemRegistryHandler.GELLED_POTION.get()));
        	    	if (!playerentity.abilities.isCreativeMode) 
        	    	{
        	    		stack.shrink(1);
        	    	}
        	    }

        	    if (playerentity == null || !playerentity.abilities.isCreativeMode) 
        	    {
        	    	if (stack.isEmpty()) 
        	    	{
        	    		playerentity.inventory.storeItemStack(new ItemStack(Items.GLASS_BOTTLE));
        	    	}
        	    	if (playerentity != null) 
        	    	{
        	    		playerentity.inventory.storeItemStack(new ItemStack(Items.GLASS_BOTTLE));
        	    	}
        	    }
    		}
    	}
    }
}

 

Learn the difference between the forge and mod event bus. Hint: LivingHurtEvent is not called on the mod event bus.

 

8 minutes ago, OberonPuck said:

!world.isRemote

9 minutes ago, OberonPuck said:

playerentity instanceof ServerPlayerEntity

These two also do the exact same thing.

Edited by ChampionAsh5357

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.