Skip to content

Instantly share code, notes, and snippets.

@Snow-Pyon
Last active May 10, 2018 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Snow-Pyon/77c36403f9d40ea4b66d57df15d2ce23 to your computer and use it in GitHub Desktop.
Save Snow-Pyon/77c36403f9d40ea4b66d57df15d2ce23 to your computer and use it in GitHub Desktop.
Snow-Pyon's script snippets (function version)

Chat Component Converter

Converts a chat component json to a human-readable text.

Code

function asSkriptColor(color: text) :: text:

  if {_color} contains "_":

    replace all "_" with " " in {_color}
    return {_color}

  else if {_color} isn't "black", "white", "yellow" or "gold":

    return "light %{_color}%"

  else:

    return {_color}
    

function asReadableText(json: jsonobject) :: text:

  put json {_json} in listvar {_json::*}

  loop tree of {_json::extra::*}:

    if branch contains "color":

      set {_color} to asSkriptColor("%loop-value%")
      set {_color} to "<%{_color}%>"

    else if branch contains "text":

      if {_color} isn't set:
        set {_color} to ""
          
      add "%{_color}%%loop-value%" to {_full-text::*}


  return join {_full-text::*} by ""

Usage

on skript load:

  add "<red><bold>Hey! <grey>you aren not permitted to enter this area." to {deniedMessages::*} #WorldGuard message
  add "You created the faction " to {deniedMessages::*} #Factions message

on packet event play_server_chat:

  set {_last-message} to asReadableText(chatcomponent pjson 0 of event-packet)

  loop {deniedMessages::*}:
    if {_last-message} contains loop-value:
      replace all loop-value with "..." in {_last-message}
      cancel event
  set chatcomponent pjson 0 of event-packet to asComponent({_last-message}) # Uses the message to component function

Message to Component

Used for converting a normal text to a chat component (useful when dealing with packets)

Code

function asComponent(messages: strings) :: jsonobjects:

  loop {_messages::*}:
    set {_chat-component::extra::1::text} to loop-value
    add (json of listvar {_chat-component::*}) to {_output::*}
    
  return {_output::*}

Usage

command /test:
  trigger:
   
    set {_packet-container} to new play_server_chat packet
    set chatcomponent pjson 0 of {_packet-container} to asComponent("<green>test")
    send player packet {_packet-container}

Send Actionbar

Used for sending an actionbar to one or more players.

Code

function sendActionbar(message: string, receivers: players, stay-time: timespan = 1 tick):

  set {_packet-container} to new play_server_chat
  set chatcomponent pjson 0 of {_packet-container} to asComponent({_message})
  set "ChatType" penum 0 of {_packet-container} to "GAME_INFO"
    
   loop {_stay-time} times:
     send packet {_packet-container} to expressions-2
     wait 1 tick

Usage

command /health:
  trigger:
  
    send actionbar "Your health is&7: &c%player's scaled health%" to player for 1 second

Show Animation

Make one or more entities show the given animation.

Available animations:

Animation Index
Swing Main Hand 0
Take Damage 1
Leave Bed 2
Swing Off Hand 3
Critical 4
Magic Critical 5

Code

function showAnimation(sources: entities, animation: integer, viewers: players):

  {_animation} is between 0 and 5
  set {_packet-container} to new play_server_animation packet
  set int pnum 1 of {_packet-container} to {_animation}
    
  loop {_sources::*}:
    
    set loop-value's world pentity 0 of {_packet-container} to loop-value  
    send packet {_packet-container} to {_viewers::*}

Usage

command /swing:
  trigger:

    showAnimation(player, 0, all players)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment