Jump to content

Event Handler doesn't work


urabaros

Recommended Posts

Goal: when Entity attacks player, it dies.

Code:

public class EventHandler {

	@SubscribeEvent
	public static void onLivingAttackEvent(LivingAttackEvent event)
	{
	 
		
		if (event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == ItemInit.LAVA_HELMET.get()
				&& event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.CHEST).getItem() == ItemInit.LAVA_CHEST.get()
				&& event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.LEGS).getItem() == ItemInit.LAVA_LEGGINGS.get()
				&& event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.FEET).getItem() == ItemInit.LAVA_BOOTS.get()) 
		{		
			
			 if (event.getSource().getTrueSource().getEntity() instanceof MobEntity) 
			 {
				 event.getSource().getTrueSource().getEntity().onKillCommand();
			 }
				 
		}

	}

}

And:

@Mod(LavaArmor.MODID)
public class LavaArmor {
	public static final String MODID = "lavaarmor";
 
	public LavaArmor() {
		
		IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
		
		bus.addListener(this::setup);
		ItemInit.ITEMS.register(bus);
		MinecraftForge.EVENT_BUS.register(this);
	 	MinecraftForge.EVENT_BUS.register(EventHandler.class);
	}
	
	@SuppressWarnings("deprecation")
	private void setup(final FMLCommonSetupEvent event) {
		
		
	}
}

I think I've done everything I've needed. Moreover, the exact same code works fine in my another project. What's wrong?

Link to comment
Share on other sites

I register it like in a tutorial you mentioned:

public class MyStaticForgeEventHandler {
    @SubscribeEvent
    public static void arrowNocked(ArrowNockEvent event) {
        System.out.println("Arrow nocked!");
    }
}

In the EventHandler

And this in the main class:

MinecraftForge.EVENT_BUS.register(MyStaticForgeEventHandler.class)

When I delete this from the main class and add 

@Mod.EventBusSubscriber(modid = LavaArmor.MODID, bus = Bus.MOD, value = Dist.CLIENT)

to the EventHandler class, my game doesn't work. :(

Link to comment
Share on other sites

Exception message: java.lang.IllegalArgumentException: Method public static void lavaarmor.EventHandler.onLivingAttackEvent(net.minecraftforge.event.entity.living.LivingAttackEvent) has @SubscribeEvent annotation, but takes an argument that is not a subtype of the base type interface net.minecraftforge.fml.event.lifecycle.IModBusEvent: class net.minecraftforge.event.entity.living.LivingAttackEvent

image.png.8785dd49e4fce5c0e115191c447a1bf8.png

Link to comment
Share on other sites

My goal is to kill every enemy that attack player that wears armor. But it just wasn't working for some reason. It's really strange, because I copied every single line from my previous project, where everything works fine.

So game was loading, but entities just don't die.

Whole code: 

https://gist.github.com/daekgorsel/c903cff212bb5e8ecdad20bf2dd13c9a

Link to comment
Share on other sites

Quote

value = Dist.CLIENT

I don't know, maybe the client can't kill entities, because the server controls that?

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yes, I've already delete it, my EventHandler looks like this:

public class EventHandler {
	
	@SubscribeEvent
	public static void onLivingAttackEvent(LivingAttackEvent event)
	{
		if (event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == ItemInit.LAVA_HELMET.get()
				&& event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.CHEST).getItem() == ItemInit.LAVA_CHEST.get()
				&& event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.LEGS).getItem() == ItemInit.LAVA_LEGGINGS.get()
				&& event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.FEET).getItem() == ItemInit.LAVA_BOOTS.get()) 
		{		
			//event.getSource().getTrueSource().getEntity().onKillCommand();
			
			 if (event.getSource().getTrueSource().getEntity() instanceof MobEntity) 
			 {
				 event.getSource().getTrueSource().getEntity().onKillCommand();
			 }
				 
		}

	}
}

 

Link to comment
Share on other sites

Yes, Logger is silent. 

	@SubscribeEvent
	public static void onLivingAttackEvent(LivingAttackEvent event)
	{
		LavaArmor.LOGGER.info("event is here");
	.
	.
	.
	}

It doesn't appear. Why? I'm so confused because the exact same thing works in another project

Link to comment
Share on other sites

I register it in the main class:

@Mod(LavaArmor.MODID)
public class LavaArmor {
	public static final String MODID = "lavaarmor";
 
	public LavaArmor() {
		
		IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
		
		bus.addListener(this::setup);
		ItemInit.ITEMS.register(bus);
		MinecraftForge.EVENT_BUS.register(this);
	 	MinecraftForge.EVENT_BUS.register(EventHandler.class);
	}
	
	@SuppressWarnings("deprecation")
	private void setup(final FMLCommonSetupEvent event) {
		
		
	}
}

 

Whole code: 

https://gist.github.com/daekgorsel/c903cff212bb5e8ecdad20bf2dd13c9a

Link to comment
Share on other sites

Sorry. I've deleted this stuff you mantioned:

Quote

Do not use @OnlyIn.

Why are you using both @EventBusSubscriber as well as the manual registration for EventHandler?

But it still doesn't work.

I'm registering it only once in main class and I don't have @OnlyIn thingy. I can't even imagine why it doesn't work

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.