Java Reference
In-Depth Information
makeFrosting(vanilla)
makeFrosting(chocolate)
That's why we use functions: to make long lists of instructions (code) easier to
read and understand, and to reuse sets of instructions with slightly different data.
We snuck something else into this example. Our makeFrosting function isn't
real Java code. When you write out an idea that's codelike but isn't really a
programming language, we call it pseudo-code . Programmers use pseudo-
code the same way artists sketch a picture before starting to paint—it helps
them to see the big picture.
You could say functions make programming a piece a cake. But back to
Minecraft.
Defining Functions in Java
Every bit of code we write in Java will be in a function; that's how Java works.
We've seen functions already, right from the very first plugin.
Back in the HelloWorld plugin, we declared a function that Minecraft calls when
the game is running: helloCommand .
We call these kinds of functions in a plugin the entry points . These are the
functions that the Minecraft server will call when it needs to. You provide the
code, and the game will call it when needed.
In our helloCommand , we're calling other functions. Here's the section from Hel-
loWorld :
public void helloCommand(MessageReceiver caller, String[] parameters) {
String msg = "That'sss a very niccce EVERYTHING you have there..." ;
Canary.instance().getServer().broadcastMessage(msg);
}
There's a call to something named instance() , a call to getServer() , and a call to
broadcastMessage() .
Java knows you're calling a function because of the parentheses after the
name of the function. It will expect that someone defined a function based
on the name and it will give that function your message. We call the stuff you
pass to functions arguments . When arguments are given to a function, the
function knows them as parameters . We say the values are passed in or the
function is called with these values. All these words and phrases are referring
to the same concept.
For example, the getServer() function doesn't take any arguments. You still use
the parentheses characters, ( and ) , so that Java knows it's a function. That
 
 
Search WWH ::




Custom Search