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.

[SOLVED] [1.11] Create a custom tool that loses durability when right-clicking a block, or breaking blocks

Featured Replies

Posted

Hey everyone,

 

I want to make a custom tool that loses durability when either, right-clicking a specific block, or breaking any block. (I can do basic block checks here) The main thing I'm struggling with is actually removing durability from the tool, and registering it correctly. I currently have a tool that extends ItemTool and have registered it. Just having trouble setting and removing durability.

 

(I know this is probably super wrong what I'm tying to do here) Bottom code crashes immediately when breaking a block, a null pointer on something called getStrVsBlock() .

 

Item Carving Tool:

public class ItemCarvingTool extends ItemToolBase {
    public ItemCarvingTool(String name, float attackDamageIn, float attackSpeedIn, ToolMaterial materialIn, Set<Block> effectiveBlocksIn) {
        super(name, attackDamageIn, attackSpeedIn, materialIn, effectiveBlocksIn);
        setMaxStackSize(1);
        setMaxDamage(15);
    }

    @Override
    public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving) {
        stack.setItemDamage(stack.getItemDamage() - 1);
        return false;
    }
}

 

Item Tool Base:

public class ItemToolBase extends ItemTool {
    public ItemToolBase(String name, float attackDamageIn, float attackSpeedIn, ToolMaterial materialIn, Set<Block> effectiveBlocksIn) {
        super(attackDamageIn, attackSpeedIn, materialIn, effectiveBlocksIn);
        this.setUnlocalizedName(name);
        this.setRegistryName(new ResourceLocation(Reference.MODID, name));
    }
}

 

Register item:

public static final Item carving_tool = new ItemCarvingTool("carving_tool", 2.0f, 2.0f, Item.ToolMaterial.IRON, null);

 

Edited by MSpace-Dev
'final' wasn't copied

To damage the itemstack, call ItemStack::damageItem instead. It automatically handles breaking when durability <= 0 & deals with statistics as well.

 

You also need to provide an empty Set<Block> instead of null when instantiating your ItemCarvingTool.

 

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

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.