Forum

Notifications
Clear all

Notes for BlockLauncher Mods

(@ctrlaltcuteness)
Active Member

Since I'm not good at wording, and this is a W.I.P., please offer feedback at clarifying these points and / or let me know ones I'm missing. (Mention or message me on Twitter to check this topic if I don't respond quickly: (at)CtrlAltCuteness)

BIG NOTE:
Your installed BlockLauncher version MUST be compatible with your installed Minecraft version!

Common reasons mods may have an error:
(unrelated to the game crashing)

  • A ModPE call being used only exists in a future version of BlockLauncher.
  • An Android system call is being used that does not work as expected, or does not exist, on your Android device.
  • The implemented Javascript language that your installed BlockLauncher uses does not support a certain feature or syntax layout.
  • There is a typo somewhere.
  • (Might cause a crash) Using a mod which modifies, or adds custom model shapes.
// -- This hook is missing in the ModPE script dump!
// can use preventDefault()
function procCmd(cmd);
// For info: If the player types '/spam eggs' into chat,
// the  this is called with the single string "spam eggs"
Quote
Topic starter Posted : 20/02/2018 11:35 am
(@ctrlaltcuteness)
Active Member

Example: procCmd hook - Dice Roller

A somewhat complex dice command.
/dice dice-spec
dice-spec must be in one of the formats below:

  • n
  • Rolls one dice with n sides.

  • ndm
  • Rolls n dice with m sides.

function procCmd(cmd) {
  "use strict"; // Enable strict mode and catch some typos and other
    // slowdowns that are not worth it. It must be the first
    // non-comment in a function to make it apply to the function and
    // everything """created""" inside it.

  // 'mo' means 'match object' when talking about Regular Expressions.
  // (This example is not about Regular Expressions, so it will
  // not be as commented on as the rest of this code.)
  // Add a space to the end and make this easier.
  var mo = /^dices([sS]*)$/.exec(cmd + " ");

  // A regular expression object's '.exec()' returns
  // the 'null' value if it failed, which is """falsey""".
  if (!mo) return; // This is not our command! Abort!
  // Else it is, and we are always going to use it,
  // so stop anything else from seeing it.
  preventDefault();

  // If the command was specified but not any arguments,
  // our first capture group in 'mo' will be """falsey""".
  // We also remove all """white-space""" at the start
  // as well as as end to test if that is the case.
  if (!(cmd = mo[1].replace(/^s+|s+$/g, ""))) {
    // Task: Add some help text.
    clientMessage(ChatColor.RED + "Add some help text here...");
    return;
  }

  // This is the test that an input must match to
  // be considered valid.
  var re = /^(?:([1-9][0-9]*|)[dD])?([2-9]|[1-9][0-9]+)$/;

  // Our rolled value lies here.
  var result = 0;

  // For a fast way, remove the multi-line
  // comment from this code.
  /*
    var arr = cmd.split(/s+/);
    for (var i = arr.length, n, s; i--; ) {
      if (!(mo = re.exec(arr)))
        return clientMessage(ChatColor.RED + "Invalid dice specifier found.");
      for (
          result += (n = (mo[1] ? Number(mo[1]) + 1 : 2)) - 1,
          s = Number(mo[2]);
          --n;
          result += Math.floor(Math.random() * s)
      ) {}
    }
    return clientMessage(ChatColor.GREEN + "You rolled: " + result);
  */

  // And back to documented code!

  // These may be used several times, so speed us up
  // by creating them only once.
  // 'rollOnce()' as well as 'rollMany()' are separated just
  // because of the fact of adding some explaination-by-code.
  var rollOne = function (sides) {
    return Math.floor(Math.random() * sides) + 1;
  }
  var rollMany = function (count, sides) {
    var result = 0;
    // Trust me on how much faster this is when
    // using 1 million coins (2 sides).
    for (++count; --count; result += rollOne(sides)) {}
    return result;
  }
  // Splitting with '/s+/' is better for this use.
  // '.every()' is kind-of new to the Javascript standard.
  if (cmd.split(/s+/).every(function (v, x) {
    if (!(x = re.exec(v))) {
      // 'clientMessage()' returns an 'undefined' value, which is
      // considered 'false' with an 'if' check.
      return clientMessage(ChatColor.RED + "Invalid dice specifier: " + v);
    }
    // Reusing variables to allow a small speedup.
    v = Number(x[1] || 1); // Default is one for "d20", etc.
    x = Number(x[2]);
    // Since 'rollOne()' and by proxy 'rollMany()' never
    // return zero by our input, this will always return
    // a value for a 'true' if check, allowing the
    // '[].every()' to continue on.
    return (result += rollMany(v, x));
  })) clientMessage("You rolled " + result);
}
ReplyQuote
Topic starter Posted : 20/02/2018 11:37 am
Share: