CCBlueX Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    Something about the Nashorn JS API & coding tips

    Scripts
    3
    4
    199
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Konoha Scarlet
      Konoha Scarlet last edited by Konoha Scarlet

      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. :axounhappy: 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 Man 2 Replies Last reply Reply Quote 1
      • ?
        A Former User @Konoha Scarlet last edited by

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

        Konoha Scarlet 1 Reply Last reply Reply Quote 0
        • Plumer Man
          Plumer Man @Konoha Scarlet last edited by

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

          1 Reply Last reply Reply Quote 0
          • Konoha Scarlet
            Konoha Scarlet @Guest last edited by

            @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 Reply Quote 0
            • First post
              Last post
            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