Java Reference
In-Depth Information
Although there's space for 5 values, we're only using the first 3 here, and
that's okay.
Getting values out looks just like putting values in, only the other way around.
Using the code we just looked at, you retrieve the values like this:
int myBestQuiz = quizScores[1];
int aBadDay = quizScores[2];
To get all the values, you can use an old-fashioned for loop:
for ( int i=0; i < 5; i++) {
me.chat( "Quiz score #" + i + ": " + quizScores[i]);
}
Remember that Array is a fixed size; if you try to retrieve a value that's past
the end of the array (like quizScores[15] ), your plugin will throw an error and
crash. In this case, since we define the quizScores array to have a size of 5, you
can safely store and retrieve values at index 0, 1, 2, 3, and 4. That's why we
use i<5 in the middle, instead of <= .
It's a lot safer to use the Array 's .length field instead of hard-coding a number
like “5.” So it would be better to write that loop like this:
for ( int i=0; i < quizScores.length; i++) {
me.chat( "Quiz score #" + i + ": " + quizScores[i]);
}
In a little bit we'll discuss an even better way to loop through all the values
in an array.
Let's do the same thing now, but with Minecraft blocks.
Here's an example of code for a quick plugin that builds a tower of different
block types. I'm using our helper function setBlockAt to change air into one of
several different materials. 1 Let's walk through this and see what's happening.
Plugin: ArrayOfBlocks
ArrayOfBlocks/src/arrayofblocks/ArrayOfBlocks.java
package arrayofblocks;
import net.canarymod.plugin.Plugin;
import net.canarymod.logger.Logman;
import net.canarymod.Canary;
import net.canarymod.commandsys.*;
1.
The codes for different materials are in the Canary API docs at https://ci.visualillusionsent.net/
job/CanaryLib/javadoc/net/canarymod/api/world/blocks/BlockType.html .
 
 
 
Search WWH ::




Custom Search