Jump to content

Fixing Events In My Main Class


OberonPuck

Recommended Posts

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));
        	    	}
        	    }
    		}
    	}
    }
}

 

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.



×
×
  • Create New...

Important Information

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