A Beginner's Guide to Writing Minecraft Plugins: Errata

Introduction

This is list of errors and their corrections that were found after the book was published. This document is a work in progress. If you find additional mistakes in the book please email walter.higgins@gmail.com.

Chapter 7

Page 84, Listing 7.3

The following code is listed with no explanation of the === operator and how it differs from the == operator which is explained in Chapter 3 (Comparing numbers):

if ( typeof sides ==='undefined' ) {
sides =6;
}

Like the == operator, the === is also used to compare two values in Javascript. However, the === uses a stricter set of rules for comparing two values, it tests that the values are of the same type and are equivalent. The == operator will resolve to true for the following comparisons:

4+1=="5"0==false''==false1==true'1'==true

This is because the == operator will try to convert the left and right operands (the values on either side of the == operator) so that they are the same type before comparing.

The === operator does no such conversion. It compares both the type and values and both type and value must be the same. This is why the === operator is more strict when comparing values. All of the above comparisons will return false because the types being compared are not the same. So the comparison 4 + 1 == '5' will evaluate to true but the comparison 4 + 1 === '5' evaluates to false because the number 5 is not the same as the string '5' when using the === comparison operator.

Page 85, 2nd Paragraph

The explanation for the code typeof sides === 'undefined' is incorrect. It reads:

The code inside the parentheses, typeof sides === 'undefined', returns the type of the sides parameter.

It should instead read:

The code inside the parentheses, typeof sides === 'undefined' does 2 things. The typeof sides expression gets the type of the sides parameter. This value is compared using the strict === operator to the string 'undefined'.

A better approach still when testing to see if a value is undefined is to compare it to the literal undefined using the === operator:

if ( sides ===undefined ) {
sides =6;
}

Chapter 8

Page 93, 3rd Paragraph

The code js console.log(farmAnimals) will not always display the array contents. Some versions of Java will instead display [object Array] or jdk.nashorn.internal.objects.NativeArray@ . This is because different versions of Java display Javascript arrays differently. The code should instead read:

js console.log(''+ farmAnimals)

The '' (single-quoted) string prefix forces the array contents to be displayed.

Page 95, Code Section, 2nd Statement

The following statment:

/js var greetings =require('greetings');

Should instead read:

/js var greetings =require('greeting');

The name of the module which must be required is 'greeting' not 'greetings'.

Chapter 9

Page 104, 2nd Paragraph

At the end of the paragraph, the book instructs you to issue the following commands at the server console prompt:

/js refresh();
/js guessTheNumber( self )

These commands should be issued at the in-game prompt instead because conversations only work within the in-game prompt, not the server prompt.

Page 106

There is an error in the code section half way down the page. The code should only be typed into the in-game prompt and should be as follows:

That is: the expression player.getStatistic( bukkit.stat.JUMP ); should be replaced with player.getStat( utils.stat.JUMP );. The code listing 11.4 is correct so you won't need to make any code changes.

Page 149

These statements should be entered at the in-game prompt not the server-console prompt. If entered at the server-console prompt you will see an error. The server console treats the ! (exclamation) character differently.

Chapter 14

Page 183, 2nd last Code example

There's an error in one of the code examples. The following code:

/js for (var key in myFirstObject){ self.sendMessage( key ) }

... should instead read:

/js for (var key in myFirstObject){ echo(self, key ) }

That is: the expression self.sendMessage( key ) should be replaced with echo( self, key ) .

Explanation

The expression self.sendMessage() only works with Bukkit and SpigotMC Minecraft servers. The book was originally written using the Bukkit API but was rewritten for use with CanaryMod when Bukkit was discontinued. The echo() function behaves much like the self.sendMessage() function but is compatible with both Bukkit/SpigotMC and CanaryMod.

Page 190, 1st Paragraph

The text:

Make your game mode creative by issuing the /gamemode survival command

...should instead read:

Make your game mode survival by issuing the /gamemode survival command