Jump to content

Custom Multi-Tool


memcallen

Recommended Posts

I created an Item, it's essentially a customizable tool. I can't make it detect when you break a block though. Also this code doesn't work:

	public float getDigSpeed(ItemStack itemstack, Block block, int metadata){
	NBTTagCompound nbt = itemstack.getTagCompound();
	try{
		if(nbt!=null){
			System.out.println("has NBT");
	speed = nbt.getInteger("Engine");
	tool = nbt.getInteger("Tool");
		}else System.out.println("Doesn't have NBT");
	}catch(Exception e){e.printStackTrace();}

	if(block.getMaterial()==Material.rock){
		if(tool==1){
			return speed*10;
		}
	}
	if(block.getMaterial()==Material.wood){
		if(tool==2){
			return speed*6;
		}
	}
	if(block.getMaterial()==Material.ground){
		return speed*3;
	}


	return super.getDigSpeed(itemstack, block, metadata)+1F;

}

 

EDIT:

scratch that, I made it work, but it drops two items when I do break the block. how could I fix this?- also it's only when I break a block that you can break with your fist.

The proud(ish) developer of Ancients

Link to comment
Share on other sites

Yes but the thing is, is that I would need a TON of code for what i'm making. It's basically a tool that can break certain blocks depending on the NBT, kind of like a super Tinkerer's Construct tool. I have the part that breaks the blocks at different speeds but if I break a block that you can break with your fist and get an item, it drops two items.

 

Here's the onBlockDestroyed function:

public boolean onBlockDestroyed(ItemStack itemstack,World world, Block block,int x,int y,int z,EntityLivingBase elb){

	List<ItemStack> drops = block.getDrops(world, x, y, z, world.getBlockMetadata(x,y,z), 0);

	for(ItemStack is:drops){
		if(!world.isRemote)
		world.spawnEntityInWorld(new EntityItem(world,x,y,z,is));
	}

	return true;
}

so it's an "artificial" tool if you want to call it that. It doesn't actually extend ItemTool.

The proud(ish) developer of Ancients

Link to comment
Share on other sites

Blocks handle their own drops, believe it or not!

 

I notice you are spawning items in the world when onBlockDestroyed is called. If you do this, you will get more than what the block drops normally, as the block you destroyed has its own drop list which it also calls when it is broken. The reason it behaves this way only for things you can break with your fists is because those blocks have a low harvest level, and will drop whether or not the tool used to break them is of an effective harvest level. For blocks with higher harvest level, they won't drop if the tool does not have a sufficient harvest level. Your tool will the block's drops regardless, and so ends up spawning the drops twice for blocks with a low harvest level.

 

If you want to continue using this method, you'll have to make it so that the block doesn't drop its own drops when broken with your tool. I would not recommend this method.

 

Another way might be to set your tool's harvest level, and override the methods that test whether the tool is effective against a given material.

Link to comment
Share on other sites

As said above - this is NOT good way of doing this, but hey - "If it works, it ain't stupid"

 

1. Use HarvestDropsEvent from net.minecraftforge.event.world.BlockEvent

2. Subscribe to it and check if harvester is not null and is player (second is not that much needed).

3. If so, then check currently held item, if item == your item, then start dropping from droplist (which is also in event).

4. Also if you wanna do it this way remember to remove your code from item, because now it'll be in event.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Yes, it is certainly possible!

 

Everything extending from net.minecraft.block has a getHarvestLevel method, so you have all the pieces you need at your disposal already. Once again, I do not recommend continuing down this path, as you will be overriding existing universal methods (blocks have drop lists and drop items on their own) with your own methods (spawn items in world via unusual tool method). My prediction is that doing this will turn out to be more trouble than it's worth.

 

I will assume you have considered all this and a very good reason for doing it this way, so cheers and good luck!

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.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.