@SubscribeEvent
public static void onHitWithFirebrand(AttackEntityEvent event) {
if (event.getEntityLiving().getHeldItemMainhand().getItem() == RegistryHandler.FIREBRAND.get()) {
if (event.getPlayer().isAlive()) {
LivingEntity target = (LivingEntity) event.getTarget();
target.setFire(4);
}
}
}
and it works great but the game crashes when you hit an inanimate entity like an item frame. Was wondering if anyone knew why. Thank you
The exception is thrown when you are trying to cast the target to a LivingEntity, but the target is not a LivingEntity. If I remember correctly, the setFire() method is defined in the Entity class, which means that you could remove the casting all together.
You can do the same by overriding hitEntity() in The Item class. That would be more efficient as the LivingAttackEvent fires every time you attack something. But hitEntity() fires only when you hit something with the item. You could reduce potential lag.
Ok create a new class called [YourItemName] in items package. Make your class extend Item create the constructor. Inside the constructor super(new Item.Properties().group(ItemGroup here)). You can add new properties in that super.
Then override hitEntity and do whatever you want.
Now register the Item. I hope u use DeferredRegister. Register the Item like this:
public static final RegistryObject<YourItemClass> = ITEMS.register(“your_item_name”, YourItemClass::new);
If you learn java before modding your Modding Experience will be 100x better. Don’t learn Java through modding. Or don’t mod Minecraft while learning java.
I made this event:
The exception is thrown when you are trying to cast the target to a LivingEntity, but the target is not a LivingEntity. If I remember correctly, the setFire() method is defined in the Entity class, which means that you could remove the casting all together.
alright ill give it a try, thank you
You can do the same by overriding hitEntity() in The Item class. That would be more efficient as the LivingAttackEvent fires every time you attack something. But hitEntity() fires only when you hit something with the item. You could reduce potential lag.
removing the casting gives me an error (sorry for the delay i had to reset my pc) how exactly would i override it? im not very experienced with events
Ok create a new class called [YourItemName] in items package. Make your class extend Item create the constructor. Inside the constructor super(new Item.Properties().group(ItemGroup here)). You can add new properties in that super.
Then override hitEntity and do whatever you want.
Now register the Item. I hope u use DeferredRegister. Register the Item like this:
public static final RegistryObject<YourItemClass> = ITEMS.register(“your_item_name”, YourItemClass::new);
Note that I gave you the answer to a simple Java problem. Please learn java: https://docs.oracle.com/javase/tutorial/
If you learn java before modding your Modding Experience will be 100x better. Don’t learn Java through modding. Or don’t mod Minecraft while learning java.
yes i use deferred register ill give it a go thank you