JavaScript Expansion Scripts - Historical

Applied By aBo0oDGamer: Aug 9, 2018 at 12:39 AM

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
 

Rounded Player Health(top)

Author: xDizasterCYx
Description: Changes the ugly health placeholder to rounded integer.
Usage: %javascript_health%
Example: %javascript_health%

Code (Javascript):

function hp() {
    return Math.round(parseInt('%player_health%') * 100) / 100;
}
hp();
 
Code (YAML):

health
:
   file
: health.js
 

Rounded Max Player Health(top)

Author: xDizasterCYx
Description: Changes the ugly max health placeholder to rounded integer.
Usage: %javascript_maxhealth%
Example: %javascript_maxhealth%

Code (Javascript):

function hp() {
    return Math.round(parseInt('%player_max_health%') * 100) / 100;
}
hp();
 
Code (YAML):

maxhealth
:
   file
: maxhealth.js
 

Amount of Specific Item in Inventory(top)

Author: Piepmatz
Description: Gives the amount of an specific item in the inventory
Usage: %javascript_hasItem_[MATERIAL]<,NAME>%
Example: %javascript_hasItem_WOODEN_SHOVEL,&2Magic Shovel

Code (Javascript):

var player = BukkitPlayer;
var searchItem = null;
var searchName = null;
var amountOfItem = 0;

function checkForItem() {
  if (args !== undefined) {
    if (args.length >= 1) {
      searchItem = args[0].toUpperCase();
      searchItem = searchItem.replace("-", "_")
                             .replace("WOODEN", "WOOD")
                             .replace("SHOVEL", "SPADE");
    };
    if (args.length == 2) {
      searchName = args[1];
    };
  };

  var itemsInInventory = player.getInventory().getContents();

  for (i = 0; i < itemsInInventory.length; i++) {
    if (searchName != null) {
      if (itemsInInventory[i] != null && itemsInInventory[i].getType() == searchItem && itemsInInventory[i].getItemMeta().getDisplayName() == searchName) {
        amountOfItem += itemsInInventory[i].getAmount();
      };
    }
    else if (itemsInInventory[i] != null && itemsInInventory[i].getType() == searchItem) {
      amountOfItem += itemsInInventory[i].getAmount();
    };
  };
  if (searchItem == null) {
    return "&cSyntax: %" + "javascript_[MATERIAL]<,NAME>" + "%";
  }
  return amountOfItem.toString();
};

checkForItem();
 
Code (YAML):

hasItem
:
  file
: hasItem.js
 

Holding item display name(top)

Author: aBooDyy
Description: Return the display name of the item you're holding in your hand.
Usage: %javascript_displayname%

Code (Javascript):

var player = BukkitPlayer;
var material = '%player_item_in_hand%';

function displayname() {
    if ( material !== 'AIR' ) {
        var has = player.getInventory().getItemInHand().getItemMeta().hasDisplayName();
        var name = player.getInventory().getItemInHand().getItemMeta().getDisplayName();
    }

    if ( material === 'AIR' ) {
    // return 'AIR' when you aren't holding an item (You can change it to whatever you want)
        return 'AIR';
    } else if ( has ) {
        return name;
    } else {
    // returns the material name (%player_item_in_hand%) when it an item doesn't has a display name
    // You can change it to whatever you want by replacing material with 'WHAT YOU WANT'
        return material;
    }
}
displayname();
 
Code (YAML):

displayname
:
  file
: displayname.js
 

Lore lines(top)

Author: aBooDyy
Description: Returns how many lore line of the item you're holding.
Usage: %javascript_lore_lines%

Code (Javascript):

var player = BukkitPlayer;
var material = '%player_item_in_hand%';


function lorelines() {
    if ( material !== 'AIR' ) {
        var lore = player.getInventory().getItemInHand().getItemMeta().getLore();
        var has = player.getInventory().getItemInHand().getItemMeta().hasLore();
    }

    if ( material === 'AIR' ) {
    // return 'AIR' when you aren't holding an item (You can change it to whatever you want)
        return 'AIR';
    } else if ( has ) {
        return lore.length;
    } else {
    // return '0' when the item you're holding doesn't has lore (You can change it to whatever you want)
        return '0';
    }
}
lorelines();
 
Code (YAML):

lore_lines
:
  file
: lore_lines.js
 

Holding item lore(top)

Author: aBooDyy
Description: Returns the lore of the item you're holding all the lines of one line you specify.
Usage: %javascript_lore% OR %javascript_lore_<LineNumber>%
Example: %javascript_lore% to return all lore lines, %javascript_lore_1% to return lore line number 1

Code (Javascript):

var player = BukkitPlayer;
var material = '%player_item_in_hand%';

var line = ' ';


function itemlore() {
    if ( material !== 'AIR' ) {
        var has = player.getInventory().getItemInHand().getItemMeta().hasLore();
        var linelore = player.getInventory().getItemInHand().getItemMeta().getLore();
    }

    if ( material !== 'AIR' && has === true ) {
        var lore = player.getInventory().getItemInHand().getItemMeta().getLore().toString();
    }

    if ( material !== 'AIR' && has === true && lore.indexOf(', ,') !== -1 ) {
        lore = lore.replace(/, ,/g, ',  ,');
    }

    if ( args.length == 1 ) {
        line = args[0];
    }

    if ( material === 'AIR' ) {
    // return 'AIR' when you aren't holding an item (You can change it to whatever you want)
        return 'AIR';
    } else if ( has && line === ' ' ) {
        return lore.replace(/^\[/, "").replace(/.$/,"").replace(/, /g, '\n');
    } else if ( has && line !== ' ' ) {
        line = parseInt(line) - 1;
        return linelore[line];
    } else {
    // return ' ' (Nothing/blank line) when the item you're holding doesn't has lore (You can change it to whatever you want)
        return ' ';
    }

}
itemlore();
 
Code (YAML):

lore
:
  file
: lore.js