Jump to content

Damaging an item with onItemRightClick() without deleting the item when it runs out of durability.


werdwar

Recommended Posts

I'm trying to use the durability of an item to represent stored magicl energy; sort of like a battery.

To do this, I am overriding the onItemRightClick() method in a custom class called itemGem that extends Item:

@MethodsReturnNonnullByDefault
public class ItemGem extends Item {
    ItemGem(Item.Properties properties) {
        super(properties);
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
        // get the itemstack being held by the player
        ItemStack itemstack = player.getHeldItem(hand);

        // if we're in the logical client and the gem still has power left:
        if(!world.isRemote && itemstack.getDamage() < itemstack.getMaxDamage()) {
            player.getCapability(MagicPowerCapability.MAGIC_POWER_CAPABILITY).ifPresent(new NonNullConsumer<IMagicPower>() {
                @Override
                public void accept(@Nonnull IMagicPower iMagicPower) {
                    // add one power to the player's capability
                    iMagicPower.addOrSubtractAmount(1);
                    // send a message to the player with their new power total
                    player.sendStatusMessage(new StringTextComponent("new power total: " + iMagicPower.getAmount()), false);
                }
            });
        }

        if(!world.isRemote) {
            // damage the item
            itemstack.damageItem(1, player, PlayerEntity -> {/* need to give a damaged version or something here so it doesn't just disappear? */});
            //TODO: figure out why the gem disappears after its power is used up.
        }

        return super.onItemRightClick(world, player, hand);
    }
}

My issue is that when the item hits zero durability, it disappears; I want it to sitck around at zero durability so that it can be recharged.

 

As I understand it, the third argument of damageItem() is a Consumer that is triggered when the item hits zero durability, but I'm not sure what I can or should do there. 

I'm not sure if I should somehow give the player a different item (e.g. an uncharged_gem) that can be recharged, or if I can somehow prevent the item from being removed. 

I guess I could just leave it at one damage and call that "empty" so that it isn't removed, but I feel like there should be a better solution.

Edited by werdwar
Link to comment
Share on other sites

Thank you very much! I have this working with a capability I added to my item, however, the durability bar isn't updating until I move it around in the inventory or drop it and pick it back up.

Is there another function I can call within onItemRightClick to make sure the durability bar updates on use?

Here is my custom class again with the overidden methods:

@MethodsReturnNonnullByDefault
public class ItemGem extends Item {

    private int max_power;

    ItemGem(Item.Properties properties, int max_power) {
        super(properties);
        this.max_power = max_power;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) throws NoSuchElementException {
        // get the itemstack being held by the player
        ItemStack itemstack = player.getHeldItem(hand);
        // get the itemstack's capability
        IGemPower iGemPower = itemstack.getCapability(GemPowerCapability.GEM_POWER_CAPABILITY).orElseThrow(() -> new NoSuchElementException("Missing 'GEM_POWER' Capability on an item of type ItemGem!"));

 		// if we're in the logical client and the gem still has power left:
        if(!world.isRemote && iGemPower.getAmount() > 0) {
            // get the player's capability
            IMagicPower iMagicPower = player.getCapability(MagicPowerCapability.MAGIC_POWER_CAPABILITY).orElseThrow(() -> new NoSuchElementException("Capability 'MAGIC_POWER' missing on player entity: all players should have a MAGIC_POWER capability"));

            // add one power to the player's capability
            iMagicPower.addOrSubtractAmount(1);
            // send a message to the player with their new power total
            player.sendStatusMessage(new StringTextComponent("new power total: " + iMagicPower.getAmount()), false);

            // subtract one power from the item's capability
            iGemPower.addOrSubtractAmount(-1);
        }

        return super.onItemRightClick(world, player, hand);
    }

    @Override
    public boolean showDurabilityBar(ItemStack stack) {
        return true;
    }

    @Override
    public double getDurabilityForDisplay(ItemStack stack) {
        IGemPower iGemPower = stack.getCapability(GemPowerCapability.GEM_POWER_CAPABILITY).orElseThrow(() -> new NoSuchElementException("Missing 'GEM_POWER' Capability on an item of type ItemGem!"));
        double durability = (double)(this.max_power - iGemPower.getAmount()) / (double)this.max_power;
        return durability;
    }
}

 

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.