Jump to content

[1.10] LivingHurtEvent not damaging entities


tripl3dogdare

Recommended Posts

I'm working on a mod that requires some rather complex damage overrides, so I'm catching the LivingHurtEvent to apply those. However, in certain cases the damage is not applied, as if the event is being canceled, despite never canceling it in my code.

 

The code:

@SubscribeEvent public void onLivingHurt(LivingHurtEvent e) {
if(e.getSource().getEntity() instanceof EntityPlayer) {
	EntityPlayer player = (EntityPlayer)e.getSource().getEntity();

	boolean weaponEquipped = false, armorEquipped = false;
	Item weapon = null, armor = null;
	for(Item weapon_ : ItemRegistry.buffPairs.keySet()) {
		weapon = weapon_;
		armor = ItemRegistry.buffPairs.get(weapon);

		if(player.getHeldItemMainhand().getItem() == weapon) {
			if(!player.getHeldItemMainhand().getTagCompound().getBoolean("active")) return;
			weaponEquipped = true;

			for(ItemStack i : player.getArmorInventoryList()) {
				if(i.getItem() == armor) {
					armorEquipped = true;
					break;
				}
			}

			break;
		}
	}

	if(weaponEquipped) e.setAmount(e.getAmount()+(float)((ItemTimedWeapon)weapon).getAttackDamage()-1f);
	if(armorEquipped) e.setAmount(e.getAmount()*(float)((ItemCharacterArmor)armor).getDamageMultiplier());
}
}

 

Basically, I'm checking 1) if the player is holding a specific weapon and if so 2) if the weapon is in the "active" state based on it's NBT, and finally 3) whether a corresponding armor item is equipped. Then I apply changes to the damage based on whether the weapon/armor is equipped. However, the item only does damage if it is not in the active state, otherwise it acts like you didn't hit at all. Really having trouble... any ideas?

 

Edit: slight code updates, no fix

Link to comment
Share on other sites

So your problem is happening here

 

for(ItemStack i : player.getArmorInventoryList()) {
     if(i.getItem() == armor) {
          armorEquipped = true;
          break outer;
     }
}

 

Look at it closely.

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.

Link to comment
Share on other sites

I don't see what the issue is there, I'm simply breaking the outer loop rather than the inner one and that's the only thing I can see that you could be talking about. It's a rather obscure bit of Java syntax, but it should work perfectly fine.

 

Edit: removed it anyway as it was redundant, but it didn't change anything

Link to comment
Share on other sites

When you break to there instead of exiting the loop you actually start the next iteration.

 

*edit Don't question this I was thinking of something else sorry.

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.

Link to comment
Share on other sites

Basically the code checks three things:

 

1. Is a certain weapon in the player's main hand? If so:

2. Is the weapon active? and...

3. Is a specific piece of armor equipped?

 

If the weapon is in the main hand but inactive, it leaves it as is.

If the weapon is active, it adds the weapon's attack damage to the event damage.

If both the weapon and the armor are equipped, it multiplies that result by the armor's damage multiplier.

 

At least, that's what it should do. What it's actually doing, though, is preventing you from hitting at all if the weapon is active. (You can hit normally if it isn't.)

Link to comment
Share on other sites

Have you tried doing any debugging, like printing out what item it is currently on from the keyset (unlocalized name) into the console. To see if it is even detecting the item being there. Also are you sure that your buffPairs is gaining the items.

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.

Link to comment
Share on other sites

No, I hadn't - however, attempting to do so led me to the problem(s), namely a NullPointerException when trying to get the item in an empty armor slot and an accidental recursive self-call in ItemCharacterArmor.getAttackMultiplier rather than referencing the desired variable. I apologize for my stupidity, and thank you for working through it with me.

Link to comment
Share on other sites

for(Item weapon_ : ItemRegistry.buffPairs.keySet()) {
		weapon = weapon_;
		armor = ItemRegistry.buffPairs.get(weapon);
		if(player.getHeldItemMainhand().getItem() == weapon) {

 

Uh.  Why are you setting the active weapon to the weapon_ keyvalue before checking if the player is actually wielding it?

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

I admit that that wasn't nearly the best way to go about it (the entire system). That's why I changed to this:

 

@SubscribeEvent public void onLivingHurt(LivingHurtEvent e) {
if(e.getSource().getEntity() instanceof EntityPlayer) {
	EntityPlayer player = (EntityPlayer)e.getSource().getEntity();
	if(player.getHeldItemMainhand() == null || !ItemRegistry.buffPairs.containsKey(player.getHeldItemMainhand().getItem())) return;
	if(!player.getHeldItemMainhand().getTagCompound().getBoolean("active")) return;

	ItemTimedWeapon weapon = (ItemTimedWeapon)player.getHeldItemMainhand().getItem();
	ItemCharacterArmor armor = (ItemCharacterArmor)ItemRegistry.buffPairs.get(weapon);

	for(ItemStack i : player.getArmorInventoryList()) {
		if(i != null && i.getItem() == armor) {
			e.setAmount(weapon.onActiveAttack(e, i));
			break;
		}

		e.setAmount(weapon.onActiveAttack(e, null));
	}
}
}

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.