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 have this item called metal detector and i want to damage it on use. But different amount depending on how long you use it, how can i do this?

  • Replies 54
  • Views 12.4k
  • Created
  • Last Reply

Top Posters In This Topic

Most Popular Posts

  • Call Item#setNoRepair to stop an Item from being repaired.    

  • You never register CapabilityHandler on the Forge event bus, so CapabiltiyHandler#attachCapability is never called and the capability is never attached to any ItemStack.   What diesieben07 s

  • Override it. Forge will call it when required, you don't call it yourself.

  • Author

stack.damageItem(batteryMulti * coilMulti, player);

 

I'm using this to damage the item inside the onItemUse method, but it doesn't damage the item  :-\

 

pls help

  • Author

the problem is in the line i showed you, or something somewhere else blocks it, because other lines of code right before this execute properly.

 

and for the testing i used detector with 30 damage taken, coil 3, rod 3, display 3 and battery 3

 

package vms.archeology.items;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants.NBT;
import vms.archeology.Reference;
import vms.archeology.blocks.ArcheologyDirt;
import vms.archeology.handlers.EnumHandler;
import vms.archeology.init.ModBlocks;
import vms.archeology.util.Utils;

public class Detector extends Item{


private List<BlockPos> modifiers = new ArrayList();
private BlockPos scanBlock;


public Detector(String unlocalizedName){
	this.setUnlocalizedName(unlocalizedName);
	this.setRegistryName(new ResourceLocation(Reference.MODID, unlocalizedName));
	this.setMaxDamage(200000);

	this.addPropertyOverride(new ResourceLocation("found"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
				if(nbt.hasKey("found")){
					if (nbt.getBoolean("found")){
						return 1.0F;
					}
				}
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("coil"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("coil"))
				return ((float)nbt.getInteger("coil"))/10;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("battery"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("battery"))
				return ((float)nbt.getInteger("battery"))/10;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("rod"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("rod"))
				return ((float)nbt.getInteger("rod"))/10;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("display"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("display"))
				return ((float)nbt.getInteger("display"))/10;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("distance"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("distance_squared"))
				return ((float)nbt.getDouble("distance_squared"))/10000;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("type"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("type"))
				return (((float)nbt.getInteger("type")) + 1)/10;
			}
			return 0;
		}
	});
}

@Override
public void onCreated(ItemStack stack, World worldIn, EntityPlayer playerIn) {
	if(stack.getTagCompound() != null || !(stack.hasTagCompound())){
		NBTTagCompound nbt = new NBTTagCompound();
		nbt.setInteger("coil", 3);
		nbt.setInteger("battery", 3);
		nbt.setInteger("rod", 3);
		nbt.setInteger("display", 3);

		nbt.setBoolean("found", false);
		nbt.setInteger("type", -1);
		nbt.setDouble("distance_squared", 0.0);
		nbt.setInteger("direction", 0);
		stack.setTagCompound(nbt);
	}
	super.onCreated(stack, worldIn, playerIn);
}

@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	ItemStack stack = player.getHeldItem(hand);
	NBTTagCompound nbt = stack.getTagCompound();
	if (nbt != null){
		nbt.setBoolean("found", false);
		stack.setTagCompound(nbt);
	}
	int x = 0;
	int y = 0;
	int z = 0;
	modifiers.clear();
	if(nbt != null){
		if(nbt.hasKey("coil") && nbt.hasKey("battery")){
			int batteryMulti = 0;
			int coilMulti = 0;
			Utils.getLogger().info("Damage: " + stack.getItemDamage());
			if (stack.getItemDamage() < 199500){					
				switch(nbt.getInteger("coil")){
				case 1:
					generateScan(0, 1, 2);
					generateScan(-1, 1, 1);
					Scan(pos, worldIn, false, stack);
					coilMulti = 1;
					break;
				case 2:
					generateScan(0, 2, 3);
					generateScan(-1, 1, 2);
					generateScan(-2, 0, 0);
					Scan(pos, worldIn, false, stack);
					coilMulti = 2;
					break;
				case 3:
					generateScan(0, 3, 4);
					generateScan(-1, 2, 3);
					generateScan(-2, 1, 2);
					Scan(pos, worldIn, false, stack);
					coilMulti = 4;
					break;
				case 4:
					generateScan(2, 1, 2);
					generateScan(1, 2, 3);
					generateScan(0, 2, 3);
					generateScan(-1, 2, 3);
					generateScan(-2, 1, 2);
					Scan(pos, worldIn, false, stack);
					coilMulti = 5;
					break;
				case 5:
					generateScan(0, 2, 3);
					generateScan(-1, 1, 2);
					generateScan(-2, 0, 0);
					Scan(pos, worldIn, true, stack);
					coilMulti = 4;
					break;
				case 6:
					generateScan(0, 1, 2);
					generateScan(-1, 1, 2);
					generateScan(-2, 1, 2);
					generateScan(-3, 1, 1);
					generateScan(-4, 1, 1);
					generateScan(-5, 0, 0);
					Scan(pos, worldIn, false, stack);
					coilMulti = 4;
					break;
				}
				Utils.getLogger().info("Coil multi: " + coilMulti);
				switch(nbt.getInteger("battery")){
				case 1:
					batteryMulti = 100;
					break;
				case 2:
					batteryMulti = 25;
					break;
				case 3:
					batteryMulti = 10;
					break;
				case 4:
					batteryMulti = 4;
					break;
				case 5:
					batteryMulti = 8;
					break;
				case 6:
					batteryMulti = 34;
					break;
				case 7:
					batteryMulti = 18;
					break;						
				}
				Utils.getLogger().info("Battery multi: " + batteryMulti);

				Utils.getLogger().info("Total multi: " + batteryMulti * coilMulti);

				stack.damageItem(batteryMulti * coilMulti, player);

			} else {
				stack.damageItem(199999 - stack.getItemDamage(), player);
			}
		}
	}



	return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}

public void generateScan(int y, int radius, int max){
	for(int x = 0 - radius; x <= radius; x++){
		for(int z = 0 - radius; z <= radius; z++){
			if(Math.abs(x) + Math.abs(z) <= max)
				modifiers.add(new BlockPos(x, y, z));
		}
	}
}

public void Scan(BlockPos pos, World world, Boolean ultraSound, ItemStack stack){
	NBTTagCompound nbt = stack.getTagCompound();
	for (int i = 0; i < modifiers.size(); i++) {
		scanBlock = new BlockPos(pos.getX() + modifiers.get(i).getX(), pos.getY() + modifiers.get(i).getY(), pos.getZ() + modifiers.get(i).getZ());
		if (world.getBlockState(scanBlock).getBlock().equals(ModBlocks.archeology_dirt)){	
			if(ultraSound && ArcheologyDirt.getIntFromState(world.getBlockState(scanBlock)) != 0 && ArcheologyDirt.getIntFromState(world.getBlockState(scanBlock)) != 14){
				nbt.setBoolean("found", true);
				nbt.setDouble("distance_squared", scanBlock.distanceSqToCenter(pos.getX(), pos.getY(), pos.getZ()));
				nbt.setInteger("type", EnumHandler.Types.getTypeId(ArcheologyDirt.getTypeFromMeta(ArcheologyDirt.getIntFromState((world.getBlockState(scanBlock))))));
				nbt.setInteger("direction", getDirection(modifiers.get(i)));
				stack.setTagCompound(nbt);

			} else if(ArcheologyDirt.getMetalFromMeta(ArcheologyDirt.getIntFromState(world.getBlockState(scanBlock)))){
				nbt.setBoolean("found", true);
				nbt.setDouble("distance_squared", scanBlock.distanceSqToCenter(pos.getX(), pos.getY(), pos.getZ()));
				nbt.setInteger("type", EnumHandler.Types.getTypeId(ArcheologyDirt.getTypeFromMeta(ArcheologyDirt.getIntFromState((world.getBlockState(scanBlock))))));
				nbt.setInteger("direction", getDirection(modifiers.get(i)));
				stack.setTagCompound(nbt);
			}
		}
	}
}

@Override
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
	NBTTagCompound nbt = stack.getTagCompound();
	if(nbt != null && !(nbt.hasNoTags())){
		if (nbt.hasKey("coil"))
			tooltip.add("Coil: " + String.valueOf(nbt.getInteger("coil")));
		else
			tooltip.add("Needs coil");
		if (nbt.hasKey("battery"))
			tooltip.add("Battery: " + String.valueOf(nbt.getInteger("battery")));
		else
			tooltip.add("Needs battery");
		if (nbt.hasKey("display"))
			tooltip.add("Display: " + String.valueOf(nbt.getInteger("display")));
		else
			tooltip.add("Needs display");
		if (nbt.hasKey("rod"))
			tooltip.add("Rod: " + String.valueOf(nbt.getInteger("rod")));
		else
			tooltip.add("Needs rod");

		if (nbt.hasKey("found"))
			tooltip.add("Found something: " + String.valueOf(nbt.getBoolean("found")));		
	}
	super.addInformation(stack, playerIn, tooltip, advanced);

}

public int getDirection(BlockPos pos){
	int x = pos.getX();
	int z = pos.getZ();
	if(x == 0){
		if (z == 0)
			return 0;
		if (z > 0)
			return 1;
		if (z < 0);
			return 5;
	}
	float d = Math.abs((float)z)/((float)x);
	if (d > 2.4){
		if (z > 0)
			return 1;
		return 5;						
	}
	if (d < 0.4){
		if (x > 0)
			return 3;
		return 7;	
	}
	if (x > 0){
		if (z > 0)
			return 2;
		return 4;	
	}
	if (z > 0)
		return 8;
	return 6;	

}

@Override
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
	if (oldStack.getItem().equals(newStack.getItem())){
		//return false;
	}
	return super.shouldCauseReequipAnimation(oldStack, newStack, slotChanged);
}


}

  • Author

The code gets to it i think, because i have few Utils.getLogger() lines scattered trough the code and they all execute properly

and how do i store FIELDS in ItemStack?

  • Author

which files are important?
there is over 5000 files in the folder and you can upload only 100 files at a time

  • Author

i tried this, but it didnt sync at all, idk how does the GitHub for desktop work i could load in the files, but they didn't show in the browser 

  • Author

Can you please try damageItem yourself, because if it also doesn't work, maybe it's because of bug in this forge version

 

I'm using forge-1.11.2-13.20.0.2210

Utils.getLogger().info("Battery multi: " + batteryMulti);
Utils.getLogger().info("Total multi: " + batteryMulti * coilMulti);

What are the outputs of these? I suspect the call to ItemStack#damageItem is working, just with a value of 0.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

  • Author
1 hour ago, Villfuk02 said:

Ok, it worked all the time.

I FEEL SO STUPID NOW

it just can't be damaged in creative mode :blank:

no, the values are more than 0 don't worry

  • Author

When i put two detectors in crafting table or anvil, they combine and repair. I don't want that to be possible, anybody knows how?

3 minutes ago, Villfuk02 said:

When i put two detectors in crafting table or anvil, they combine and repair. I don't want that to be possible, anybody knows how?

 

Call Item#setNoRepair to stop an Item from being repaired.

 

 

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

4 minutes ago, Villfuk02 said:

how do i thank you in this new version of the forum?

 

You can like posts by clicking the "Like this" button in the bottom right of the post. I think that's the only thanking mechanism available now.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • Author

Thanks for everything, now i'd like to make the item use capabilities instead of nbt to store the values.
Where do i start?

14 minutes ago, Villfuk02 said:

Thanks for everything, now i'd like to make the item use capabilities instead of nbt to store the values.
Where do i start?

 

Start by reading the documentation.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • Author

But there are capabilities for storing items, fluids and energy, but i need to just store some ints bools doubles and some fields

 

so what?

Edited by Villfuk02

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.