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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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