Main menu

Category Archives: ScriptCraft

Post navigation

I decided for my first ScriptCraft project I would try to create a rainbow. There were two reasons for choosing this:

I thought it’d look nice

It would need a bit of maths, so it’s something you couldn’t easily build by-hand

I was a bit disheartened when I found that the creator of ScriptCraft, Walter Higgins, had included a rainbow as a standard function in ScriptCraft, but I persisted regardless and I’m glad I did.

There are, I think, three things that make this script interesting:

It’s got some useful maths in it

It’s got an array variable

It’s got a second function

So, I’ll have a quick chat about each of these, but because it’s fundamental to the rest. This post is all about the maths.

I can just about hear the groans now, but I promise, this is crazy useful and really not that hard.

Useful Maths – Calculating the Area of a Rectangle or Square

The area of a rectangle or square is just the width multiplied by the height. Take a look at the two shapes below. The rectangle is 2 high and 5 wide and contains 2 x 5 = 10 squares. It’s area is 10. The square is 5 high and 5 wide and it’s area is 5 x 5 = 25.

Count the little squares if in any doubt.

Useful Maths – Squares and Square Roots

In a square, width and height are the same so the square’s area is always the same number multiplied by itself. Above it was 5 x 5 = 25.

A number squared is simply the number multiplied by itself. The name makes sense when you see that it relates to the area of a square, as above.

Two squared is 2 x 2 = 4. Three squared is 3 x 3 = 9. A square is often written as the number with a superscript 2 after it. You can see that here:

A square root is the reverse of a square. If 2 x 2 = 4, then the square root of 4 is 2. In the same way, the square root of 9 is 3. Every positive number has a square root, but most aren’t easy to guess like the two I’ve just shown. The square root of 2 is 1.414! A calculator or your computer program can always figure them out though. A square root is normally written as shown here:

Useful Maths – Measuring Distance in 2D and 3D

Now that we know about squares and square roots, we can talk about calculating the distance between two points in 2D and 3D. This is something programmers need to do all the time. The technique that’s used is known as Pythagoras’ Theorem and it’s been round since ancient times.

The person who invented it (probably not Pythagoras, even though his name’s on it now) noticed that there was something interesting about triangles which have right-angles in them (a perfectly square corner). They noticed that if you squared the length of the two shorter sides and added them together that that was the same as the square of the length of the longest side. Notice that the longest side is always the one which is directly across from the right angle. This diagram shows everything I’ve just said visually, showing the squared side lengths as physical squares:

If you’re in doubt, all the little squares are exactly the same size, you can count them to make sure they’re the same.

You might not see it, but we now have everything we need to calculate distances in 2D. Consider two points, shown in red below, which are x units apart horizontally and y units apart vertically:

It’s a right-angle triangle! This means that we know that the distance is related to the length of x and y like this:

but we don’t want distance squared, we only want distance so we can reverse the squaring with a square root which gives:

This is really easily extended to 3D too. If the points are at different Z positions and z is the difference between them in that direction the distance is:

Which is almost exactly the same, but has the z bit added.

In JavaScript – Measuring Distance in 2D and 3D

So, what does this look like in JavaScript? A little different, but easy to understand.

JavaScript doesn’t have a short way to write “squared” so we just multiply the variable by itself to get the squared value.

When it comes to square roots, the built-in Math object has a function for calculating square roots called “sqrt”.

Lets see how this looks together:

var x = 4;
var y = 3;
var distance = Math.sqrt((x * x) + (y * y));

Hopefully you can relate this to what we’ve shown above. Even if you don’t get it, you can still copy and use this code yourself if you need it.

The extension to 3D is almost identical. You’re probably figured it out, but I’ll show it anyhow for completeness:

Our mods:

Operators:

Operators in any programming language are used when you want to calculate something new: they operate on values. variables, or expressions to produce a new value.

Since ScriptCraftJS is built on the JavaScript langauge, it uses standard JavaScript operators. As it happens, many other programming languages (including C, C++ and Java) use the same operators or very similar ones.

Drone Functions:

As we have seen before, in ScriptCraft you use a drone to do your building for you. The drone has functions that are part of it.

Here are some of the main drone functions that are useful when building your mods:

Reminder: JavaScript is a well-established programming language, mainly used in web development. ScriptCraft is a Minecraft mod that allows you to write JavaScript code for building structures in Minecraft and writing new Minecraft mods. (So it’s a mod for creating other mods.)

Steps 1 and 2: Try out ScriptCraft commands and write simple mods

Step 3: Comparing JavaScript to Scratch

Some people criticise Scratch as being “childish”, but I don’t agree. While it is designed so that even 8 year olds can use it, it is still has all of the key features of ‘adult’ programming languages, as listed in the image at the top of this post.

(Technically, any programming language with variables, decision and loops is Turing Complete.)

This means that, if you already know how to write a Scratch programs that use these features, you will be able to apply that knowledge to any other language, such as JavaScript. The syntax of JavaScript is different, but it uses the same computational thinking.

Notes:

Even though they have basic ideas in common, every programming language has its own specific commands that relate to its purpose: Scratch is focused on 2D games and animations, while ScriptCraft is focused on operating inside Minecraft, and JavaScript generally is used for interactive websites.

the echo command that features in these slides is not a standard JavaScript command, it is just used in ScriptCraft to display things on your screen in Minecraft. Everything else is standard JavaScript.

Planning and design: as you may remember from when you learned how to program in Scratch, a programmer’s most important tools are paper and pencils, for figuring out what you want to create before you write code for it.

You might find it helpful to work in pairs on designing and coding your mods. During the session, I will ask people to load their mods onto my computer, demo them to the group, and explain how their code works. To get things started, at the top and bottom of this post are pictures of mods created by Ruaidhri and Luke. We will take a look at how they work.

Practical notes:

During development and testing, you can end up with lots of incomplete structures that slow down your CraftBukkit server. A simple fix is to delete your world:

Stop your CraftBukkit server

Open the CraftBukkit folder on your computer: in it you will see folders called world and possibly world_nether and world_the_end delete them all

While you are at it, edit server.properties in the CraftBukkit folder to make your new world superflat, write level-type=flat and you could also change other properties such as spawn-monsters=false

Our First Mod: sayhi.js

I’ll show you how to write a very simple one, sayhi.js, and then a more complex one, scraper.js. After that, it’s up to you to figure out what you can do!

In the Canarymod folder, find the folder scriptcraft / plugins, then make a new folder with your name (or any unique folder name).

Using Notepad++, type in the code shown below and save it in a file called sayhi.js. Here are explanations of what everything means:

Try it out in Minecraft and see if it works!

NOTE: Every time you create a new ScriptCraft program or edit one, you have to type /js refresh() in Minecraft to update your changes. If no commands run, make sure you are opped. (In the Canarymod window, type op <your_mincraft_name>).

To run it, type /js sayhi() in Minecraft.

A More Complex Mod: scraper.js

This makes the skyscraper shown above. We will figure out together how it works!

Previously, we installed a Canarymod server and inside it we installed ScriptCraft, a mod that will allow us to build structures in MineCraft by writing JavaScript code.

Canarymod is a Minecraft Server. Therefore, the instructions we followed to connect to it were:

Start MineCraft, making sure it’s the same version (1.8 in our case)

Choose Multiplayer, then Direct Connect and enter localhost as the server address

If this works, in your CraftBukkit window you’ll see a message that you connected.

Here, localhost is actually means “the address of this local computer”.

But you can also connect to a friend’s computer, if you are all on the same network and you can find out their address. (If you are on different networks, things get a bit more complex: you will have to search online about how to set up port forwarding on your router, and you will probably also need a free Dynamic DNS service.)

On a local network, to find your IP address, open a command window. (In Windows, run or search for cmd.) In the command window, enter the command ipconfig and read through to find an entry like this: