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

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?

  • Author

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

  • Author
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

  • Author

Ye! I have been using this in my main class:

MinecraftForge.EVENT_BUS.register(EventHandler.class);

But it wasn't registering in the game.

Quote

value = Dist.CLIENT

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

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.

  • Author

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

	}
}

 

  • Author

I'm not sure about you question, but when Entity attacks my, nothing appears on Console. So I think it's not called... am I right?

  • Author

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

  • Author

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

  • Author

No, I'm not using them. My EventHandler and Main class sools exactly as I mentioned before. I've dropped gist to show other classes.

  • Author

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

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

Only this way! I've put Logger into this method and let a zombie hit me. Logger didn't show up. 

  • Author

Sorry, I can't do that. My project is not loading to git repo. Can I just provide a screenshot or something?

  • Author

THANK YOU SO MUCH I WOULD NEVER NOTICED IT MYSELF!!! 😭😭😭

Thank you that you invested so much time to help me! It works perfectly! I hope you'll have a nice day! Thank you so much!

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.