Jump to content

1.19.2 Create Custom Event Methods


ChasePlays

Recommended Posts

I am trying to create a method that can be overridden in my AxeItemEx class that extends AxeItem

Ex. The method in AxeItemEx

public void onHurtByLightning(Player p, int amnt){
	//Gather Player/or Entity instance and DamageSource instance
	//and check if the damage came from a lightning bolt
	//then return the Player/or Entity instance and the amount of damage the player recieived
}

 

I want this method to run every tick and check when the player has recieived damage and then check if the damage is caused from a lightning bolt and if it is true it will trigger that method in any Custom Item that extends my custom AxeItemEx class so then you could be able to call it like this

@Override
public void onHurtByLightning(Player p, int amnt){
	p.displayClientMessage(Component.literal("Damage: " + amnt), false);
}

I have no idea how to make this happen but here are the things I tried to do.

1. Created a new class named AxeHurtEvent and extended Event(I did not understand what I was suppose to do)

2.Created a new class that implemented IForgeItem(probably not what I am looking for)

none of the above work for obvious reasons as I have no clue how they work since the code isnt present. Any ideas or explanations? 

Side Note: I know you cant have a return Object in a void method but I have no clue how the regular Events work and how they are able to store the correct object instances in their constructor so that we may use it for our needs.

Edited by ChasePlays
Link to comment
Share on other sites

LivingHurtEvent

The DamageSource will be LIGHTNING_BOLT

I have no idea how your Axe relates to this. Maybe LivingEntity.getItemInHand() ?

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

perhaps I didnt word this correctly, I know how LivingHurtEvent works and how to check if player was damaged by lightning. Thats not what I am trying to achieve. You know how AxeItem has event-like methods such as onLeftClickEntity and onUsingTick? I want to create my own class named AxeItemEx that extends AxeItem so that I may create my own custom event-like methods such as the one listed above (onHurtByLightning). Thing is I have no idea how the event-like methods from AxeItem class work since they are from the interface IForgeItem and there is no proper documentation on either of them and I haven't seen anybody else on this forum trying to achieve what I am trying to do.

So long story short I am trying to create an event-like method that can be overriden by any custom item

Ex. 

public class ThunderAxe extends AxeItemEx{
	@Override
	public void onHurtByLightning(Player p, int amnt){
		//do whatever I want here when this event is triggered
	}
}

 

Link to comment
Share on other sites

They work by magic. Or maybe not. 🙂 

They work because somebody calls them from the relevant place in the code.

I gave you the answer above. Pseudo code:

public static void livingHurt(LivingHurtEvent event) {
   if (event.damageSource() == DamageSource.LIGHTNING_BOLT 
       && event.getEntity() instanceof Player player
       && player.getItemInHand(MAIN_HAND).getItem() instanceof AxeItemEx axeItemEx
       ) {
       axeItemEx.onHurtByLightning(player, event.getAmount());
   }
}

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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.