No chat to show

This project's files are all here to explore.

Files
mace-cooldown.sk
Code
1 116
mace-cooldown.sk
# ============================================================
#   Mace Cooldown System
#   - 2 mace hits = 30s cooldown
#   - No mace hit for 15s = hit counter resets
#   - Works with breach enchant swapping
# ============================================================

options:
    prefix:        &8[&6⚔ Mace&8]&r
    hit-limit:     1
    cooldown-sec:  180
    reset-sec:     0

    # Colors
    c-warn:   &e
    c-bad:    &c
    c-good:   &a
    c-gray:   &7

# ── Helper: check if an item is a mace (any enchants, including Breach) ──────
function isMaceItem(i: item) :: boolean:
    if type of {_i} is mace:
        return true
    return false

# ── Helper: remaining cooldown in seconds (0 if not on cooldown) ─────────────
function maceCD(uuid: text) :: number:
    if {mace.cd::%{_uuid}%} is set:
        set {_remaining} to difference between {mace.cd::%{_uuid}%} and now
        if {_remaining} > 0:
            return {_remaining} / 1000
    return 0

# ── On player attacks with mace ───────────────────────────────────────────────
on damage:
    attacker is a player
    victim is a living entity

    set {_p}    to attacker
    set {_uuid} to "%uuid of {_p}%"

    # Only care about mace hits
    if isMaceItem(tool of {_p}) is false:
        stop

    # ── Block attack if on cooldown ──
    set {_cd} to maceCD({_uuid})
    if {_cd} > 0:
        cancel event
        set {_cdRound} to floor({_cd}) + 1
        send action bar "{@c-bad}✗ Mace cooldown: {@c-warn}%{_cdRound}%s{@c-gray} remaining" to {_p}
        stop

    # ── Register this hit ──
    add 1 to {mace.hits::%{_uuid}%}
    set {mace.lastHit::%{_uuid}%} to now

    set {_hits} to {mace.hits::%{_uuid}%}

    # ── Trigger cooldown if hit limit reached ──
    if {_hits} >= {@hit-limit}:
        set {mace.cd::%{_uuid}%} to ({@cooldown-sec} * 1000) milliseconds from now
        delete {mace.hits::%{_uuid}%}
        delete {mace.lastHit::%{_uuid}%}

        send action bar "{@c-bad}⚠ Mace on cooldown! {@c-gray}({@cooldown-sec}s)" to {_p}
        send title "" with subtitle "{@c-bad}⚔ Mace Cooldown {@c-gray}— {@c-warn}{@cooldown-sec}s" to {_p} for 3 seconds with 0.3 seconds fade in and 0.5 seconds fade out
        stop

    # ── Show hit progress ──
    set {_remaining} to {@hit-limit} - {_hits}
    send action bar "{@c-warn}⚔ Mace hits: %{_hits}%{@c-gray}/{@c-bad}{@hit-limit} {@c-gray}(resets in {@reset-sec}s without hitting)" to {_p}

# ── Every-second ticker: action bar updates + counter/cooldown expiry ─────────
every 1 second:
    loop all players:
        set {_uuid} to "%uuid of loop-player%"

        # ── Cooldown active: show shrinking bar ──
        set {_cd} to maceCD({_uuid})
        if {_cd} > 0:
            set {_cdRound} to floor({_cd}) + 1
            send action bar "{@c-bad}⚒ Mace cooldown: {@c-warn}%{_cdRound}%s{@c-gray} remaining" to loop-player

        # ── Clean up expired cooldown variable ──
        else if {mace.cd::%{_uuid}%} is set:
            delete {mace.cd::%{_uuid}%}

        # ── Reset hit counter if idle for reset-sec ──
        if {mace.hits::%{_uuid}%} > 0:
            if {mace.lastHit::%{_uuid}%} is set:
                set {_elapsed} to difference between {mace.lastHit::%{_uuid}%} and now
                # difference returns milliseconds
                if {_elapsed} >= ({@reset-sec} * 1000):
                    delete {mace.hits::%{_uuid}%}
                    delete {mace.lastHit::%{_uuid}%}
                    send action bar "{@c-gray}↺ Mace combo reset." to loop-player

# ── Clean up on player quit ───────────────────────────────────────────────────
on quit:
    set {_uuid} to "%uuid of player%"
    delete {mace.hits::%{_uuid}%}
    delete {mace.lastHit::%{_uuid}%}
    delete {mace.cd::%{_uuid}%}

# ── Admin command to manually clear a player's mace state ────────────────────
command /macereset <player>:
    permission: mace.admin
    permission message: {@prefix} {@c-bad}You don't have permission.
    trigger:
        set {_uuid} to "%uuid of arg-1%"
        delete {mace.hits::%{_uuid}%}
        delete {mace.lastHit::%{_uuid}%}
        delete {mace.cd::%{_uuid}%}
        send "{@prefix} {@c-good}Reset mace state for {@c-warn}%arg-1%{@c-good}." to player
        send "{@prefix} {@c-good}Your mace cooldown was cleared by {@c-warn}%player%{@c-good}." to arg-1