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.