Java Reference
In-Depth Information
Imports
Each object recipe in Java lives in a package, like java.util or net.canarymod.Canary , or
something like that. You have to declare this package in an import statement at the
very top of the Java source code file.
If you forget, you'll get an error from javac that says something like “ cannotfindsymbol ”.
You need to look in the Canary documentation or the Java documentation to find the
full package name. We'll use a lot of the common ones in our examples, for Player ,
Server , Location , Entity , and various Java libraries. For your convenience, these are all
listed in Appendix 7, Common Imports , on page 253 .
So with fred in hand, you can get—and set—data for that player, using the
period ( . ) to indicate which function or variable you want inside that object.
Here are a few snippets of code that show what that looks like:
// Is fred sneaking?
boolean sneaky = fred.isSneaking();
// Hungry yet?
int fredsHunger = fred.getHunger();
// Make him hungry!
fred.setHunger(0);
// Where's fred?
Location where = fred.getLocation();
The Player object has a function named isSneaking() , which returns a true or false ,
depending on whether that player is in sneaking mode.
Remember, that's the kind of thing you can use in an if statement:
if (fred.isSneaking()) {
fred.setFireTicks(600); // Set him on fire!
}
There's also a getHunger() function that returns an int telling you how hungry
that player is. You can set the player's hunger level as well. The last snippet
here shows how to get the player's current location in the world.
As you might guess, objects contain internal variables that their functions
work with. For instance, in these examples the Player object for Fred has a
location stored internally. We can get the value of Fred's location, we can set
a new value, but at all times Fred has his own internal copy of his current
location.
 
 
 
Search WWH ::




Custom Search