Skip to content
_Mokus edited this page Dec 22, 2025 · 5 revisions

Custom Cooldown

In the item code, implement MokusLibCooldownItem.

import net.mokus.mokuslib.cooldown.MokusLibCooldownItem;
@Override
public int getMaxCooldown(ItemStack item) {
    return MAXCOOLDOWN_INT;
}

@Override
public int getAddedCooldown(ItemStack item) {
    return ADDED_COOLDOWN_INT;
}

It will add these methods to assign the max cooldown and the added ones.

Check if you can use the item.
If you want the item to not be usable after it reached the cooldownThreshold you have to call this.
!checkCooldown(user, this);

To apply the cooldown:
applyCooldown(playerEntity, stack);

Explanations

The cooldownThreshold where you can use the item, this is the equation: currentCooldown <= (maxCooldown + 10 - addedCooldown); An example, if your maxCooldown is 240 and your added one is 120. The cooldown threshold is 110 meaning if your currentCooldown is under that number you can use the item.

Example Item Code

This code is from 1.21.1 but the Library's code is the same both for 1.20.1 and 1.21.1

package net.mokus.mokuslib.item;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.mokus.mokuslib.cooldown.MokusLibCooldownItem;

public class CooldownTestItem extends SwordItem implements MokusLibCooldownItem {

    public CooldownTestItem(ToolMaterial toolMaterial, Settings settings) {
        super(toolMaterial, settings);
    }

    @Override
    public int getMaxCooldown(ItemStack item) {
        return 240;
    }

    @Override
    public int getAddedCooldown(ItemStack item) {
        return 60;
    }

    @Override
    public UseAction getUseAction(ItemStack stack) {
        return UseAction.SPEAR;
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
        ItemStack itemStack = user.getStackInHand(hand);
        if (!checkCooldown(user, this)) {
            return TypedActionResult.fail(itemStack);
        }
        user.setCurrentHand(hand);
        return TypedActionResult.consume(itemStack);
    }

    @Override
    public int getMaxUseTime(ItemStack stack, LivingEntity user) {
        return 72000;
    }

    @Override
    public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
        if (!(user instanceof PlayerEntity playerEntity)) return;

        float f = playerEntity.getYaw();
        float g = playerEntity.getPitch();
        float h = -MathHelper.sin(f * ((float) Math.PI / 180.0F)) * MathHelper.cos(g * ((float) Math.PI / 180.0F));
        float k = -MathHelper.sin(g * ((float) Math.PI / 180.0F));
        float l = MathHelper.cos(f * ((float) Math.PI / 180.0F)) * MathHelper.cos(g * ((float) Math.PI / 180.0F));
        float m = MathHelper.sqrt(h * h + k * k + l * l);
        float n = 3.0F * ((1.0F * 2) / 4.0F);
        h *= n / m;
        k *= n / m;
        l *= n / m;
        playerEntity.addVelocity(h, k, l);
        playerEntity.useRiptide(40,0.0f,stack);
        applyCooldown(playerEntity,stack);
    }
}

Clone this wiki locally