JavaScript Expansion Scripts

Apr 2, 2018
JavaScript Expansion Scripts
  • PlaceholderAPI
    JavaScript Expansion Scripts
    The JavaScript Expansion for PlaceholderAPI allows for expansions that wouldn't really need an entire expansion otherwise. For example, a random number or letter generator, or an expansion that sends out a random motivational message. On this page, you can find some scripts made by the community.

    How to Use
    1. Install the JavaScript expansion using /papi ecloud download JavaScript
    2. Add the downloaded script file to /plugins/PlaceholderAPI/javascripts/ folder.
    3. Copy the config file code to /plugins/PlaceholderAPI/javascript_placeholders.yml

    How to Contribute

    1. Make a javascript expansion!
    2. Edit this page and add your expansion using this template.
    3. (Optional) Include a download link and/or source code link.



    Random Letter(top)

    Author: NathanG
    Description: Prints a random letter from A to Z.
    Usage: %javascript_randomletter%

    Code (Javascript):

    function randomLetter() {
       var start = "A".charCodeAt(0);
       var random = Math.random() * 26;

       return String.fromCharCode(start + Math.floor(random));
    }

    randomLetter();
     
    Code (YAML):

    randomletter
    :
        expression
    : 'file: random_letter.js'
        type
    : 'string'
     

    Source & Download (Right click > Save Link As...)

    Random Integer Between(top)

    Author: NathanG
    Description: Generates a random integer between two given integers.
    Usage: %javascript_randomintbetween_<min>,<max>%
    Example: %javascript_randomintbetween_10,20% produces a random integer between 10 and 20.

    Code (Javascript):

    var min = 1;
    var max = 25;

    function randomInteger() {
       if (args.length == 2) {
           min = args[0];
           max = args[1];
       }

       var random = Math.random() * (max - min);
           random += min;

       return Math.floor(random);
    }

    randomInteger();
     
    Code (YAML):

    randomintbetween
    :
       expression
    : 'file: random_integer_between.js'
       type
    : 'string'
     

    Source & Download (Right click > Save Link As...)

    Data(top)

    Author: clip
    Description: placeholder that allows you to get and set custom data via the placeholder arguments specified when the placeholder is called.
    Usage:
    • %javascript_data_get,<path>% - attempts to get a string value from the path specified, empty string if no value found..
    • %javascript_data_getint,<path>% - attempts to get an integer from the path specified, 0 if no value found.
    • %javascript_data_set,<path>,<value>% - sets the value for the path specified.
    • %javascript_data_add,<path>,<amount>% - attempts to add to the value at the path specified
    • %javascript_data_subtract,<path>,<amount>% - attempts to subtract from the value at the path specified
    You will need to insert the "set", "add", or "subtract" placeholder in the plugin function where you want to track the data. A quick example of functionality for this placeholder would be a "1 time use" item in a GUI plugin that supports PlaceholderAPI:
    Code (YAML):
    menu_title: 'One time use menu'
    open_command
    : onetimeuse
    size
    : 9
    items
    :
      'denied_item'
    :
        slot
    : 0
        priority
    : 2
        material
    : BARRIER
        display_name
    : '&cYou already clicked the item once :('
      'allowed_item'
    :
        slot
    : 0
        priority
    : 1
        view_requirement
    :
          requirements
    :
            check_if_can_use
    :
              type
    : string equals
              input
    : '%javascript_data_get,{player_uuid}.has_used%'
             # the get argument will return an empty string if the value has never been set so we can just check for that
              output
    : ''
        material
    : DIAMOND
        # we need to make sure we set the data value that way next time it is checked, it will tell us if the give command has been ran
        # Since the placeholder to set data returns nothing, we can add it anywhere to set the data. We will just drop it in the command
        left_click_commands
    :
       - '[console] give %player_name% diamond 1 %javascript_data_set,{player_uuid}.has_used,true%'
        - '[close]'

    Code (Javascript):

    function set(path, data) {
        Data.set(path, data);
        return "";
    }
    function add(path, amount) {
        if (isNaN(amount)) {
            return "";
        }
        var amt = Data.exists(path) ? Data.get(path) : 0;
        if (isNaN(amt)) {
            amt = 0;
        }
        amt = parseInt(amt) + parseInt(amount);
        Data.set(path, amt.toFixed());
        return "";
    }
    function subtract(path, amount) {
        if (isNaN(amount)) {
            return "";
        }
        var amt = Data.exists(path) ? parseInt(Data.get(path)) : 0;
        if (isNaN(amt)) {
            amt = 0;
        }
        amt = parseInt(amt) - parseInt(amount);
        Data.set(path, amt.toFixed());
        return "";
    }
    function get(path)  {
        return Data.exists(path) ? Data.get(path) : "";
    }
    function getInt(path) {
        if (Data.exists(path)) {
            var amt = Data.get(path);
            if (isNaN(amt)) {
                return 0;
            } else {
                return parseInt(amt).toFixed();
            }
        }
        return 0;
    }
    function getUsage(firstArg) {
        switch(firstArg) {
        case "get":
            return "get,<path>";
        case "getint":
            return "getint,<path>";
        case "add":
            return "add,<path>,<amount>";
        case "subtract":
            return "subtract,<path>,<amount>";
        case "set":
            return "set,<path>,<value>";
        default:
            return "first argument must be get, getint, set, add, subtract";
        }
    }
    function runPlaceholder() {
        if (args.length == 0) {
            return getUsage("no args");
        }
        else if (args.length == 1) {
            return getUsage(args[0]);
        }
     
        if (args.length == 2) {
            if (args[0].equals("get")) {
                return get(args[1]);
            }
            else if (args[0].equals("getint")) {
                return getInt(args[1]);
            }
        }
        else if (args.length == 3) {
            if (args[0].equals("set")) {
                return set(args[1], args[2]);
            }
            else if (args[0].equals("add")) {
                return add(args[1], args[2]);
            }
            else if (args[0].equals("subtract")) {
                return subtract(args[1], args[2]);
            }
        }
        return getUsage(args[0]);
    }
    runPlaceholder();
     
    Code (YAML):

    data
    :
        file
    : 'data.js'
     

    Has Permission(top)

    Author: aBooDyy
    Description: Check if player have "permission.test" permission and send a custom message. it's very useful and you can use this method with a lot of things, it's like true, false result.
    Usage: %javascript_has_permission%
    NOTE: this method works if you haven't changed the true boolean option in PlaceholderAPI config ('true': 'yes') if you changed it and don't want to use the default one change "yes" in "(haspermission === "yes")" to the thing you set in the true boolean option.


    Code (Javascript):

    // create a variable and name it wantever you want like this
    // and use the placeholder you want, i'm using this one
    var haspermission = "%player_has_permission_permission.test%";

    // create a function with the name you want
    function permission() {

    // if the haspermission variable that we created before return yes (true boolean)
    // the js placeholder will return what we set in the return down
       if (haspermission === "yes") {
           return "&aYou have the Test permission!";
       }

    // if the haspermission varibale wasnt true it will return what we set down
       else {
           return "&cYou don't have the Test permission!";
       }
    }
    // by this we are calling the function to run
    permission();
     
    Code (YAML):

    haspermission
    :
       expression
    : 'file: has_permission.js'
     

    Animated Text(top)

    Authors: aBooDyy && NathanG
    Description: Change the return value every 1 seconds the placeholder called. it have 3 different values (you can add more). This placeholder is useful for plugins which don't have animated text feature (e.g. DeluxeMenus Lore).
    Usage: %javascript_animated_text_<ID>,<Text1>,<Text2>,<Text3>%
    Example: %javascript_animated_text_1,&aA,&bB,&6C%
    IMPORTANT: if you have 2 placeholders or more have the same <ID> the placeholders won't work as they should. so make sure you change the <ID> (set it form 0-100+).
    NOTE: if you need help in adding/removing values OR got some issues with it, Feel free to ask me or anyone else in the Help Chat server in Discord.

    Code (Javascript):

    var messages = ["This is", "This is a", "This is a test message"];
    var numdata = "%player_name%." + IDv + "." + messages;
    var IDv = 0;

    function getMessage(ID) {
        if ( args.length == 4) {
            IDv = args[0];
            messages = [args[1], args[2], args[3]];
        }

        var msgnumber = Data.exists(numdata) ? Data.get(numdata) : 0;
        msgnumber++;

        if (msgnumber >= 3) {
            msgnumber = 0;
        }

        Data.set(numdata, msgnumber);

        return messages[msgnumber];
    }
    getMessage(IDv);
     
    Code (YAML):

    animated_text
    :
      file
    : animated_text.js
     
  • Loading...
  • Loading...