Jump to content

[1.11]More problems with custom item


Villfuk02

Recommended Posts

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


}

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

 

 

  • Like 1

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.