#lines that start with # are comments

#Give command, with material name lookup
# Note that the second variable $qty is optional, and if no value
# is given, 1 is the default. The function player() returns the
# name of this player, and data_values() turns "stone" into 1
#/i $data [$qty=1] = runas('~op', sconcat(/give player() data_values($data) $qty))
###

#Give the player a diamond pickaxe, shovel, and axe
# Note that commands are separated by \ and this creates a macro
#/kit diamond = /give player() data_values('diamond_pickaxe') 1 \
#	/give player() data_values('diamond_spade') 1 \
#	/give player() data_values('diamond_axe') 1
###

#Suppose we have a plugin that broadcasts messages only to moderators, but it doesn't include the user's name
#Also suppose that it is a very long command.
#We can use this command to fix that:
#/mb $ = /mod-broadcast concat('(',player(),')') $

#Lets notify our users to use the new, shorter version
#/mod-broadcast [$] = die(concat(color('red'), 'Use /mb instead of /mod-broadcast'))

adminrunas:/runas $player $cmd [$] = >>>
    runas($player,
        sconcat($cmd $)
    )
<<<

rainbow:/rainbow [$player] = >>>
        @player = player()
        if($player != '', @player = player($player))
        if(import('rainbowarmour.'.@player) != null){
                clear_task(import('rainbowarmour.'.@player))
                export('rainbowarmour.'.@player, null)
                die('No more rainbow :(')
        }
        msg('Rainbow Armour!!!')
        set_pinv(@player, array(103: array(type: 298)))
        set_pinv(@player, array(102: array(type: 299)))
        set_pinv(@player, array(101: array(type: 300)))
        @currentColor = array(0,0)
        @nextColor = array(0)
        @colors = array(
                array(255, 0, 0),  #red
                array(255, 128, 0), #orange
                array(255, 255, 0), #yellow
                array(0, 255, 0),  #green
                array(0, 0, 255),  #blue
                array(127, 0, 255), #indigo
                array(102, 0, 204)  #violet
        )
        @shades = 50 #Number of shades between each colour
        @interval = set_interval(100, closure(
                if(!ponline(@player) || pinv(@player, 103) == null || pinv(@player, 102) == null || pinv(@player, 101) == null){
                    clear_task()
                    clear_task(import('rainbowarmour.'.@player))
                    export('rainbowarmour.'.@player, null)
                    die('No more rainbow :(')
                } else {
                        if(@currentColor[1] >= @shades - 1){
                            if(@currentColor[0] >= array_size(@colors) - 1){
                                @currentColor[0] = 0
                            } else {
                                @currentColor[0] = @currentColor[0] + 1
                            }
                            @currentColor[1] = 0
                        } else {
                            @currentColor[1] = @currentColor[1] + 1
                        }
                        @nextColor[0] = @currentColor[0] + 1
                        if(@nextColor[0] > array_size(@colors) - 1){
                            @nextColor[0] = 0
                        }
                        @reddif = @colors[@nextColor[0]][0] - @colors[@currentColor[0]][0]
                        @greendif = @colors[@nextColor[0]][1] - @colors[@currentColor[0]][1]
                        @bluedif = @colors[@nextColor[0]][2] - @colors[@currentColor[0]][2]
                        @color = array(
                                integer(@colors[@currentColor[0]][0] + (@currentColor[1] * (@reddif / @shades))),
                                integer(@colors[@currentColor[0]][1] + (@currentColor[1] * (@greendif / @shades))),
                                integer(@colors[@currentColor[0]][2] + (@currentColor[1] * (@bluedif / @shades)))
                        )
                        set_armor_color(@player, 103, @color)
                        set_armor_color(@player, 102, @color)
                        set_armor_color(@player, 101, @color)
                }
        ))
        export('rainbowarmour.'.@player, @interval)
<<<

~default:/list [$] = >>>
    die('Just use tab.')
<<<

~default:/fixtowny = >>>
    # How many seconds they have to wait before being able to re-run it
    assign(@cooldown, 600)
    assign(@pinfo, pinfo())

    # Ensure the persistent variable exists. If it doesn't, set it to 0.
    if(is_null(get_value('towny_reload_time')),
        store_value('towny_reload_time',0)
    )

    # Get our persistent value
    assign(@lastreset, get_value('towny_reload_time'))

    # Note that time is in msec, not sec. So we always divide it by 1000.
    if(@lastreset + @cooldown > time() / 1000,
        assign(@timeleft, round(subtract(add(@lastreset, @cooldown), divide(time(),1000))))
        die(concat(color(RED)'[Error]', color(WHITE)'You must wait' @timeleft 'seconds before fixing again!'))
    )

    # Save the current time() (in seconds) in towny_reload_time
    store_value('towny_reload_time', time() / 1000)

    runas('~op', '/ta reload')
    
    broadcast(concat(array_get(@pinfo,4), color(RED)'has fixed towny!'))
<<<