Custom Enchants

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Tewahh

Member
Aug 18, 2023
27
1
3
Hello, now I know I've made a previous post about custom enchants, but that was when I knew close to nothing about functions and skript, I've learned more over the 6 ish months, but when I was making this new skript, I was thinking about using functions. which is what I did, Im using an addon called "SkEnchants" for my enchant handler, but it still looks clunky. I was wondering, what all can I do to improve this skript, as well as every other skript?

code_language.skript:
function decode(t: text) :: integer:
    if {_t} parsed as integer > 10:
        return {_t} parsed as integer
    if {_t} = "X":
        return 10
    else if {_t} = "IX":
        return 9
    else if {_t} = "VIII":
        return 8
    else if {_t} = "VII":
        return 7
    else if {_t} = "VI":
        return 6
    else if {_t} = "V":
        return 5
    else if {_t} = "IV":
        return 4
    else if {_t} = "III":
        return 3
    else if {_t} = "II":
        return 2
    else if {_t} = "I":
        return 1

function toRoman(n: integer) :: string:
    if {_n} > 10:
        return "%{_n}%"
        stop
    set {_roman::*} to "X", "IX", "V", "IV" and "I"
    loop 10, 9, 5, 4 and 1:
        add 1 to {_index}
        while {_n} >= loop-value:
            add 1 to {_next}
            remove loop-value from {_n}
            set {_result::%{_next}%} to {_roman::%{_index}%}
    return (join {_result::*} by "") ? "0"

function hasEnchant(i: item, t: string) :: boolean:
    set {_lore::*} to lore of {_i} split at "||"
    loop {_lore::*}:
        if loop-value contains "%{_t}%":
            return true

function getEnchantLevel(i: item, t: string) :: integer:
    set {_lore::*} to lore of {_i} split at "||"
    set {_n} to 0
    loop {_lore::*}:
        add 1 to {_n}
        if loop-value contains "%{_t}%":
            set {_enchant::*} to loop-value split at "%{_t}% "
            set {_enchant} to decode({_enchant::2})
    return {_enchant}

function applyEnchant(i: item, t: string, l: integer, r: boolean):
    set {_lore::*} to lore of {_i} split at "||"
    set {_n} to 0
    loop {_lore::*}:
        add 1 to {_n}
        if loop-value contains "%{_t}%":
            if {_r} = true:
                set line {_n} of lore of {_i} to "%{_t}% %toRoman({_l})%"
            else:
                set line {_n} of lore of {_i} to "%{_t}% %{_l}%"

function removeEnchant(i: item, t: string):
    set {_lore::*} to lore of {_i} split at "||"
    set {_n} to 0
    loop {_lore::*}:
        add 1 to {_n}
        if loop-value contains "%{_t}%":
            delete {_lore::%{_n}%}
            set {_lore} to (join {_lore::*} by "||")
            set lore of {_i} to {_lore}

command /customEnchant <text> <text> <integer>:
    # /customEnchant [enchant|removeEnchant] [Enchant Name] [Enchant Level]
    trigger:
        if arg-1 is "enchant":
            applyEnchant(player's held item, "%arg-2%", arg-3, true)
        if arg-1 is "removeEnchant":
            removeEnchant(player's held item, "%arg-2%")

on tab complete:
    if event-string is "/customEnchant":
        set tab completions for position 1 to "enchant" and "removeEnchant"
        set tab completions for position 2 to {customEnchants::*}
        set tab completions for position 3 to "1", "2", "3", "4", "5", "6", "7", "8", "9" and "10"

on damage:
    if hasEnchant(attacker's held item, "&d&lLightning") is true:
        if difference between now and {cooldown::lightningEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            apply slowness 5 to victim for 5 seconds
            loop 2 times:
                strike lightning effect at victim
                wait 1 second
            set {cooldown::lightningEnchant::lastUsed::%attacker's uuid%} to now

function doubleStrikeProc(attacker: player, victim: player, chance: integer):
    chance of {_chance}%:
        set {_damage} to damage
        make {_attacker} damage {_victim} by {_damage} * 2
        set {cooldown::doubleStrikeEnchant::lastUsed::%{_attacker}'s uuid%} to now

function effectProc(chance: integer, effect: text, level: integer, attacker: player, victim: player, time: text):
    chance of {_chance}%:
        apply ({_effect} parsed as potion effect type) ({_level} parsed as number) to {_victim} for ({_time} parsed as timespan)
        set {cooldown::"%{_effect}%"Enchant::lastUsed::%{_attacker}'s uuid%} to now

on damage:
    if hasEnchant(attacker's held item, "&7&lWither") is true:
        if difference between now and {cooldown::witherEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&7&lWither") = 1:
                effectProc(1, "wither", 1, attacker, victim, "3 seconds")
            
            else if getEnchantLevel(attacker's held item, "&7&lWither") = 2:
                effectProc(3, "wither", 2, attacker, victim, "6 seconds")
            
            else if getEnchantLevel(attacker's held item, "&7&lWither") = 3:
                effectProc(5, "wither", 3, attacker, victim, "9 seconds")
            
            else if getEnchantLevel(attacker's held item, "&7&lWither") = 4:
                effectProc(7, "wither", 4, attacker, victim, "12 seconds")
            
            else if getEnchantLevel(attacker's held item, "&7&lWither") = 5:
                effectProc(9, "wither", 5, attacker, victim, "15 seconds")
    else if hasEnchant(attacker's held item, "&7&lNausea") is true:
        if difference between now and {cooldown::nauseaEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&7&lNausea") = 1:
                effectProc(1, "nausea", 1, attacker, victim, "3 seconds")
            
            else if getEnchantLevel(attacker's held item, "&7&lNausea") = 2:
                effectProc(3, "nausea", 2, attacker, victim, "6 seconds")
            
            else if getEnchantLevel(attacker's held item, "&7&lNausea") = 3:
                effectProc(5, "nausea", 3, attacker, victim, "9 seconds")
            
            else if getEnchantLevel(attacker's held item, "&7&lNausea") = 4:
                effectProc(7, "nausea", 4, attacker, victim, "12 seconds")
            
            else if getEnchantLevel(attacker's held item, "&7&lNausea") = 5:
                effectProc(9, "nausea", 5, attacker, victim, "15 seconds")
    
    else if hasEnchant(attacker's held item, "&7&lPoison") is true:
        if difference between now and {cooldown::poisonEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&8&lPoison") = 1:
                effectProc(6, "poison", 1, attacker, victim, "10 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lPoison") = 2:
                effectProc(11, "poison", 2, attacker, victim, "8 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lPoison") = 3:
                effectProc(16, "poison", 3, attacker, victim, "6 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lPoison") = 4:
                effectProc(21, "poison", 4, attacker, victim, "4 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lPoison") = 5:
                effectProc(26, "poison", 5, attacker, victim, "2 seconds")
    
    else if hasEnchant(attacker's held item, "&7&lBlindness") is true:
        if difference between now and {cooldown::blindnessEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&8&lBlindness") = 1:
                effectProc(6, "blindness", 1, attacker, victim, "10 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lBlindness") = 2:
                effectProc(11, "blindness", 2, attacker, victim, "8 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lBlindness") = 3:
                effectProc(16, "blindness", 3, attacker, victim, "6 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lBlindness") = 4:
                effectProc(21, "blindness", 4, attacker, victim, "4 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lBlindness") = 5:
                effectProc(26, "blindness", 5, attacker, victim, "2 seconds")
    
    else if hasEnchant(attacker's held item, "&7&lSlowness") is true:
        if difference between now and {cooldown::slownessEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&8&lSlowness") = 1:
                effectProc(6, "slowness", 1, attacker, victim, "10 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lSlowness") = 2:
                effectProc(11, "slowness", 2, attacker, victim, "8 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lSlowness") = 3:
                effectProc(16, "slowness", 3, attacker, victim, "6 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lSlowness") = 4:
                effectProc(21, "slowness", 4, attacker, victim, "4 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lSlowness") = 5:
                effectProc(26, "slowness", 5, attacker, victim, "2 seconds")
    
    else if hasEnchant(attacker's held item, "&7&lHunger") is true:
        if difference between now and {cooldown::hungerEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&8&lHunger") = 1:
                effectProc(6, "hunger", 1, attacker, victim, "10 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lHunger") = 2:
                effectProc(11, "hunger", 2, attacker, victim, "8 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lHunger") = 3:
                effectProc(16, "hunger", 3, attacker, victim, "6 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lHunger") = 4:
                effectProc(21, "hunger", 4, attacker, victim, "4 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lHunger") = 5:
                effectProc(26, "hunger", 5, attacker, victim, "2 seconds")
    
    else if hasEnchant(attacker's held item, "&7&lDarkness") is true:
        if difference between now and {cooldown::darknessEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&8&lDarkness") = 1:
                effectProc(6, "darkness", 1, attacker, victim, "10 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lDarkness") = 2:
                effectProc(11, "darkness", 2, attacker, victim, "8 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lDarkness") = 3:
                effectProc(16, "darkness", 3, attacker, victim, "6 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lDarkness") = 4:
                effectProc(21, "darkness", 4, attacker, victim, "4 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lDarkness") = 5:
                effectProc(26, "darkness", 5, attacker, victim, "2 seconds")
    
    else if hasEnchant(attacker's held item, "&7&lWeakness") is true:
        if difference between now and {cooldown::weaknessEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&8&lWeakness") = 1:
                effectProc(6, "weakness", 1, attacker, victim, "10 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lWeakness") = 2:
                effectProc(11, "weakness", 2, attacker, victim, "8 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lWeakness") = 3:
                effectProc(16, "weakness", 3, attacker, victim, "6 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lWeakness") = 4:
                effectProc(21, "weakness", 4, attacker, victim, "4 seconds")
            
            else if getEnchantLevel(attacker's held item, "&8&lWeakness") = 5:
                effectProc(26, "weakness", 5, attacker, victim, "2 seconds")


# make a enchant disabler function


on damage:
    if hasEnchant(attacker's held item, "&6&lDouble Strike") is true:
        if difference between now and {cooldown::doubleStrikeEnchant::lastUsed::%attacker's uuid%} is less than 15 seconds:
            send " " to console
        else:
            if getEnchantLevel(attacker's held item, "&6&lDouble Strike") = 1:
                doubleStrikeProc(attacker, victim, 1)
            if getEnchantLevel(attacker's held item, "&6&lDouble Strike") = 2:
                doubleStrikeProc(attacker, victim, 3)
            if getEnchantLevel(attacker's held item, "&6&lDouble Strike") = 3:
                doubleStrikeProc(attacker, victim, 5)

every second:
    loop all players:
        if hasEnchant(loop-player's boots, "&7Speed") is true:
            if getEnchantLevel(loop-player's boots, "&7Speed") = 1:
                apply swiftness 1 to loop-player for 2.01 seconds
            else if getEnchantLevel(loop-player's boots, "&7Speed") = 2:
                apply swiftness 2 to loop-player for 2.01 seconds
        if hasEnchant(loop-player's boots, "&7Bounce") is true:
            if getEnchantLevel(loop-player's boots, "&7Bounce") = 1:
                apply jump boost 2 to loop-player for 2.01 seconds