Jump to content

Damaging Item in Event consumes it


Javisel

Recommended Posts

I've made a LivingHurtEvent that checks if the player is under a certain health threshold, and then checks if they have a specific undamaged item. The Event will then damage the item and heal the player. The damaged item uses OnUpdate to recover durability over time, simulating a cooldown effect.

Problem is: it seems that the item is destroyed even if hasn't taken enough damage. 

Event Code


	@SubscribeEvent

	public void DevilContract(LivingHurtEvent e) {

		if (e.getEntityLiving() instanceof EntityPlayer) {

			EntityPlayer victim = (EntityPlayer) e.getEntityLiving();
			ISouls Souls = victim.getCapability(SoulsProvider.Souls_CAP, null);
			ItemStack stack = ItemStack.EMPTY;
			if (victim.getHealth() <= (victim.getMaxHealth() * .50)) {

				if (victim.inventory.hasItemStack(new ItemStack(ModItems.DemonShield)) && Souls.getSouls() >= 100) {

					for (int i = 0; i < victim.inventory.getSizeInventory(); i++) {
						if (victim.inventory.getStackInSlot(i).getItem().equals(ModItems.DemonShield)) {

							stack = victim.inventory.getStackInSlot(i);
							if (stack.isItemDamaged()) {

							} else if (!stack.isItemDamaged()) {

								victim.heal(victim.getMaxHealth());

								victim.addPotionEffect(new PotionEffect(MobEffects.RESISTANCE, 50, 5));
								victim.addPotionEffect(new PotionEffect(MobEffects.ABSORPTION, 50, 5));

								Souls.consume(100);

								stack.damageItem(stack.getMaxDamage() - 1, victim);

								System.out.println("Item Damage: " + stack.getItemDamage());
							}

							else {

							}
						}
					}

				}
			}

		}
	}


Item Code

 

package com.javisel.items;

import java.util.List;
import java.util.Random;

import javax.annotation.Nullable;

import com.javisel.resourcesystems.KaliModeProvider;
import com.javisel.resourcesystems.PTKProvider;
import com.javisel.resourcesystems.SoulsProvider;

import net.minecraft.client.Minecraft;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.ItemElytra;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class DemonShield extends ItemBase {

	public DemonShield(String name) {
		super(name);
		this.setMaxStackSize(1);
		this.setMaxDamage(168000);
	
		// TODO Auto-generated constructor stub
	}

	
	   @SideOnly(Side.CLIENT)
	    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
	    {
			EntityPlayer p = Minecraft.getMinecraft().player;

		   tooltip.add("PASSIVE: Devil's Contract");
		   tooltip.add("Type: Stim");
		   tooltip.add("If you fall bellow 30% Health, you heal to full, gain resistance, absorption. 140 Minute Cooldown");
		   tooltip.add("Cost: 100 Souls");

		   if (stack.getItemDamage()!=0) {
		   tooltip.add("Cooldown: " + ((stack.getItemDamage())/20)+"s");

		   }
		   
		   else {
			   tooltip.add("Cooldown: Ready");

		   }
		   if (p!=null) {
			   
				tooltip.add("Souls: " + p.getCapability(SoulsProvider.Souls_CAP, null).getSouls());
		   }
		  
		   
	    }
	   

		@Override
		public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
			if (stack.getItemDamage() != 0) {
				
				stack.setItemDamage(stack.getItemDamage()-1);
			}
		


			}
}

 

Link to comment
Share on other sites

9 minutes ago, Javisel said:

Problem is: it seems that the item is destroyed even if hasn't taken enough damage. 

Try stepping through your code in the debugger of your IDE it will show you exactly what is going on.

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

1 hour ago, Animefan8888 said:

Try stepping through your code in the debugger of your IDE it will show you exactly what is going on.

I'm stepping through the debugger and can't figure out what's wrong. If i set the MaxDamage to a lower value (100) it works fine. But a value like 168000 breaks it.

Link to comment
Share on other sites

4 minutes ago, Javisel said:

I'm stepping through the debugger and can't figure out what's wrong. If i set the MaxDamage to a lower value (100) it works fine. But a value like 168000 breaks it.

That number is too big actually. There is a max amount the max damage can be which is like a short, I'm not quite sure.

Edited by Animefan8888

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

2 hours ago, Animefan8888 said:

That number is too big actually. There is a max amount the max damage can be which is like a short, I'm not quite sure.

it’s a signed small int (values between -(2^15) and (2^15)-1)

it’s a 2 byte value

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Just now, Cadiboo said:

it’s a signed small int (values between -(2^15) and (2^15)-1)

it’s a 2 byte value

So between −32,768 and 32,767

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

2 minutes ago, Cadiboo said:

it’s a signed small int (values between -(2^15) and (2^15)-1)

it’s a 2 byte value

Yes that is what I had written before. But then I edited it because I remembered it was saved as a short value and read as a short value because that is the closest thing to its size.

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

1 minute ago, Animefan8888 said:

Yes that is what I had written before. But then I edited it because I remembered it was saved as a short value and read as a short value because that is the closest thing to its size.

I don’t understand the difference (or what a short is I assume it’s a java number), I’ll look it up

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

1 minute ago, Cadiboo said:

I don’t understand the difference (or what a short is I assume it’s a java number), I’ll look it up

It's a two byte number.

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

1 minute ago, Animefan8888 said:

It's a two byte number.

Yep, worked with every primitive except them TIL

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.