help to make an item that whem click on main hand, fix/heals up the durability of an item off-hand

Started by JPZamps on

Topic category: Help with modding (Java Edition)

Last seen on 21:05, 13. Apr 2024
Joined Dec 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
help to make an item that whem click on main hand, fix/heals up the durability of an item off-hand

The idea would basically be a Silver tape that the player could hold in his hand and it would only be necessary to have an item in his left hand/off-hand, as soon as the player clicked the item in his left hand/off-hand would heal +16 durability and the tape would disappear or lose -1 durability 

Any ideias how? im kind lost with this one

Last seen on 01:06, 14. Mar 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I don't think you can simply…
Wed, 03/15/2023 - 04:14

I don't think you can simply use code blocks to achieve this functionality. You may need to write code in the use() function of your Item class. Here's some example code I wrote that can consume 1 durability point from a tape item and repair 16 durability points of another item held in the player's offhand. If the offhand item is not damaged or is also a tape item, the repair will not be attempted to prevent two tape items from repairing each other.



    @Override
    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {

        // Get the itemStack that the player is holding need repair tool
        ItemStack itemStack = player.getItemInHand(
                hand == InteractionHand.MAIN_HAND ? InteractionHand.OFF_HAND : InteractionHand.MAIN_HAND);
        // Get the itemStack that the player is using as a tape to repair the tool
        ItemStack tape = player.getItemInHand(hand);

        // If the tool being repaired is damaged and is not RepairTape
        if (itemStack.isDamaged() && !itemStack.is(this)) {
            // Repair the tool being held by the player
            itemStack.setDamageValue(Math.max(0, itemStack.getDamageValue() - 16));
            // Decrease the durability of the tape itemStack
            tape.hurtAndBreak(1, player, (living) -> {
                living.broadcastBreakEvent(hand);
            });
        }
        return super.use(level, player, hand);
    }