TIL: "Minecraft & Rainbows"

If there will ever be a Minecraft movie, then it would be a blockbuster.

-Unknown
(gif: share.drewlearns.com/f6bc0e) Minecraft rainbows using javascript in script craft
PEW PEW

How I learned JavaScript

When we had had our second kid, I was fortunate enough to have a job that gives 8 weeks of paternity leave (thank you Flywheel). A lot of things happened during this hiatus; besides having a kid we made a cross country move from Alaska to North Carolina. To pass time, I spent a lot of nights reading books on JavaScript while waiting for the next feeding time.

I read one book that was like an encyclopedia. It was called “JavaScript & Jquery – interactive front-end web development” by Jon Duckett. While I felt like the theory is there, I hadn’t a clue how to actually develop anything in JavaScript from scratch which left me feeling pretty down about it all. Bored, tired, and frustrated I decided to do something about it. I bought the next book on my amazon list for learning code in Minecraft thinking worst case scenario “I have something to play with my son when he wakes up“.

Like a little kid who just did some hardly-noteworthy accomplishment, I was so excited that I was shooting out a line of rainbow blocks from a simple script I had wrote that I let out a loud “YASSSSS!”.

Of course this woke the baby, ending my evening of Minecraft. It also woke up my excitement for learning more JavaScript. It’s written for kids so I blazed through the first half of the book the first night and :boom: it’s like so obvious/easy now that I have an actual application. It even teaches how to write your own bash commands!

Anyways, I tell ya all that to recommend the book Code a Minecraft Mod in Javascript Step by Step by Joshua Romphf as a first step in developing with Javascript instead of reading the later (unless you just want a reference book).

This Lesson is a step by step guide to reproducing one of the many projects from this book with a little of my own spin to it. I hope you enjoy reading it as much as I did learning and sharing it.

TL;DR: I read a book on how to code JS but found a kid’s book with a real application (Minecraft).
Below is what I learned

What you came here for: How to shoot rainbows

Let’s do some setup:

Please note: these instructions are for mac os and may differ greatly in windows.
Download Java Development kit from: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.hml (follow instructions to download and install for your OS, you may need to create an account). Install it.

Download Minecraft: Java edition Download Minecraft: Java Edition | Minecraft (https://www.minecraft.net/en-us/download/) – you will need a paid license (trial version will not work for this).  Create and account, install and log into your account.

Open Minecraft.

Then note the version of Minecraft installed:

Close Minecraft (for the moment), we will get to the fun stuff in just a bit!

SPIGOT Mine Craft & BuildTools.jar

On your desktop create a folder called “SpigotMC”.

Go to BuildTools.jar (https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar).  Download the file and save it to the directory you just created called ~/Desktop/SpigotMC.

Prepare the preparations! This part is command line interface (CLI) heavy.

Note: You can drop a comment in the comment section at the bottom of this article if you get stuck or would like to provide feedback!

Run :

Note: anywhere you see me say run without specifically specifying I am referring to terminal):

cd /Desktop/SpigotMC
ls

You should see BuildTools.jar in this directory.

Run:

java -jar BuildTools.jar --rev 1.14.4 

Note: The rev number above should match the version of Minecraft you noted before.

When it is completed you should see: 

“Success! Everything compiled successfully. Copying final .jar files now.”

Run:

ls

You should now see 2 versions of “spigot-1.xx.x.jar” this means it upgraded and you should be able to now run your own personal server with your own plugins on your computer.

You should also see a file called eula.txt. 

Edit this file using nano by running:

nano eula.txt

Edit the 3rd line down that says: “eula=false” to say “eula=true” – this means you accept mojang’s agreement from https://account.mojang.com/documents/minecraft_eula.

Press “^” (“control”) & “X” keys to close the nano editor.

Type “Y” then “return” to save.

Install Scriptcraft!

Now we want to add JavaScript capabilities to our Minecraft server – we do this by downloading the plugin called scriptcraft:

Run:

cd ~Desktop/SpigotMC/plugins
git clone https://github.com/walterhiggins/ScriptCraft/releases/download/3.4.0/scriptcraft.jar
ls

You should now see “scriptcraft” in the plugins directory.

To back out of the scriptcraft plugins directory and go up one level in the folder heirarchy, run:

cd ..

Clear your terminal by typing:

clear

Then press “return“.

Now, to initiate your server run:

java -jar spigot-1.14.4.jar

Note: Version should match your Minecraft version!

‘Gif’ yo’self the Power!

Make yourself a server operator, by running:

> op drewlearns 

Note: Use your Minecraft username not “drewlearns” and the “>” mark is not needed to be typed.

Test to see if scriptcraft is running by typing into terminal:

> js 2+2

You should see the results:

[Timestamp} [Server thread/INFO]: "4"

The 4 means it added 2+2 and output 4.

Let’s test what we have done so far!

Start Minecraft and click the “play” button again.

Click multiplayer:

Click “Add Server”.

Type in a name of your choosing for the server and make the Server Address “localhost”.

Join the server you just made.

The Minecraft realm should load. 

Now type “/“ to test if you can run js from within Minecraft then type the following:

js echo(self, "Hello World!");

You should see “Hello World!” Pop up on the screen!

Note: Close Minecraft and terminal for this next bit!

Stream line

Let’s change gears for a moment and make that whole process you just went through a little bit easier!
Create a new .txt file in your favorite text editor and save it to the desktop as spigotmc.command (your version of spigot in this file should match per the above!):

#!/bin/bash 
cd ~/SpitgotMC && java -jar spigot-1.14.4.jar

Change the file permissions of that file you just made by running:

cd ~/Desktop && chmod u+x spigotmc.command

Now if you open that file, you should be able to start the local server with a single click.

Note: You will need to close the server, save the js file you edit, and restart it after ANY/ALL changes are made to your JS file we make in a bit. At least if you’d like to run from within Minecraft and then run:

>js refresh();

Install xcode and make it executable from terminal!

Go to VSC’s website and download Visual Studio Code so we have a work space and code editor to work in.

After downloading and installing Visual Studio code open it up and make it the active window.

Now open the Command Palette by pressing:

⇧⌘P

Now type:

Shell Command: Install 'code' command in PATH

This installs the ability to open a directory from within terminal!

Close Visual Studio now and Open Terminal.

In terminal type the following command to open the spigotMC folder (directory) we created early on:

cd ~/Desktop/SpigotMC

CLI pro tip here:

You can type in $ cd ~/D (just the first letter of the directory/file you want to open) and press the “Tab” and it will produce options available for example: “Desktop” “Downloads” will present themselves with just the “/D” but if you typed “De” it will auto fill out “Desktop” and save you a few keystrokes.

ls

You should see a list of all files within this directory specifically a folder called ScriptCraft. This is the directory we are going to open. Now type the following into terminal:

cd scriptcraft
ls

Now we are looking for a folder called “plugins”. So, ls again to see what we have. It should look something like this:

data      lib       modules   plugins   readme.md

We are opening the plugins folder:

cd plugins && ls

CLI pro tip:

You can string together terminal commands by typing “&&” between them and it will run them one ofter the other. Alternatively, you can say “;” (example: $ cd plugins; ls)between them but if the first command fails, the second will not run.

It should look like this:

alias             commando          examples          spawn.js

arrows.js         homes             signs

at.js             drone             minigames

classroom.js      entities.js       signs

We are going to create a JavaScript file here by running:

touch drewlearns.js

Now we can open this directory from terminal directly in Xcode (Visual Studio Code) by running:

code .

Terminal should open up and provide you something that looks like this:

We are cooking with bacon now! Let’s create our first JavaScript command. Before we do there is some things you should know:

This plugin folder will be where you put any new code you wish to run from JavaScript within Minecraft but any changes you make to it will not be visible without closing your server and the spigotmc.command we created earlier.

I already wrote the code and also provided really helpful comments to help you understand each line below. All ya gotta do if you want to run the rainbow command without any JavaScript knowledge is copy the following to this file in the editor window for drewlearns.js file we created:

Note: You can see what each line means in the comments between the “/*” and then “*/

You can make multiline comments in this fashion or single line comments by using “//” if you prefer.

Feel free to customize this as you see fit and name the variables anything you like.

/*multiline comment*/
//single line comment

exports.drewlearns = { 
    /*
    xports.exercises = {}: will make sure than anything we put in here will be loaded by scriptcraft without adding anything to other files.
    */
    
        /*
        Contained within that scripts curly brackets aka "Mustaches" is where we will index 
        our script and call it "rainbows:" followed by calling a function(){}
        */
    
    rainbows: function () {
    
                /*
                The function is what we are going to "code" here to shoot rainbows nested within it's curly             
                brackets.
                */
            var droneOne = new Drone(self);
    
                /*
                First, within that function we are going to create a local variable and call it "droneOne" specifically ("var drone") and initialize it with an "=".  This sets the variable's value and makes it "appear". Then we are going to make a Drone - basically a bot that does whatever we tell it to do from our current position by saying "new Drone(self)".  There are tons of things we can do with Drones we will get into soon. Now exit out of the variable with a ";". We want to exit out of all lines of code when using JavaScript.
                */
    
            var i = 0;
                /*
                From here we are going to create a while loop.  While loops execute any nested codes (nested within the mustaches from the "function(){}" above. We will do this based on it's conditions we set.  Typically we do this with an interval which we will call "i". We will want to initialize and interval "i" and set it's value to 0 and then every time the command is executed we will want it to increase that interval by 1. We will get into that in a moment. We will add the command to increase the increment after the event we want to repeat happens.
                */

            while (i < blocks.rainbow.length) {
                /*
                This command says "move forward one block every iteration and drop one block of the current color
                */
               /*
               Let's break this down: 
                A while loop is called by saying "while(){}" with the conditions placed inside the parenthesis and the commend to run "while" those conditions are "true" inside the "Mustaches". When the condition of interval "i" is >= (greater than or equal to in this specific case) the condition on the right - the expression is "false" and stops running the loop. In this case "i < some condition". Now, "blocks.rainbow.length" is value we want to call, calculate and compare to "i" and see if it's true or false. If the expression is true, it will run what is inside the "mustaches"(
                    Not getting too deep in the specifics on this here but block.rainbow.length looks up the block called rainbow and determins how many of those blocks exist in it's array) and counts them.  In this specific array there is a 0-7 value, meaning there is 8 rainbow blocks.  meaning the 9th time this while loop runs, the expression will = "false", stop running any commands within it's "mustaches" and go to the next command.
               */
                
              var currentBlock = blocks.rainbow[i]; 
                /*
                We set a new vaiable to calculate how many blocks we have placed using the interval we created earlier ("i") and update that interval so that the position we place the next block doesn't replace the one we just put down. Without this line, we would just shoot out one block that changes 8 colors. We call this variable "currentBlock", initialize it using "=" and then index the blocks.rainbow's array's index.  Initially it is set to 0 (We set "i" to = 0 in line 26 above)
                */
                droneOne.fwd().box(currentBlock); 
                /*
                Remember we created the "droneOne" variable earlier, here we are calling that variable and telling it to perform a built in command called "fwd()". "fwd()" tells the drone we created to move forward to the ".box()" and telling it that the "box" to move to is the one we set up in the "currentBlock" variable before and place the next block in the rainbow array out.
                This is an example of method chaining.
                */
                i++;
                /*
                Increasing the value by 1 increment is done by using "++".  (You could also have the interval count down by using "--" but for this case we want to count up.)
                */
            }
        } // if you wanted to make another command, place a comma here and place the next {} with your "nameOfTheCommand: function(){};"

Now press “⌘” and “s” keys together to save the file.

Let’s fire this bad boi up!

This is the part you have been waiting for. All this hard work can be put together here and the results are fun but first close all your active windows and let’s start from the top.

Remember that bash command we created on your desktop called “spigotmc.command” – fire that bad boi up. You should see your terminal open up and some stuff fire up. It should look like this:

Your server is running with your rainbow shooting command ready to go!

Start Minecraft and click the “play” button again.

Click multiplayer:

Join the server you made before.

The Minecraft realm should load. 

Now within the Minecraft world you just created, started, and modified… type “/“ you are locked and loaded with a rainbow cannon!

Type the following command:

js drewlearns.rainbows();

Pew-Pew! You did it, Congrats!

If you have any questions or feedback about this tutorial, feel free to drop a comment below and don’t forget to subscribe to this blog so you can get the latest Minecraft or coding projects I share.

Share on telegram
Telegram
Share on twitter
Twitter
Share on reddit
Reddit
Drew Karriker

Drew Karriker

My name is Drew Karriker, and I am really passionate about helping others, learning, and computers.

I created this site as a place to post projects and share things I have learned.

Learn More About Me...

Recent Articles:

Let's be friends...

Hey Friend! You can use this form or send an email if you’d like to reach out! I’d love to hear your thoughts!

Email: drew@drewlearns.com

Telegram: @drewlearns

Scroll to Top

Join the drew learns community

I'm looking for motivated and active members in the coding community to join a slack space focused on various coding languages, and share our passion for learning.

Will you join us?