JavaScript Fundamentals in Minecraft

Planning
Step 1
In this lesson we are going to learn some fundamental JavaScript concepts that will help you make plugins for Minecraft!
Temp1476909320
Information
Step 2
Let's begin by talking about our coding environment

All code you write when making Minecraft plugins will go into the file called plugin.js directly to the right of your lesson.

Temp1476909500
Step 3
Practice adding code to plugin.js

Type the code pictured below into your plugin.js file.

bukkit.broadcastMessage(text) will display text in game to everyone on the server.

In this case text is "Hello World!" so we should see "Hello World!" printed in game.

Temp1476909807
Step 4
Before we can see what our code is doing we need to deploy it to the server

You should also remember to save often. Each time you want to test a change to your plugin you will need to deploy the plugin to the server and use the /reload or /rl command in game to reload the new version of the plugin.

Temp1476910210
Check Your Work
Step 5
When the server reloads you should see this message

When we reload the server it executes all of the code in our plugin files. For now our plugin only has one line of code that simply displays this message.

Temp1476910532
Information
Step 6
Variables!

Think of a variable like a box that has a label and something inside.

The label tells you which box you are referring to, and inside the box you find the contents. In this analogy the box is the variable and the contents inside the box are the "value" of that variable.

Temp1476911092



Pictured above we have a variable called x that has a value of 22!

Step 7
Define a variable called "text"

In JavaScript, we define variables using the keyword var before the variable name.

This code will define a new variable called text.

Temp1476911159
Step 8
Give "text" a value

In JavaScript we set the value of a variable using the = symbol.

This code will set the value of text to "Minecraft is fun!"

Temp1476911777
Step 9
Broadcast another message

Use bukkit.broadcastMessage() to display the value of text when we reload the server.

Temp1476911913
Step 10
Save, deploy, and reload
Temp1476911963
Check Your Work
Step 11
Make sure you see the message "Minecraft is fun!" displayed in game when you reload the server
Temp1476912081
Step 12
Set the value of "text" to "Something else"

Here we are changing the value of the text variable to "Something else" and then broadcasting a message using the same code as before.

Temp1476912382
Step 13
Save, deploy, and reload
Temp1476912400
Check Your Work
Step 14
You should see these three messages when you reload the server

With the power of variables, the same line of code broadcasts two different messages!

The first time we call bukkit.broadcastMessage(text); the value of text is `"Minecraft is fun!"

Then we change the value of text to "Something else"

Then, when we call bukkit.broadcastMessage(text); a second time, the value of text is "Something else"!

Temp1476912775
Step 15
Define a new variable called "num" and give it a numeric value

Here we define a new variable num and set its value to 9

Then we broadcast num to see the value printed in game.

Temp1476914899
Step 16
Save, deploy, and reload
Temp1476915167
Check Your Work
Step 17
You should see "9" printed in game like this

Here we see the value of num printed in game when we reload the server.

Temp1476915150
Information
Step 18
Data types

JavaScript variables can hold any of the possible data types:

  • Strings
  • Numbers
  • Booleans
  • Arrays
  • Objects

For now we are going to focus on Strings and Numbers

A string is a series of characters like "Hello World".
Strings are written with quotes. You can use single or double quotes.

A number is exactly that: a number. JavaScript has only one type of number.
Numbers can be written with or without decimals.

Temp1476914779
Information
Step 19
Why do we need data types?

Data types are important because they tell the computer how to handle certain operations such as +, -, *, and /

For the computer to evaluate the expression 6 + 6 it needs to first identify 6 as a number. Then, once it knows it is combining two numbers, it can add them together to get 12

To evaluate the expression "Hello" + "Goodbye" the computer first identifies "Hello" and "Goodbye" as strings (denoted by the double quotes) and then will concatenate the strings (link them together) to get "HelloGoodbye"

As you can see, data types are important because we wouldn't want 6 + 6 to evaluate to 66, and who knows what "Hello" + "Goodbye" would evaluate to if we tried to add them up!

If we try to combine a number with a string using + the computer will treat the number as a string. 6 + "Hello" will be evaluated as "6" + "Hello" and result in "6Hello"

Step 20
Add this broadcast to your plugin

What do you think it will print? Think about it for a moment before trying it out.

Temp1476916078
Step 21
Save, deploy, and reload
Temp1476916817
Check Your Work
Step 22
How did you do?

If you guessed it would print 12 then you were correct!

Temp1476916900
Step 23
Change the value of "num" as shown and then broadcast it again

Now what do you think the last broadcast will print?

Temp1476917033
Step 24
Save, deploy, and reload
Temp1476917052
Check Your Work
Step 25
How did you do?

If you answered 14 then you were correct!

Temp1476917209
Information
Step 26
How does this work?
  1. variable num is defined (has no value)
  2. value of num is set to 9
  3. we print num (value of num is 9)
  4. we print num + 3 (value of num is 9, 9 + 3 is 12)
  5. value of num is set to num + 5 (value of num is 9, num is set to 9 + 5 which is 14)
  6. we print num (value of num is 14)
Temp1476917522
Challenge
Step 27
CHALLENGE: Add these lines of code to your plugin. What will each of these four broadcasts print?

Without deploying the code to your server, think about what each of these four broadcasts will print by following the value of num.

When you are ready call an instructor over and explain to them how you reached the answer.

Temp1476918081
JavaScript Fundamentals in Minecraft Info

Account

MVCode Clubs

Created By

Mvcc logo new aaron

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach