JavaScript Section Tutorial en - Glomzzz/RandomItem GitHub Wiki

JavaScript Section Tutorial

Learn the JavaScript

https://www.javascripttutorial.net/

JavaScript section in RandomItem

Simple

script:
  type: javascript
  script: |
    var id = <quality.data-json>.id;
    switch(id){
     case "Rough":
     return 1;
     case "Normal":
     return 2;
     case "Fine":
     return 3;
     case "Excellent":
     return 4;
     case "Epic":
     return 5;
     case "Legendary":
     return 6;
     default:
     return 0;
     }

What is "<quality.data-json>"?

it's a calling of the string section quality 's substring after random base on weights

it will return the JSON of the substring

   -> quality.data-json :
    {
      "id": "Rough",
      "objects": [
        "WOODEN_SWORD",
        "Rough",
        "Quality: Rough",
        "_______<-Quality Attribute->_______",
        " Damage: 10 ",
        "0"
      ],
      "weight": "45"
    }

You can call the variables by the id directly in the script

Complex

if u wanna use function in the script, you must write a main function and run it by yourself

script1:
  type: javascript
  script: |
    function main(){
      var id = <quality.data-json>.id;
    switch(id){
     case "Rough":
     return 0;
     case "Normal":
     return 1;
     case "Fine":
     return 2;
     case "Excellent":
     return 3;
     case "Epic":
     return 4;
     case "Legendary":
     return 5;
     default:
     return 114514;
     }
    }
    main();

(The same is true for custom objects)

Other

You can run other javascript sctions's script by calling it! (<{js section's id}-script> -> <script1.script>)

script1:
  type: javascript
  script: |
    <script2.script>
script2:
  type: javascript
  script: |
    var id = <quality.data-json>.id;
    switch(id){
     case "Rough":
     return 0;
     case "Normal":
     return 1;
     case "Fine":
     return 2;
     case "Excellent":
     return 3;
     case "Epic":
     return 4;
     case "Legendary":
     return 5;
     default:
     return 114514;
     }

<script2.script> :

    var id = {
      "id": "Rough",
      "objects": [
        "WOODEN_SWORD",
        "Rough",
        "Quality: Rough",
        "_______<-Quality Attribute->_______",
        " Damage: 10 ",
        "0"
      ],
      "weight": "45"
    }.id;
    switch(id){
     case "Rough":
     return 0;
     case "Normal":
     return 1;
     case "Fine":
     return 2;
     case "Excellent":
     return 3;
     case "Epic":
     return 4;
     case "Legendary":
     return 5;
     default:
     return 114514;
     }

Different sections,different JSONs.

String section

   -> quality.json :
    {
      "value-map": {
        "Excellent": {
          "id": "Excellent",
          "objects": [
            "GOLDEN_SWORD",
            "Excellent",
            "Quality: Excellent",
            "_______<-Quality Attribute->_______",
            " Damage: 50 ",
            "1"
          ],
          "weight": "6"
        },
        "Epic": {
          "id": "Epic",
          "objects": [
            "DIAMOND_SWORD",
            "Epic",
            "Quality: Epic",
            "_______<-Quality Attribute->_______",
            " Damage: 70 ",
            "2"
          ],
          "weight": "3"
        },
        "Legendary": {
          "id": "Legendary",
          "objects": [
            "NETHERITE_SWORD",
            "Legendary",
            "Quality: Legendary",
            "_______<-Quality Attribute->_______",
            " Damage: 100 ",
            "3"
          ],
          "weight": "1"
        },
        "Fine": {
          "id": "Fine",
          "objects": [
            "IRON_SWORD",
            "Fine",
            "Quality: Fine",
            "_______<-Quality Attribute->_______",
            " Damage: 30 ",
            "0"
          ],
          "weight": "10"
        },
        "Normal": {
          "id": "Normal",
          "objects": [
            "STONE_SWORD",
            "Normal",
            "Quality: Normal",
            "_______<-Quality Attribute->_______",
            " Damage: 20 ",
            "0"
          ],
          "weight": "35"
        },
        "Rough": {
          "id": "Rough",
          "objects": [
            "WOODEN_SWORD",
            "Rough",
            "Quality: Rough",
            "_______<-Quality Attribute->_______",
            " Damage: 10 ",
            "0"
          ],
          "weight": "45"
        }
      },
      "id": "quality"
    }
   -> quality.data-json :
    {
      "id": "Rough",
      "objects": [
        "WOODEN_SWORD",
        "Rough",
        "Quality: Rough",
        "_______<-Quality Attribute->_______",
        " Damage: 10 ",
        "0"
      ],
      "weight": "45"
    }

Number section

   -> strength.json :
    {
      "id": "strength",
      "bound": "20",
      "start": "1",
      "fixed.integer.max": "-1"
      "fixed.integer.min": "0",
      "fixed.decimal.max": "0",
      "fixed.decimal.min": "0",
      "decimal": "0",
    }

Caculation section

   -> x.json :
    {
      "fixed.decimal.max": "2",
      "max": "233",
      "fixed.integer.min": "0",
      "fixed.decimal.min": "0",
      "formula": "(1+(0/2)) * 0 * 18 / 20",
      "id": "x",
      "fixed.integer.max": "-1"
    }

javascript section

   -> script.json :
    {
      "id": "script",
      "script": "var id = <quality.data-json>.id;
    switch(id){
     case "Rough":
     return 0;
     case "Normal":
     return 1;
     case "Fine":
     return 2;
     case "Excellent":
     return 3;
     case "Epic":
     return 4;
     case "Legendary":
     return 5;
     default:
     return 114514;
     }"

   -> script.script :
    var id = {
      "id": "Rough",
      "objects": [
        "WOODEN_SWORD",
        "Rough",
        "Quality: Rough",
        "_______<-Quality Attribute->_______",
        " Damage: 10 ",
        "0"
      ],
      "weight": "45"
    }
    }

If you wanna see more about JSON, turn on the "debug" option in config.yml, please

How to call the JSON of a random section?

Narmally,we can call it by "<id.json>"

If u wanna call the substring's JSON of a strings section, enter this "<id.data-json>"

⚠️ **GitHub.com Fallback** ⚠️