• 欢迎来到Minecraft插件百科!
  • 对百科编辑一脸懵逼?帮助:快速入门带您快速熟悉百科编辑!
  • 因近日遭受攻击,百科现已限制编辑,有意编辑请加入插件百科企鹅群:223812289

ChatControl Pro/WIKI/JavaScript变量

来自Minecraft插件百科
Qsefthuopq讨论 | 贡献2019年1月29日 (二) 12:08的版本 脚本
跳转至: 导航搜索

ChatControl Pro supports dynamic, high performance variables you create! They can be used across the plugin, in for example chat formatter or death messages. Currently, the file javascript.txt in variables folder stores all custom variables.

Those can not only be used to display information, but because you can write your own full code in Javascript to display them, they can also do whatever you would wish for your players/server.

To use Javascript variables, set Variables.Custom_Enabled to true in your settings.yml.

Notice: As the variables need to be recompiled (and scrips re-run) on every new message (to see if the value has changed), adding too many of them might negatively affect server's performance.

Requirements

It is recommended you know a little bit of Java, knowing how to access methods in Bukkit, and have some basic knowledge of Javascript itself, or at least know how to use Google properly and adjust and debug others' Javascript code to your needs.

运算符

定义

开始声明变量. 把变量名放到运算符后.

// 定义{player_name}变量:

define {player_name}


脚本

使用这个运算符来些脚本 (JavaScript)来获取变量.脚本从新一行开始写.

脚本接受以下来自以下的变量:

  • 'player' 获取可用玩家名
  • 'event' 获取可用事件名
// 返回Watch out 的变量示例!对玩家发送消息和 
// 播放音效:
define {sound_and_message}
script:
  // define local pl variable, only available within this script
  var pl = event.getPlayer();
  // define local sound variable
  var sound = Java.type("org.bukkit.Sound").ANVIL_LAND;
  // actually plays sound to the player
  pl.playSound(pl.getLocation(), sound, 1F, 1F);
  // return "Watch out!" message as the placeholder 
  “Watch out!”;


Using variables from PlaceholderAPI in your custom variable

Here is an example on how you can combine other plugins' variables to mix up your own variable depending on another variable's output!

define {test}

script:

   getTagOrPrefix(); // call the function below that returns the tag or prefix


   function getTagOrPrefix() {

       // replace with the tag

       var tag = "{deluxetags_identifier}";

     

       // if the tag is empty, return prefix, else return tag with [] around it.

       return tag == "" ? "{pl_prefix}" : "[" + tag + "]";

   }

Accessing NMS easily

define {ping}

script:

   // acts if as you were inside of CraftPlayer class, so NMS is directly available

   player.getHandle().ping  


Let's analyze this code really quickly:

  1. The define operator makes up and registers the variable {ping}, so you can use it in the plugin.
  2. The script: begins the declaration of the Javascript code itself. Write your code below this line. It can have unlimited size - ChatControl will stop reading it when it reaches the end of the file or another define operator.
  3. The code itself - contains predefined "player" variable you can use (if present) to get the player instance. This instance represents CraftPlayer in Bukkit in Java, so you can easily access NMS internals without even specifying those pesky 1_12_R0 numbers every time!

Javascript/Java Coding Guide

There is an extensive guide on how to code Javascript for Java on these locations:

  • Unofficial Nashorn Tutorial
  • Official Nashorn Tutorial