-
Notifications
You must be signed in to change notification settings - Fork 15
Vol2
Before we go on, I would like to ask a question.
What can a plugin do? What is it for?
This is a tricky question. Everyone knows that plugins add things to PocketMine, but few can classify their main uses. Plugins can basically do anything, but three main components are commands, event handling and scheduled tasks. We are going to discuss about them one by one in this volume.
We are going to register a command called /greet
.
It is very easy to register a command. You don't even need to write PHP code. Remember our old plugin.yml
? Time to open it again. The last time we saved it, it should be like this:
name: MyPlugin
version: 1.0.0
author: YourName
api: 1.12.0
main: yourname\myplugin\MyPlugin
The next part is going to be about commands, so add a new line commands:
name: MyPlugin
version: 1.0.0
author: YourName
api: 1.12.0
main: yourname\myplugin\MyPlugin
commands:
To add a command, write down its name greet
, description (as shown in /help) Send back a message
and usage message /greet<usage here>
:
name: MyPlugin
version: 1.0.0
author: YourName
api: 1.12.0
main: yourname\myplugin\MyPlugin
commands:
greet:
description: Send back a message
usage: /greet <usage here>
It is pretty straightforward. You would understand what this thing means even if I didn't teach you.
You can add multiple commands:
name: MyPlugin
version: 1.0.0
author: YourName
api: 1.12.0
main: yourname\myplugin\MyPlugin
commands:
greet:
description: Send back a message
usage: /greet <usage here>
another-command:
description: This is another command
usage: /another-command <argument> <another argument>
Hmm, typing /greet every time seems to be too troublesome. What about adding aliases? In this way, typing /hey or /hi would be converted into /greet automatically.
name: MyPlugin
version: 1.0.0
author: YourName
api: 1.12.0
main: yourname\myplugin\MyPlugin
commands:
hi:
description: Send back a message
usage: /greet <usage here>
aliases: [hey, hi]
another-command:
description: This is another command
usage: /another-command <argument> <another argument>
Note that it must be called aliases
not alias
. Use a pair of square braces ([]
) to contain the aliases, and separate the aliases by commas (,
).
Note: commands must not contain colons (
:
) or spaces. This is because colons are used to specify the owner of a command (you don't need to care about this as it rarely happens), and spaces are used to separate the command name and the arguments behind it.
Anyway, we have registered the command, but we haven't yet added the function to handle the command. Now, we are heading to our first PHP function. We are going to write real code, where we are seeing infinite possibilities.
We handle commands in a function
in your main class called onCommand
.
Before doing anything else, again, look at this code and copy it into your main class's class body, i.e. between the class ... PluginBase{
line and the }
line.
public function onCommand(CommandSender $sender, Command $command, $label, array $args){
$commandName = $command->getName();
if($commandName === "greet"){
$sender->sendMessage("Hi!");
return true;
}
}
Also, in the use
statements at the beginning, use
the following classes:
use pocketmine\command\Command;
use pocketmine\command\ComamndSender;
Don't read below yet. Read the code above. How much do you understand?
Now let's see if you are correct.
Functions originate from mathematics. A common notation for a mathematical function is f(x)
. For example, f(x) = 3x + 2
, which means that writing f(x)
is equivalent to writing 3x + 2
, and writing f(12)
is equivalent to 3 * 12 + 2
, which is 38
.
In PHP, we write f(x) = 3x + 2
like this:
function f($x){
return 3 * $x + 2;
}
- On the first line,
function
means you are defining a function. - The next word,
f
, is the function name. - The things inside the parentheses
()
are known as parameters or arguments.$x
is an example of a parameter. All parameters must start with$
, followed by a parameter name. The rules for a parameter name is same as that of a class name (A-Z
a-z
0-9
_
), except that the convention for variables is to start with a lowercase word (likethisIsName
instead ofThisIsName
).$x
represents the value passed in. For example, if someone usesf(12)
,$x
will represent the12
. - The content between the
{
and the}
is called the function body. When someone uses the function, the code inside the function body is run.
In this function, the function body is return 3 * $x + 2;
. return
means that the expression between return
and ;
will be set as the function's result, a.k.a. function return value in PHP. In this case, the function's return value is 3 * $x + 2
. If $x
represents 12
, the function's return value will become 3 * 12 + 2
, and hence the return value is 38.
The return
line is known as the return statement. As you can guess, there are other types of statements apart from a return statement.
Actually, a function body can contain a theoretically infinite number of statements. Each statement is delimited (i.e. marked as ended) by a semicolon ;
.
unfinished page
PocketMine Plugin Tutorials wiki
Copyright © 2020 PEMapModder
Everyone is welcome to read, save, update, redistribute and/or modify the contents of this Repositorywsdazxsasd