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

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

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.

  • Author

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

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.

  • Author

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.)

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.

  • Author

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.

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.

  • Author

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

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.