// Hand-written examples for Docs entries that no example elsewhere in the documentation covers.
// An entry starts with '@ ' followed by the entry's syntax exactly as the Docs page shows it.
// Every example here is compiled when the documentation is checked, so it has to be real code.

@ %server.max_players%
send "&7%server.online_players% of %server.max_players% players online"

@ %server.time%
set {%player%.last_seen} to server.time

@ %event.world%, %event.x%, %event.y%, %event.z%
on block break:
    log "%player.name% broke a block at %event.x%, %event.y%, %event.z% in %event.world%"

@ %discord.user%
on discord command "/hello":
    reply "Hello, %discord.user%!"

@ %discord.channel%
on discord command "/where":
    reply "You are in #%discord.channel%"

@ <amount> in front
give 16 bread to player

@ ( )
set _total to (_price + _tax) * _amount

@ random integer between A and B
set _roll to random integer between 1 and 6

@ random number between A and B
set _chance to random number between 0 and 1

@ > < >= <=
if {%player%.coins} >= 100:
    send "&aYou can afford it."

@ is a number / is not a number
if arg.1 is not a number:
    send "&cUsage: /pay <amount>"
    stop

@ {_list.<n>}
set _parts to split "Coins:500" by ":"
send "You have %{_parts.2}% coins"

@ remove <value> from <variable>
remove player.name from {winners}

@ join <list> with <separator>
send "Online: " + join server.players with ", "

@ trimmed <text>
set _name to trimmed args

@ replace <a> with <b> in <text>
set _pretty to replace "_" with " " in "diamond_sword"

@ last <n> characters of <text>
set _ending to last 3 characters of player.name

@ characters <a> to <b> of <text>
set _middle to characters 2 to 4 of "Hello"

@ length of <text>
if length of args > 50:
    send "&cThat is too long."

@ rounded <number>
send "TPS: " + rounded server.tps

@ append <text> to file at <path>
append "\n%player.name% joined" to file at "plugins/HyperScript/log.txt"

@ delete file at <path>
delete file at "plugins/HyperScript/notes/old.txt"

@ execute player command <text>
execute player command "spawn"

@ give <who> <item> or give <item> to <who>
give player 3 diamond
give 16 bread to player

@ attacker
on entity damage:
    if attacker is set and victim is "villager":
        cancel event

@ %event.click_type%
on inventory click:
    if event.click_type is "SHIFT_LEFT":
        cancel event

@ %event.recipients%
on player chat:
    log "%player.name% wrote to: %event.recipients%"

@ %event.location%
on block break:
    spawn particle "flame" at event's location

@ permission: value
command /fly:
    permission: "server.fly"
    trigger:
        execute console command "fly %player.name%"

@ permission <who> remove <node>
permission player remove "server.fly"

@ delete line <n> of <who>'s scoreboard
delete line 3 of player's scoreboard

@ headers <text>, <text>, ...
async:
    send post request to "https://api.example.com/v1/events" with body "{\"type\":\"join\"}" headers "Authorization: Bearer abc123"

@ hide player count
on server ping:
    hide player count

@ set server icon to <path>
on server ping:
    set server icon to "plugins/HyperScript/icon.png"

@ set quit message to <text>
on player quit:
    set quit message to "&c- &f%player.name%"

@ hide join message / hide quit message
on player join:
    hide join message

@ set chat message to <text>
on player chat:
    if message contains "badword":
        set chat message to "(removed)"

// ---- Entries where the automatic match picked a line that shows something else ----

@ /
set _average to {%player%.total} / {%player%.games}

@ %
if {%player%.blocks} % 50 == 0:
    send "&bAnother 50 blocks mined!"

@ -
set _left to {%player%.coins} - 50

@ *
set _double to {%player%.coins} * 2

@ and, or, not
if {%player%.coins} >= 50 and not {shop_closed}:
    send "&aThe shop is open."

@ is / is not
if event.cause is "FALL":
    cancel event

@ <list> + <value>
set _everyone to server.players + "Console"

@ <text> + <text>
set _greeting to "Hello, " + player.name

@ send <text> to <player name>
send "&e%player.name% says hi!" to arg.1

@ wait <number> seconds
set _delay to 5
wait _delay seconds

@ file at <path> exists
if file at "plugins/HyperScript/notes.txt" exists:
    send read file at "plugins/HyperScript/notes.txt"

@ damage
on entity damage:
    if damage >= 10:
        send "&cThat hit hard!" to victim

@ block
on block break:
    if block is "diamond_ore":
        broadcast "&b%player.name% found diamonds!"

@ slot
on inventory click:
    if slot is 1:
        send "&7You clicked the first slot."

@ item
on right click:
    if item is "compass":
        send "&bYou are in %player.world%"

@ permission <who> add <node> / permission <who> remove <node>
permission player add "server.fly"
permission player remove "server.fly"

@ permission <who> add <node>
permission player add "server.fly"

@ sorted <list>
set _names to sorted server.players

@ reversed <list>
set _newest_first to reversed {winners}

@ x of <location>, y of <location>, z of <location>
set _height to y of player's location

@ world of <location>
send "Your home is in " + world of {%player%.home}

@ set y of <variable> to <number>
set {_loc} to player's location
add 10 to y of {_loc}
teleport player to {_loc}

@ block at <position>
if block at player's location is "water":
    send "&bYou are standing in water."

@ send <text> to player
send "&aSaved." to player

@ send <text> to all players
send "&6The event starts in one minute!" to all players

@ remove <item> from <who>
if player has 5 diamond:
    remove 5 diamond from player

@ has <item>
if player has 5 diamond:
    send "&aYou can afford the upgrade."

@ does not have <item>
if player does not have 1 iron pickaxe:
    send "&cBring a pickaxe."

@ parse <text> as number
set _amount to parse arg.1 as number
if _amount is not set:
    send "&cThat is not a number."

@ parse <text> as integer
set _whole to parse "7.9" as integer

@ parse <value> as text
set _label to parse 52 as text

@ return [<value>]
function double(_n):
    return _n * 2

@ function <name>(<locals>):
function reward(_who, _amount):
    add _amount to {%_who%.coins}
    return {%_who%.coins}

@ local function <name>(<locals>):
local function prefix():
    return "&7[&bShop&7] "

@ command
on player command:
    if command is "plugins":
        cancel event

@ amount
on item pickup:
    send "&7Picked up %event.amount% %event.item%"

@ %event.from%
on world change:
    send "&7You left %event.from% for %player.world%"

@ %hyperscript_<name>%
set {event_active} to true
# Any plugin that uses PlaceholderAPI can now show it:
# "Event running: %hyperscript_event_active%"

@ %hyperscript_player_<name>%
set {%player%.coins} to 100
# Any plugin that uses PlaceholderAPI can now show it to that player:
# "Coins: %hyperscript_player_coins%"

@ min(a, b, ...) / max(a, b, ...)
set _level to max(1, {%player%.level})
set _cheapest to min([30, 12, 45])

@ abs(n)
set _apart to abs(player.y - 64)

@ round(n)
set _whole to round(2.5)

@ floor(n) / ceil(n)
set _pages to ceil(size of {warps} / 9)

@ rounded up <n> / rounded down <n>
set _minutes to rounded up (_seconds / 60)

@ sqrt(n)
set _side to sqrt(25)

@ duration(seconds)
set _left to duration({%player%.ready} - server.time)
send "&cWait %_left%."

@ duration(<seconds>)
send "&7Uptime: " + duration(125)

@ distance between <a> and <b>
if distance between player's location and {spawn} > 100:
    send "&7You are far from spawn."

@ is banned / is not banned
if arg.1 is banned:
    send "&c%arg.1% is banned."

@ items <a> to <b> of <list>
set _top to items 1 to 5 of reversed sorted {scores}

@ index of <value> in <list>
set _place to index of player.name in {queue}

@ random item of <list>
send random item of ["Hello!", "Hi there!", "Welcome!"]

@ keys of {<table>}
for each _name in keys of {warp}:
    send "&7 - &b%_name%"

@ break / continue
for each _name in {queue}:
    if _name is "Steve":
        continue
    send "&7Next: %_name%"
    break

@ send <text> with click command <text>
send "&a[Go home]" with click command "/home"

@ send <text> with hover <text>
send "&e[?]" with hover "&7Hover text can explain things."

@ with click command <text>
send "&a[Accept]" with click command "/accept"

@ with click suggestion <text>
send "&7[Reply]" with click suggestion "/msg Alex "

@ with click url <text>
send "&b[Website]" with click url "https://example.com"

@ with hover <text>
send "&6Rules" with hover "&7Be kind.\n&7No griefing."

@ set helmet of <who> to <item>
set helmet of player to 1 diamond helmet enchanted with protection 4

@ clear inventory of <who>
clear inventory of player

@ helmet, chestplate, leggings, boots
set chestplate of player to 1 iron chestplate
delete boots of player

@ tool
if tool of player is not set:
    send "&7Your hand is empty."

@ offhand
set offhand of player to 1 shield

@ inventory slot <n>
set inventory slot 1 of player to 1 compass named "&bMenu"

@ kick, ban, unban
kick arg.1 because "&cPlease calm down."
ban arg.1 for 7 days because "&cGriefing"

@ kick <who> [because <text>]
kick arg.1 because "&cPlease calm down."

@ ban <who> [for <duration>] [because <text>]
ban arg.1 for 7 days because "&cGriefing"

@ unban <who>
unban arg.1

@ <who> is banned / is not banned
if arg.1 is not banned:
    send "&7%arg.1% is not banned."

@ refuse login [because <text>]
on player login:
    if {maintenance}:
        refuse login because "&eBack soon!"

@ add <who> to team <name>
add player to team "red"

@ remove <who> from team
remove player from team

@ remove <who> from team <name>
remove player from team "red"

@ set color of team <name> to <colour>
set color of team "red" to red

@ set prefix of team <name> to <text> / set suffix of team <name> to <text>
set prefix of team "red" to "&c[Red] "

@ set friendly fire of team <name> to <true or false>
set friendly fire of team "red" to false

@ delete team <name>
delete team "red"

@ team of <who>
if team of player is "red":
    send "&cGo red!"

@ players of team <name>
for each _mate in players of team "red":
    send "&cRed team, assemble!" to _mate

@ <on|off>, <red|blue|green>
command /event <on|off>:
    trigger:
        send "&7You chose %arg.1%."

@ <player> or <target>
command /greet <player>:
    trigger:
        send "&aHello from %player.name%!" to arg.1

@ %event.name%
on player login:
    log "%event.name% is connecting from %event.address%"

@ %event.uuid%
on player login:
    log "%event.name% has the id %event.uuid%"


@ %event.address%
on player login:
    log "%event.name% is connecting from %event.address%"

@ heal <who> / feed <who>
heal player
feed player

@ health of <who>
if health of player < 6:
    send "&cYou are badly hurt!"

@ max health of <who>
set max health of player to 40

@ food of <who>
set food of player to 20

@ level of <who>
add 5 to level of player

@ gamemode of <who>
set gamemode of player to creative

@ flight of <who>
set flight of player to true

@ apply <effect> [<level>] to <who> [for <duration>]
apply speed 2 to player for 30 seconds

@ clear effects of <who> / clear <effect> effect of <who>
clear effects of player
clear speed effect of player

@ ignite <who> [for <duration>] / extinguish <who>
ignite arg.1 for 3 seconds
extinguish player

@ freeze <who> / unfreeze <who>
freeze arg.1
send "&cYou have been frozen." to arg.1

@ vanish <who> / unvanish <who>
vanish player
send "&7Nobody can see you now."

@ <who> has effect <effect> / does not have effect <effect>
if player has effect speed:
    send "&bYou are fast."

@ has effect / does not have effect
if player does not have effect night vision:
    apply night vision to player for 5 minutes

@ <who> is frozen / is vanished / is flying
if arg.1 is frozen:
    send "&7%arg.1% is frozen."

@ is frozen / is vanished / is flying
if player is flying:
    send "&7Enjoy the view."

@ is tagged / is not tagged
if tool of player is tagged "wand":
    send "&dThe wand hums."

@ heal, feed, apply <effect>, freeze, vanish
heal player
apply regeneration 2 to player for 10 seconds

@ top <n> players by {<name>}
set _place to 0
for each _name in top 10 players by {coins}:
    add 1 to _place
    send "&e#%_place% &f%_name%"

@ rank of <who> by {<name>}
set _mine to rank of player by {coins}
send "&7You are #%_mine%"

@ gui page
open gui with title "&8Menu" with 1 rows for player:
    slot 5 is "paper" named "&fPage %gui page%"

@ page <n> of <list> with <k> per page
set _shown to page 2 of {names} with 9 per page

@ pages of <list> with <k> per page
set _count to pages of {names} with 9 per page

@ next page / previous page
open gui with title "&8Menu" with 1 rows for player:
    slot 1 is "arrow" named "&ePrevious":
        previous page
    slot 9 is "arrow" named "&eNext":
        next page

@ set gui page to <n>
open gui with title "&8Menu" with 1 rows for player:
    slot 5 is "compass" named "&eBack to the first page":
        set gui page to 1

@ refresh gui
open gui with title "&8Clicker" with 1 rows for player:
    slot 5 is "gold_ingot" named "&6Clicks: %{%player%.clicks}%":
        add 1 to {%player%.clicks}
        refresh gui

@ every day at <hh:mm>:
every day at 18:00:
    broadcast "&6The evening event starts now!"

@ every monday at <hh:mm>:
every monday at 00:00:
    delete {weekly_winner}

@ %event.region%
on region enter:
    send action bar "&7Entering &f%event.region%" to player

@ %event.lines%
on sign change:
    log "%player.name% wrote: %event.lines%"

@ %event.clicked%
on entity click:
    send "&7You clicked &f%event.clicked%"

@ %event.level%, %event.old_level%
on level up:
    send "&aLevel %event.old_level% -> %event.level%"

@ starts with / ends with
on player chat:
    if message starts with "!":
        cancel event
        send "&7Commands start with / here."

@ matches
if not (arg.1 matches "[a-zA-Z0-9_]{3,16}"):
    send "&cThat is not a valid name."

@ chance of <n>%
on block break:
    if chance of 5%:
        give 1 diamond to player
        send "&bLucky find!"

@ random player
set _winner to random player
broadcast "&6%_winner% wins the draw!"

@ shuffled <list>
set _order to shuffled server.players

@ unique <list>
set _once to unique ["a", "b", "a"]

@ sum of <list> / average of <list>
set _total to sum of [10, 20, 30]
set _mean to average of [10, 20, 30]

@ players within <n> blocks of <location>
for each _near in players within 10 blocks of player:
    send "&7%player.name% waves at you." to _near

@ <text> parsed as number
set _amount to arg.1 parsed as number
if _amount is not set:
    send "&cThat is not a number."

@ now formatted as <pattern>
send "&7It is " + now formatted as "HH:mm"

@ %server.date%
log "Backup finished on %server.date% at %server.clock%"

@ %server.clock%
send "&7Server time: &f%server.clock%"

@ %server.weekday%
if server.weekday is "saturday":
    send "&6Double coins today!"

@ %server.hour%, %server.minute%
if server.hour >= 22:
    send "&7It is late. Good night!"

@ options:
options:
    prefix: "&8[&6Shop&8] "
command /shop:
    trigger:
        send "{@prefix}&7Welcome."

@ command /name:
command /hello:
    trigger:
        send "&aHello!"

@ on <event>:
on player join:
    send "&aWelcome!"

@ on load:
on load:
    if {motd} is not set:
        set {motd} to "&aWelcome!"

@ every <time>:
every 5 minutes:
    broadcast "&7Remember to vote!"

@ function name():
function greet():
    send "&aHello!"

@ # Requires: 1.5.9
# Requires: 1.5.9
on player join:
    send "&aWelcome!"

@ # Tags: chat, welcome
# Tags: chat, welcome
on player join:
    send "&aWelcome!"

