Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

LiquidBounce Forum

  1. Home
  2. Scripts
  3. Something about the Nashorn JS API & coding tips

Something about the Nashorn JS API & coding tips

Scheduled Pinned Locked Moved Scripts
4 Posts 3 Posters 349 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Konoha ScarletK Offline
    Konoha ScarletK Offline
    Konoha Scarlet
    wrote on last edited by Konoha Scarlet
    #1

    First of all, I wanna say that the ScriptAPI which LiquidBounce used is based on the Nashron JS, which runs at JS standard ES5. (It means that there are no Map, => or class in your script) Let me tell you something about the features of this unique API, which differs from the JS running on this page.
    NOTE: Some of following codes will be regarded as syntax error (in VS Code), please ignore them

    1. You can't use Kotlin standrad library and extensions in your scripts
    Many of us are enjoyed with Kotlin Lib and Extensions such as map, filter and sorted to Iterable<T>. They are not avilable in your scripts. Instead, you can use akin JS functions to implement such actions.

    2. You can bypass getters and setters in some particular contexts
    You can read data in ArrayLists like JS Array:

    arrayList[0] = 1;
    

    You can use alike method in any classes which implements java.io.Serializable such as LinkedList and HashMap.
    Unfortunately, it seems that Mojang doesn't like to implement this interface. As a result of that, we can only use blockPos.getX() instead of directly get x coordinates like a paramter. undefined Oops!

    3. You can use JS functions where needs Java 8 Fuctions(API)
    It means that you can use function () { [native code] } with Java 8 Stream API. Meanwhile, single line function can be written like this:

    [1, 2, 3].map(function(x) x * x).forEach(function(x) Chat.print(x));// out 149
    

    It will return the result of this line.

    4. You can use useful syntactic sugar which normal ES5 not included

    • Java-like for each
    for each (var entity in mc.theWorld.loadedEntityList)
    //...
    
    • Directly use some Java Classes
    var particles = new java.util.LinkedList();
    var healthMap = new java.util.HashMap();
    
    java.lang.Math.signum(0.5 - Math.random())//first Math is Java's, second is JavaScript's
    
    • some function extensions
    string.trimLeft()
    string.trimRight()
    //only for js str
    

    5. It is possible to add guarded catch clauses that only execute if the specified condition is true

    try {
        throw "BOOM";
    } catch (e if typeof e === 'string') {
        print("String thrown: " + e);
    } catch (e) {
        print("this shouldn't happen!");
    }
    

    More informations: https://www.baeldung.com/java-nashorn

    Enjoy 😛

    ? Plumer ManP 2 Replies Last reply
    1
    • ? Offline
      ? Offline
      A Former User
      replied to Konoha Scarlet on last edited by
      #2

      @konoha-scarlet good tutorial but still for a new user who sees this its not understandable

      Konoha ScarletK 1 Reply Last reply
      0
      • Plumer ManP Offline
        Plumer ManP Offline
        Plumer Man
        replied to Konoha Scarlet on last edited by
        #3

        @konoha-scarlet Very good tips 🙂 Would be useful but i barely use lb anymore

        1 Reply Last reply
        0
        • Konoha ScarletK Offline
          Konoha ScarletK Offline
          Konoha Scarlet
          replied to A Former User on last edited by
          #4

          @ali00035 said in Something about the Nashorn JS API & coding tips:

          good tutorial but still for a new user who sees this its not understandable

          For these users, I suggest to read MCP code (to be familiar with MC) rather than directly write scripts

          1 Reply Last reply
          0

          About
          • Terms of Service
          • Privacy Policy
          • Status
          • Contact Us
          Downloads
          • Releases
          • Source code
          • License
          Docs
          • Tutorials
          • CustomHUD
          • AutoSettings
          • ScriptAPI
          Community
          • Forum
          • Guilded
          • YouTube
          • Twitter
          • D.Tube
          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups