Jump to content

Recommended Posts

Posted

So I am creating an event where if a player is holding a certain item and is behind a player, they can perform a "backstab." I figured out everything except how to get the item from the source of damage to make sure they are holding the correct item...
My Code:
 

	@SubscribeEvent
    public void onLivingHurtEvent(LivingHurtEvent event) {
		if (event.getSource().getImmediateSource() != null) {
			if (event.getSource().getImmediateSource().getHeldEquipment() == _Items.itemAquaDagger) {
			    System.out.println("Player With Aqua Dagger Detected");
				double targetYaw = event.getEntityLiving().getRotationYawHead();
				double playerYaw = event.getSource().getTrueSource().getRotationYawHead();
				if(Math.abs(targetYaw - playerYaw) < 80 || Math.abs(targetYaw - playerYaw) > 280) {
					event.setAmount(event.getAmount() * 5);
					event.getEntity().playSound(SoundEvents.BLOCK_ANVIL_HIT, 1.0F, 1F);
				}
			}
		}
    }

I did find .getHeldEquipment() but I am very certain that is not what I want for this case. Btw the system.out.println was just a test to make sure it was the if statement that was the problem.

Posted

.getHeldEquipment() returns an ItemStack, it will never be equal to an Item.

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.

Posted (edited)

Here is some example code that checks if the player is holding a carrot-on-a-stick (found in the EntityPig class).

[Mojang Code Removed]

Notice that this code checks first if the entity is a player and then checks both main hand and off hand. 

 

Edit: Oops, didn't realize I shouldn't be pasting Mojang code here. Totally makes sense though. For anyone seeing this later, just check the EntityPig method "canBeSteered"

Edited by Icedice9
Removed Mojang Code
Posted (edited)
29 minutes ago, Draco18s said:

.getHeldEquipment() returns an ItemStack, it will never be equal to an Item.

Well, do you have any other ideas? I used to work on this on 1.10.2 until I had school stuff and came back to this a long while later. In 1.10.2, you could have used .getHeldItemMainhand().getItem() and work from there. However, I guess in 1.12.2 they changed it so you can't use that anymore. Btw, .getHeldEquipment() returns an Iterable<ItemStack> to my knowledge, the code I put up there was just as far as I gotten to.

Edit: IceDice9, grabbing the information from the player would work since I can use .getHeldItemMainhand() from there, but I need to grab it from event to make sure I am getting the source of the damage, not just a random player (I don't even know how that would go on a multiplayer server)

Edited by Inforno
Answering another reply in edit without having to double post
Posted
3 minutes ago, Inforno said:

Btw, .getHeldEquipment() returns an Iterable<ItemStack> to my knowledge, the code I put up there was just as far as I gotten to.

Your comparison will still not be true.

3 minutes ago, Inforno said:

In 1.10.2, you could have used .getHeldItemMainhand().getItem() and work from there.

This would still work...that method still exists.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted (edited)
4 minutes ago, Animefan8888 said:

Your comparison will still not be true.

This would still work...that method still exists.

The method still exists yes, but I spent more time than needed trying to find a way to find and use it. I mean that the method was removed from .getImmediateSource to my knowledge (unless there is some obvious right in front of me way to still use it and wasted an entire hour cause I am blind)

Edited by Inforno
Grammar error
Posted (edited)
10 minutes ago, Animefan8888 said:

Returns an Entity, getHeldItemMainHand is a method in EntityLivingBase.

I wished I was that blind and that worked. Unfortunately, since I am using LivingHurtEvent, EntityLivingBase, in this case, would be the victim. So basically this line:
 

if (event.getEntityLiving().getHeldItemMainhand().getItem() == _Items.itemAquaDagger) {

would mean the victim has to hold the dagger, not the player.
Unless of course there is something other than LivingHurtEvent I can use..

Edited by Inforno
Posted
1 minute ago, Inforno said:

Unless of course there is something other than LivingHurtEvent I can use..

Entity can be cast to EntityLivingBase or EntityPlayer, which has those methods. Also you should use an instanceof check. 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted (edited)
17 minutes ago, Animefan8888 said:

Entity can be cast to EntityLivingBase or EntityPlayer, which has those methods. Also you should use an instanceof check. 

Thank you! Tbh this is my first time using events, instanceof, or casting as somewhat new to java and forge
Im posting my code incase anyone wants it in the future or you double check incase I did something wrong (but it works so eh):
 

	@SubscribeEvent
    public void onLivingHurtEvent(LivingHurtEvent event) {
		if (event.getSource().getImmediateSource() != null) {
			if (event.getSource().getImmediateSource() instanceof EntityPlayer) {
				EntityPlayer player = (EntityPlayer) event.getSource().getImmediateSource();
				if (player.getHeldItemMainhand().getItem() == _Items.itemAquaDagger) {
					double targetYaw = event.getEntityLiving().getRotationYawHead();
					double playerYaw = event.getSource().getTrueSource().getRotationYawHead();
					if(Math.abs(targetYaw - playerYaw) < 80 || Math.abs(targetYaw - playerYaw) > 280) {
						event.setAmount(event.getAmount() * 5);
						event.getEntity().playSound(SoundEvents.BLOCK_ANVIL_HIT, 1.0F, 1.0F);
					}
				}
			}
		}
    }

 

Edited by Inforno

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.