[background=rgba(255, 255, 255, 0)]Note: This is a very basic pocketmine tutorial. If you want to learn more, I suggest you visit sekjun9878's tutorial here: http://www.minecraft...tmine.[/background]
[background=rgba(255,255,255,0)]If you want to learn more about PHP go to codeacademy.com and search for PHP. This website really helped me and should benefit you too.[/background]
[background=rgba(255,255,255,0)]Now, to the real tutorial...[/background]
Part 1: Preparation[/background]'][background=rgba(255,255,255,0)]If you are really serious about coding your own pocketmine plugin, you will need a code editor like notepad++. This is the recommended editor by Sekjun. It is flexible and easy to use. (It's also free .) I would suggest downloading it from cnet.com. You will also need your own Pocketmine server, available from pocketmine.net. One you have downloaded and prepared all of the above programs, you are ready to start programming.[/background]
[background=rgba(255,255,255,0)]
PHP Basics']Instead of dumping a bunch of code on you and leaving you to decipher it all, I will give you snippets of code and tell you how each of them works. If you are still having trouble understanding the code, visit the above codeacademy link. Here we go.
Several rules to remember about PHP:
All lines of code end in '{', '}', or ;. (We'll get to this a little later.)
Anything with a '$' in front of it is a variable or array. (This will also be explained later.)
Open up Notepad++ and go to File>Save As. Set the name of our plugin to 'ExamplePlugin' and set the file type to .php. THIS IS IMPORTANT! DO NOT FORGET IT! Now we put in the header. This header tells Pocketmine exactly what this plugin does and who it was made by.
First things first. <?php just says to the server, 'This is written in PHP.' The /* and */ surrounding the header tell pocketmine that this is its header. The __PocketMine Plugin__ lets the console know that this is a plugin. name=ExamplePluginis simply stating the name of the plugin. This will be printed to the console. The version=0.0.1 line is there to state what version the plugin is. This will also be printed to the console. author=shoghicp is the author of the plugin, in this case you. This will also apear in the console. Our class=ExamplePlugin line is very important. It tells our sever where to start reading the plugin. It is wise to have this be the same as the name of the plugin. Our final line: apiversion=7 will tell the server if the plugin was designed for that version of pocketmine. It will let you know if the plugin has a chance of crashing or corrupting the server. This is the only optional line. YOU MAY EDIT WHAT IS AFTER THE =, BUT NOT WHAT IS BEFORE.
class ExamplePlugin implements Plugin{
private $api;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
}
Below out header, skip a line and copy this in below. Make sure that ExamplePlugin is the same as our class in the header. Besides that, do not edit this part. Now to our actual programming.
Lesson 1: Variables
Variables are the heart and soul of PHP. Without variables, PHP simply could not function. For those of you who have successfully completed the math course known as Pre-Algebra, you will know that x is often know as a common variable. This variable is a slot that holdes a value, ie. 6. In PHP a variable is denoted by a $ in front of it. For example, our variable x in PHP would be understood as $x. This variable can be as long or as short as you would like. For example, you could have variable $abcde. Now that we understand what a variable is, we can store a number in its "slot." If we wanted to say that we wanted our variable $abcde to be equal to 6, we would type:
$abcde = 6;
Congradulations! You have just stored your first variable in PHP! Also, you may have noticed the semicolon at the end of this line of code. This lets our console know that the line has ended and that we are done with our code. Remember that variables don't necesarily need to be numbers, they can contain words, or strings as they are properly known. Now we will create our other variable, $x. Follow the format above and set $x to 3.
$x = 3;
Our two variables are not equal, as anybody knows, 3 is not equal to 6. However, we programmers have a fancy way of saying not equal. it looks like this:
$x != $abcde;
Our != simply means is not equal. The above code is true. 3 != 6. Other helpful things we programmers use are our friends < and > (less than and greater than) as well as <= and >= (less than or equal to and greater than or equal to.) These friends are used in the same way as =. We can also use + and - in PHP.
$x + 1 < $abcde;
Similarly, booleans are used in PHP. A boolean is a simple true/false that can be used in substitution of a value or string. They are used to let us know a condition of an object with only one other possible condition. They look and function in the same way:
$MCPErules = true;
We use these friends of ours as ways to communicate through our plugin. Congratulations! Move on to lesson 2.
Lesson 2: If/Else/ElseIf and our first outputs
Our if/else codes will tell the server what to do in a certain situation. Recall our variables $abcde and $x from earlier. Our if/else block of code will follow this format:
if ($x = 3) {
console ("Our variable x is equal to 3!");
} else {
console ("Our variable x is not equal to 3!");
}
}
I know you are feeling confused, but I will let you know exactly what this code does. In plain English terms it is saying "If our variable $x is equal to 3 then the console will say 'Our variable x is equal to 3!' Otherwise, the console will say'Our variable x is not equal to 3!'" The beginning of the code, if ($x = 3) {, contains the terms for our console to print. It is always inside parentheses. In this case, the requirement for printing our first statement to the console is $x = 3. Now notice our { at the end of that line. This is telling us that we are starting our reward for meeting the requirement of $x = 3. Now we skip to the next line and indent. Here is the actual reward for our program. The console will print our message. In order to do that, we must put our message in parentheses and quotation marks. Also, Don't forget to end the line with our good ol' friend, the semicolon. Now lets say $x did not equal 3. Then we move on to our else block of code. We indent again and enter ourselves the other squiggly bracket (}) to close off our code from above, letting the console know that we're done with the if portion of this code. Since in our hypothetical world, $x != 3 (See what I did there?), we will perform a different action, also enclosed in our new friends { and }. Place another { and skip a line. Indent and we'll put in our above console's output, but adding a not in between is and equal. Remember to end with a semicolon. Now since our hypothetical $x does not equal 3, our console will skip running the first console print and will skip to the second. Skip a line and put in our closing squiggly bracket to close off the Else block. Skip another line and add our final squiggly bracket to close off our entire if statement. That's all nice, but what if we want to find out if $x is equal to 4? This is where elseif comes in. Elseif is pretty much exactly what it sounds like. If the requirements are not met for If, instead of jumping to a default Else, we can try again and have another If! We can use elseif in our above code to check to see if $x = 4:
if ($x = 3) {
console ("Our variable x is equal to 3!");
} elseif ($x = 4) {
console ("Our variable x is equal to 4!");
} else {
console ("Our variable x is not equal to 3!");
}
}
Congrats! You now understand If/Else/Elseif!
Lesson 3: Switch
Ah... our switch statements. These statements are like a long list of elseifs. They are used when you want a different result for each of many different outputs. They use a format that uses the term case often. All this refers to is saying "if this is the case." Here is an example:
switch($x) {
case 0:
console("0");
break;
case 1:
console("1");
break;
case 2:
console("2");
break;
case 3:
console("3");
break;
default:
console("Variable x is unknown!");
}
At the top of this code, we see our switch($x). This is telling the rest of the code block to start the switch statement. I'll get to our ($x) in a moment. We have our { as usual and then skip a line and indent. These are our cases that I talked about earlier. The code inside of each of these cases is run when the string or value after our case is the one above in the perhentheses. For example, if this code were to be run, case 3 would be activated since the value in the perhentheses is $x (or 3). No other cases are run, only case 3. Since case 3 is running, our console will print 3. Now below our console line, we have a fancy break; with the same indentation. This lets the server know not to run anything below it and that this is the end of the case. If you forget it, the case below it (default) will also run (I'll get to our default case in a second.) The case below it is special and optional. It does not say "case", and is treated differently. This default case is run only when the argument ($x) does not equal 0, 1, 2 or 3 or when a break; is forgotten. For example, if $x = 4, none of our cases are triggered and our default case runs. Then our console would display Variable x is unknown! since we have no idea what it is because it didn't activate a previous case. Remember, this line is optional. Skip a line and close off all of the code with }.
Lesson 4: Bedtime: our sleep code.
Sleep is a simple and easy way to make our console 'do nothing' for a little while. It is used like this:
sleep(10);
console("I just woke up!");
After this code is run, nothing will happen for 10 seconds. Then, the console will print 'I just woke up!' Remeber, the number in the perhentheses is the length of time the server will rest (in seconds.) The child of sleep() is usleep(). Usleep works in the same way, except that it has the plugin rest for the number of microseconds in parentheses. Microseconds are millionths of a second. Similarly, there is time_nanosleep(x,y). This works the same way as sleep() and usleep() except that it has two arguments. X is the time it will sleep in seconds and y the time it will sleep in nanoseconds. A nanosecond is one billionth of a second.
For now, I suggest running this code independently of pocketmine. Use the run feature of Notepad++ instead and replace our console commands with print. Print will follow the same format, but NOT HAVE THE PERHENTHESES.
Lesson 1: Arrays
The importance of arrays in PHP cannot be overstated. They are the most important part of PHP other than variables. Arrays in PHP are very similar to variables, due to the fact that they often follow similar rules. The names of arrays also start with $, but are treated differently because of what comes after the =. Instead of containing a single value, arrays carry several strings within them. Arrays follow this format:
$array = ("Arrays", "Are", "Great");
So our array $array at this point has three strings in it: Arrays, Are and Great. Each of these strings has a specific location in the array, starting with 0. In this case, Arrays occupies location 0, Are occupies location 1 and Great occupies location 2. These locations are called indexes. These indexes contain our strings, also known as elements Now lets say we want to find the string in the second index in this array. To do this, we need to remember that the second element is actually in slot location - 1. In this case, it is in index 1. If we wanted to print this second index to the console, we would have to follow this format:
We would print to the console element 1 of $array. The number in normal brackets is the index of the string we want. So the console would print to us: Are. If we wanted to modify one of our elements, we would do something similar to defining a variable:
$array[2] = "Awesome";
The console is being told to define the element in index 2 of array $array and set it to be "Awesome". Our array now reads Arrays, Are and Awesome. In an almost identical way, we can delete an element or the entire array with unset:
unset($array[2]);
Now our array contains only Arrays and Are. To delete our entire array we would type:
unset ($array);
Arrays are wonderful in the world of PHP because they act like a list that can contain anything from the names of all the people to enter your server to the numbers from 1 to one million. The possibilities are simply endless.
Lets say we want to add something systematically to the end of our array. That is what array_push is for. It is actually a function (you'll learn about this in part 3) that adds something to the end of our array. Lets say we didn't delete our array and we still only have Arrays and Are as part of our array. Now we want to add amazing to the end of our array. Watch:
array_push($array, "amazing");
Array_push takes 2 arguments, which array the string will be pushed to and the string that will be puched. These are what you see after array_push. So now our array $array reads Arrays Are Amazing. Using array_push is an easy way to add some new strings to the end of our array.
For now, I suggest running this code independently of pocketmine. Use the run feature of Notepad++ instead and replace our console commands with print. Print will follow the same format, but NOT HAVE THE PERHENTHESES.
Lesson 1: Functions: Part A
For me, functions have been and still are a bit tricky. They can be used to easily modify strings and arrays without having to go through a lot of unnecessary coding. Lets see how they work:
As you already know, our $function on the first line is a variable, denoted by the $ and defined by abc. Now $edit is defined by a version of $function that has been modified by a function, strlen. Strlen is short for 'string length' which will save to our $edit the length of $function (abc.) In this way, $edit now stores the number 3. Now, on the next line, our console is simply printing $edit, or 3. See how this can be somewhat complicated? If not, you soon will.
We have several other functions we use in PHP, including strtoupper and strtolower. These guys work in a similar way--they modify a string. Strtoupper simply makes all of the characters in the variable uppercase. Strtolower similarly makes all characters lowercase.
As you can see, these two other functions will work in an identical way to our previous one. Our console will print on two seperate lines:
ABC
abc
This can be very helpful when trying to limit caps spam in chat.
This is the hardest part of programming in PHP you have yet encountered. Oh man, it's gonna be a real pain in the but to type this all out... Anyway, OOP is the way that objects in your server will interact with each other. All of these objects belong to a class. This class is a group of objects that share properties. It looks like this:
class Pig(){
$breathes = true;
$dropsPork = true;
$name;
}
The first line is letting our server know that it has a class, Pig. in the { and }, you will find the properties that all of these pigs have. They all breathe, drop pork and have a name. These properties look eerily similar to variables and can contain a boolean, value or string. This is optional, as you can see with $name. All pigs have a name, but not all of there names are the same, which you will soon learn to program. Now that we have our class, we can create an actual object, or in this case, pig. An object is a thing that contains properties. To create our pig, we follow this format:
$Piggy = new Pig(Piggy);
The $Piggy at the beginning of our code is the actual name of the object. Quite unoriginally, our pig is named Piggy. To create $Piggy we need an = and 'new Pig' afterwards to let the code know that Piggy is a new Pig. The last (Piggy) is going to be used in a moment.
Now for our constructor:
public function __construct($name) {
$this->name = $name;
}
This constructor is called each time a new object is made. It delivers individual properties to an object. The properties we want given to each new object are delivered in this way. This time, $name is being delivered. Our public function __construct is used to let the code know that this is our constructor function. The ($name) is the property that will be assigned a value, in this case, $Piggy will recieve the name 'Piggy.' The indented $this->name = $name; below is acting as a directory. It is telling our plugin to go from where it is now to the stuff in perentheses above where we defined Piggy and setting what we find there to $Piggy's $name. This is one of the most complicated parts of PHP. If you don't understand it, I suggest you visit this: http://www.codecademy.com/courses/web-beginner-en-ZQQ64/0/1?curriculum_id=5124ef4c78d510dd89003eb8.
Lesson 3: Intermediate OOP
Now that we know how to find and assign properties to our objects, let's make our objects actually do something. We'll attatch functions to our objects. A function tied to an object is called a method. Now, for our first method, we will have Piggy talk. Watch:
public function talk() {
return "I'm a talking pig!";
}
Similar to our __construct function (actually a method ), this function can be called, but not when a new object is created. Instead of being called __construct, this method is called talk(). When this function is called, the object will return "I'm a talking pig!" If we wanted our pig to say his name:
public function name() {
return "I'm ".$this->name.".";
}
Notice how we added the $this->name in there. This is also directing the object to state its unique name. Make sure that .$this->name. is outside of quotation marks and within periods. Now let's call these methods to Piggy.
echo $Piggy->name();
(Echo is similar to print, but we will use it to call the name() method on Piggy.) The $Piggy-> is letting the plugin know who is calling the method. The name() is the method being called onto that object. $this and -> can also be used on our server to run specific aspects of our server. For example:
$this->api->console->run(give RealSmileyguy 1 1);
Would be the same as if I were to enter '/give RealSmileyguy 1 1' into the console. It would give my player 1 stone block. The arrows are simply saying where the method is and what to do with it. You can also use the arrow notation to guide the plugin to use built in functions. For example, $this->api->time->set(0); will set the time to 0. For more information, see the api documentation at the bottom of this post.
Good luck!
Q: My code won't work! It keeps saying syntax error! A: This simply means you forgot a simple piece of punctuation. Check for missing brackets, semicolons, spaces or other issues.
Q: My plugin keeps saying that it is outdated and can possibly crash or corrupt the server! A: This has to do with the header and the api version. This means that the plugin may not work. However, some plugins are timeless and will be able to work with any version of Pocketmine-MP. I would still run it. It should still be okay.
Q: How do I load a plugin? A: Drag your saved plugin (.php or .pmf only) into the plugins directory in your Pocketmine-MP server.
Helpful links:
You need to add instructions on how to summon a variables using $this-> because I had A LOT of trouble when I first started making plugins and if you are familiar with java programming it means the same as . and its where you summon something in ex. this.someThingI will start making simplified videos on my YouTube channel on making plugins once I hit 50 subscribers
class First Plugin{
private $api, $server, $config;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
}
if ($x = 3) {
console ("Our variable x is equal to 3!");
} else {
console ("Our variable x is not equal to 3!);
}
}
Please give me a working version of it.
Thanks,
Ryan:) :| :/
This looks pretty good, except for the fact that you forgot to define $x. Try adding $x = 3 above
if ($x = 3) {
console ("Our variable x is equal to 3!");
} else {
console ("Our variable x is not equal to 3!);
}
}
But below the previous code. Great job! So nice to see an interest in PHP coding!
You need to add instructions on how to summon a variables using $this-> because I had A LOT of trouble when I first started making plugins and if you are familiar with java programming it means the same as . and its where you summon something in ex. this.someThing
I will start making simplified videos on my YouTube channel on making plugins once I hit 50 subscribers
I too am still learning PHP and am still shaky with $this->. I would love to see some of your videos and will post a link to them in this forum as soon as I view them. I also am trying to have the readers fully understand the basics of PHP before delving into summoning. Thanks for the awesome feedback!
Rollback Post to RevisionRollBack
Java/C/C++ dev, server owner and hacker extraordinare
This is pretty knowledgable stuff, I just started codecademy a few months ago but I only use it to sharpen my html css and js I never thought the site was able to do php that why I never really tried it but I guese ill try it if I want to fix some of the slugish plugins I have downloaded...
This looks pretty good, except for the fact that you forgot to define $x. Try adding $x = 3 above
if ($x = 3) {
console ("Our variable x is equal to 3!");
} else {
console ("Our variable x is not equal to 3!);
}
}
But below the previous code. Great job! So nice to see an interest in PHP coding!
I too am still learning PHP and am still shaky with $this->. I would love to see some of your videos and will post a link to them in this forum as soon as I view them. I also am trying to have the readers fully understand the basics of PHP before delving into summoning. Thanks for the awesome feedback!
Thanks and how do you run the php file out of PocletMine in Notpad++?
I have made a simple plugin that will trick players into believing Herobrine has joined the game. At first I had a few syntax errors, but then I fixed them. I ran it again, and PocketMine simply said Failed loading plugin: evaluation error. I'm not sure what went wrong. Here is the script if you can pick up any errors right away.
class ExamplePlugin implements Plugin{
private $api;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
}
public function init(){
$this->api->console->register("hb", "This command summons Herobrine", array($this, "handleCommand"));
}
public function __destruct(){
}
public function handleCommand($cmd, $arg, $params){
switch($cmd){
case "hb":
console("Herobrine has joined the game");
console("Herobrine: I AM HERE. I AM WATCHING. ALWAYS.");
break;
case "hbscare":
$user = strtolower(array_shift($params[0]));
$player = $this->server->api->player->get($user);
console("Herobrine: I have taken \"$player\". Better watch your back.");
break;
}
}
}
?>
I also have one more question: how would I make a plugin that renders the player invisible? Or is that extremely advanced?
Thanks for your help!
Sorry, my internet has been really sporadic and I haven't been able to post recently. @themayorofspace, I'm still learning too and will take a look at it. Just by examining the code, I realized that for your second command (hbscare), you need another line of code similar to hb above to register the command with the console. Try:
Also: are you using quotation marks or double apostrophes? One more thing, your text will only be printed to the console. Is this what you want? Other than this, looks pretty good!
Rollback Post to RevisionRollBack
Java/C/C++ dev, server owner and hacker extraordinare
The Meaning of Life, the Universe, and Everything.
Join Date:
11/25/2012
Posts:
42
Member Details
<?php
/*
__PocketMine Plugin__
name=HBScare
version=1.3.3
author=themayorofspace
class=HBScare
apiversion=9
*/
class HBScare implements Plugin{
private $api;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
}
public function init(){
$this->api->console->register("hb", "This command summons Herobrine", array($this, "handleCommand"));
$this->api->console->register("hbscare", "(description)", array($this, "handleCommand"));
}
public function __destruct(){}
public function handleCommand($cmd, $args, $issuer, $alias){
switch($cmd){
case "hb":
$this->api->chat->broadcast("Herobrine has joined the game");
$this->api->chat->broadcast("Herobrine: I AM HERE. I AM WATCHING. ALWAYS.");
break;
case "hbscare":
$user = strtolower($args[0]);
$this->api->chat->broadcast("Herobrine: I have taken $user. Better watch your back.");
break;
}
}
}
@Andro_rus, this looks pretty good, the only thing I see that may mess up the code is the second to last line of code. I think that this->API->chat->broadcast("Herobrine: I have taken " . $user . ". Better watch your back.") would work better and prevent "$user" from being broadcasted.
Rollback Post to RevisionRollBack
Java/C/C++ dev, server owner and hacker extraordinare
Thank you all! It worked perfectly. I was also wondering if the invisibility plugin is possible. If it is could I get information on it, as I would like to program that myself. But again, thank you all.
[background=rgba(255,255,255,0)]If you want to learn more about PHP go to codeacademy.com and search for PHP. This website really helped me and should benefit you too.[/background]
[background=rgba(255,255,255,0)]Now, to the real tutorial...[/background]
[background=rgba(255,255,255,0)]
Several rules to remember about PHP:
V Copied and pasted from Pocketmine.net. V
First things first. <?php just says to the server, 'This is written in PHP.' The /* and */ surrounding the header tell pocketmine that this is its header. The
__PocketMine Plugin__ lets the console know that this is a plugin. name=ExamplePlugin is simply stating the name of the plugin. This will be printed to the console. The version=0.0.1 line is there to state what version the plugin is. This will also be printed to the console. author=shoghicp is the author of the plugin, in this case you. This will also apear in the console. Our class=ExamplePlugin line is very important. It tells our sever where to start reading the plugin. It is wise to have this be the same as the name of the plugin. Our final line: apiversion=7 will tell the server if the plugin was designed for that version of pocketmine. It will let you know if the plugin has a chance of crashing or corrupting the server. This is the only optional line. YOU MAY EDIT WHAT IS AFTER THE =, BUT NOT WHAT IS BEFORE.
Below out header, skip a line and copy this in below. Make sure that ExamplePlugin is the same as our class in the header. Besides that, do not edit this part. Now to our actual programming.
Lesson 1: Variables
Variables are the heart and soul of PHP. Without variables, PHP simply could not function. For those of you who have successfully completed the math course known as Pre-Algebra, you will know that x is often know as a common variable. This variable is a slot that holdes a value, ie. 6. In PHP a variable is denoted by a $ in front of it. For example, our variable x in PHP would be understood as $x. This variable can be as long or as short as you would like. For example, you could have variable $abcde. Now that we understand what a variable is, we can store a number in its "slot." If we wanted to say that we wanted our variable $abcde to be equal to 6, we would type:
Congradulations! You have just stored your first variable in PHP! Also, you may have noticed the semicolon at the end of this line of code. This lets our console know that the line has ended and that we are done with our code. Remember that variables don't necesarily need to be numbers, they can contain words, or strings as they are properly known. Now we will create our other variable, $x. Follow the format above and set $x to 3.
Our two variables are not equal, as anybody knows, 3 is not equal to 6. However, we programmers have a fancy way of saying not equal. it looks like this:
Our != simply means is not equal. The above code is true. 3 != 6. Other helpful things we programmers use are our friends < and > (less than and greater than) as well as <= and >= (less than or equal to and greater than or equal to.) These friends are used in the same way as =. We can also use + and - in PHP.
Similarly, booleans are used in PHP. A boolean is a simple true/false that can be used in substitution of a value or string. They are used to let us know a condition of an object with only one other possible condition. They look and function in the same way:
We use these friends of ours as ways to communicate through our plugin. Congratulations! Move on to lesson 2.
Lesson 2: If/Else/ElseIf and our first outputs
Our if/else codes will tell the server what to do in a certain situation. Recall our variables $abcde and $x from earlier. Our if/else block of code will follow this format:
I know you are feeling confused, but I will let you know exactly what this code does. In plain English terms it is saying "If our variable $x is equal to 3 then the console will say 'Our variable x is equal to 3!' Otherwise, the console will say 'Our variable x is not equal to 3!'" The beginning of the code, if ($x = 3) {, contains the terms for our console to print. It is always inside parentheses. In this case, the requirement for printing our first statement to the console is $x = 3. Now notice our { at the end of that line. This is telling us that we are starting our reward for meeting the requirement of $x = 3. Now we skip to the next line and indent. Here is the actual reward for our program. The console will print our message. In order to do that, we must put our message in parentheses and quotation marks. Also, Don't forget to end the line with our good ol' friend, the semicolon. Now lets say $x did not equal 3. Then we move on to our else block of code. We indent again and enter ourselves the other squiggly bracket (}) to close off our code from above, letting the console know that we're done with the if portion of this code. Since in our hypothetical world, $x != 3 (See what I did there?), we will perform a different action, also enclosed in our new friends { and }. Place another { and skip a line. Indent and we'll put in our above console's output, but adding a not in between is and equal. Remember to end with a semicolon. Now since our hypothetical $x does not equal 3, our console will skip running the first console print and will skip to the second. Skip a line and put in our closing squiggly bracket to close off the Else block. Skip another line and add our final squiggly bracket to close off our entire if statement. That's all nice, but what if we want to find out if $x is equal to 4? This is where elseif comes in. Elseif is pretty much exactly what it sounds like. If the requirements are not met for If, instead of jumping to a default Else, we can try again and have another If! We can use elseif in our above code to check to see if $x = 4:
Congrats! You now understand If/Else/Elseif!
Lesson 3: Switch
Ah... our switch statements. These statements are like a long list of elseifs. They are used when you want a different result for each of many different outputs. They use a format that uses the term case often. All this refers to is saying "if this is the case." Here is an example:
At the top of this code, we see our switch($x). This is telling the rest of the code block to start the switch statement. I'll get to our ($x) in a moment. We have our { as usual and then skip a line and indent. These are our cases that I talked about earlier. The code inside of each of these cases is run when the string or value after our case is the one above in the perhentheses. For example, if this code were to be run, case 3 would be activated since the value in the perhentheses is $x (or 3). No other cases are run, only case 3. Since case 3 is running, our console will print 3. Now below our console line, we have a fancy break; with the same indentation. This lets the server know not to run anything below it and that this is the end of the case. If you forget it, the case below it (default) will also run (I'll get to our default case in a second.) The case below it is special and optional. It does not say "case", and is treated differently. This default case is run only when the argument ($x) does not equal 0, 1, 2 or 3 or when a break; is forgotten. For example, if $x = 4, none of our cases are triggered and our default case runs. Then our console would display Variable x is unknown! since we have no idea what it is because it didn't activate a previous case. Remember, this line is optional. Skip a line and close off all of the code with }.
Lesson 4: Bedtime: our sleep code.
Sleep is a simple and easy way to make our console 'do nothing' for a little while. It is used like this:
After this code is run, nothing will happen for 10 seconds. Then, the console will print 'I just woke up!' Remeber, the number in the perhentheses is the length of time the server will rest (in seconds.) The child of sleep() is usleep(). Usleep works in the same way, except that it has the plugin rest for the number of microseconds in parentheses. Microseconds are millionths of a second. Similarly, there is time_nanosleep(x,y). This works the same way as sleep() and usleep() except that it has two arguments. X is the time it will sleep in seconds and y the time it will sleep in nanoseconds. A nanosecond is one billionth of a second.
For now, I suggest running this code independently of pocketmine. Use the run feature of Notepad++ instead and replace our console commands with print. Print will follow the same format, but NOT HAVE THE PERHENTHESES.
Lesson 1: Arrays
The importance of arrays in PHP cannot be overstated. They are the most important part of PHP other than variables. Arrays in PHP are very similar to variables, due to the fact that they often follow similar rules. The names of arrays also start with $, but are treated differently because of what comes after the =. Instead of containing a single value, arrays carry several strings within them. Arrays follow this format:
So our array $array at this point has three strings in it: Arrays, Are and Great. Each of these strings has a specific location in the array, starting with 0. In this case, Arrays occupies location 0, Are occupies location 1 and Great occupies location 2. These locations are called indexes. These indexes contain our strings, also known as elements Now lets say we want to find the string in the second index in this array. To do this, we need to remember that the second element is actually in slot location - 1. In this case, it is in index 1. If we wanted to print this second index to the console, we would have to follow this format:
We would print to the console element 1 of $array. The number in normal brackets is the index of the string we want. So the console would print to us: Are. If we wanted to modify one of our elements, we would do something similar to defining a variable:
The console is being told to define the element in index 2 of array $array and set it to be "Awesome". Our array now reads Arrays, Are and Awesome. In an almost identical way, we can delete an element or the entire array with unset:
Now our array contains only Arrays and Are. To delete our entire array we would type:
Arrays are wonderful in the world of PHP because they act like a list that can contain anything from the names of all the people to enter your server to the numbers from 1 to one million. The possibilities are simply endless.
Lets say we want to add something systematically to the end of our array. That is what array_push is for. It is actually a function (you'll learn about this in part 3) that adds something to the end of our array. Lets say we didn't delete our array and we still only have Arrays and Are as part of our array. Now we want to add amazing to the end of our array. Watch:
Array_push takes 2 arguments, which array the string will be pushed to and the string that will be puched. These are what you see after array_push. So now our array $array reads Arrays Are Amazing. Using array_push is an easy way to add some new strings to the end of our array.
For now, I suggest running this code independently of pocketmine. Use the run feature of Notepad++ instead and replace our console commands with print. Print will follow the same format, but NOT HAVE THE PERHENTHESES.
Lesson 1: Functions: Part A
For me, functions have been and still are a bit tricky. They can be used to easily modify strings and arrays without having to go through a lot of unnecessary coding. Lets see how they work:
As you already know, our $function on the first line is a variable, denoted by the $ and defined by abc. Now $edit is defined by a version of $function that has been modified by a function, strlen. Strlen is short for 'string length' which will save to our $edit the length of $function (abc.) In this way, $edit now stores the number 3. Now, on the next line, our console is simply printing $edit, or 3. See how this can be somewhat complicated? If not, you soon will.
We have several other functions we use in PHP, including strtoupper and strtolower. These guys work in a similar way--they modify a string. Strtoupper simply makes all of the characters in the variable uppercase. Strtolower similarly makes all characters lowercase.
As you can see, these two other functions will work in an identical way to our previous one. Our console will print on two seperate lines:
ABC
abc
This can be very helpful when trying to limit caps spam in chat.
Lesson 2: Basic Object-Oriented-Programming (OOP.)
This is the hardest part of programming in PHP you have yet encountered. Oh man, it's gonna be a real pain in the but to type this all out... Anyway, OOP is the way that objects in your server will interact with each other. All of these objects belong to a class. This class is a group of objects that share properties. It looks like this:
The first line is letting our server know that it has a class, Pig. in the { and }, you will find the properties that all of these pigs have. They all breathe, drop pork and have a name. These properties look eerily similar to variables and can contain a boolean, value or string. This is optional, as you can see with $name. All pigs have a name, but not all of there names are the same, which you will soon learn to program. Now that we have our class, we can create an actual object, or in this case, pig. An object is a thing that contains properties. To create our pig, we follow this format:
The $Piggy at the beginning of our code is the actual name of the object. Quite unoriginally, our pig is named Piggy. To create $Piggy we need an = and 'new Pig' afterwards to let the code know that Piggy is a new Pig. The last (Piggy) is going to be used in a moment.
Now for our constructor:
This constructor is called each time a new object is made. It delivers individual properties to an object. The properties we want given to each new object are delivered in this way. This time, $name is being delivered. Our public function __construct is used to let the code know that this is our constructor function. The ($name) is the property that will be assigned a value, in this case, $Piggy will recieve the name 'Piggy.' The indented $this->name = $name; below is acting as a directory. It is telling our plugin to go from where it is now to the stuff in perentheses above where we defined Piggy and setting what we find there to $Piggy's $name. This is one of the most complicated parts of PHP. If you don't understand it, I suggest you visit this: http://www.codecademy.com/courses/web-beginner-en-ZQQ64/0/1?curriculum_id=5124ef4c78d510dd89003eb8.
Lesson 3: Intermediate OOP
Now that we know how to find and assign properties to our objects, let's make our objects actually do something. We'll attatch functions to our objects. A function tied to an object is called a method. Now, for our first method, we will have Piggy talk. Watch:
Similar to our __construct function (actually a method ), this function can be called, but not when a new object is created. Instead of being called __construct, this method is called talk(). When this function is called, the object will return "I'm a talking pig!" If we wanted our pig to say his name:
Notice how we added the $this->name in there. This is also directing the object to state its unique name. Make sure that .$this->name. is outside of quotation marks and within periods. Now let's call these methods to Piggy.
(Echo is similar to print, but we will use it to call the name() method on Piggy.) The $Piggy-> is letting the plugin know who is calling the method. The name() is the method being called onto that object. $this and -> can also be used on our server to run specific aspects of our server. For example:
Would be the same as if I were to enter '/give RealSmileyguy 1 1' into the console. It would give my player 1 stone block. The arrows are simply saying where the method is and what to do with it. You can also use the arrow notation to guide the plugin to use built in functions. For example, $this->api->time->set(0); will set the time to 0. For more information, see the api documentation at the bottom of this post.
Good luck!
Q: My code won't work! It keeps saying syntax error!
A: This simply means you forgot a simple piece of punctuation. Check for missing brackets, semicolons, spaces or other issues.
Q: My plugin keeps saying that it is outdated and can possibly crash or corrupt the server!
A: This has to do with the header and the api version. This means that the plugin may not work. However, some plugins are timeless and will be able to work with any version of Pocketmine-MP. I would still run it. It should still be okay.
Q: How do I load a plugin?
A: Drag your saved plugin (.php or .pmf only) into the plugins directory in your Pocketmine-MP server.
Helpful links:
API documentation: https://github.com/PocketMine/PocketMine-MP/wiki/API-documentation
Plugin examples: https://github.com/PocketMine/PocketMine-MP/wiki/Plugin-examples
Thanks to codeacademy.com for teaching me PHP and as the basis for this tutorial!
I will update this tutorial frequently adding more parts and lessons the longer I go. Thanks for reading!
Java/C/C++ dev, server owner and hacker extraordinare
Java/C/C++ dev, server owner and hacker extraordinare
Java/C/C++ dev, server owner and hacker extraordinare
<?php
/*
__PocketMine Plugin__
name=First Plugin.php
description=First Plugin
version=0.0.1
author=Ryan
class=First Plugin
apiversion=7
/*
class First Plugin{
private $api, $server, $config;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
}
if ($x = 3) {
console ("Our variable x is equal to 3!");
} else {
console ("Our variable x is not equal to 3!);
}
}
Please give me a working version of it.
Thanks,
Ryan:) :| :/
Lead developer of Dragonet!
Check out my game, Adventuria!
Dev of (IMO the best server ever) TwilightGamez!
This looks pretty good, except for the fact that you forgot to define $x. Try adding $x = 3 above
if ($x = 3) {
console ("Our variable x is equal to 3!");
} else {
console ("Our variable x is not equal to 3!);
}
}
But below the previous code. Great job! So nice to see an interest in PHP coding!
I too am still learning PHP and am still shaky with $this->. I would love to see some of your videos and will post a link to them in this forum as soon as I view them. I also am trying to have the readers fully understand the basics of PHP before delving into summoning. Thanks for the awesome feedback!
Java/C/C++ dev, server owner and hacker extraordinare
Thanks and how do you run the php file out of PocletMine in Notpad++?
Lead developer of Dragonet!
Check out my game, Adventuria!
Dev of (IMO the best server ever) TwilightGamez!
for anyone who's learning PLEASE learn PHP and don't go on Youtube and look up how to do it.
I'm not quite sure but i believe it is somewhere under the toolbar. Thanks to everyone for the positive feedback!
Java/C/C++ dev, server owner and hacker extraordinare
<?php
/*
__PocketMine Plugin__
name=HBScare
version=1.3.3
author=themayorofspace
class=HBScare
apiversion=9
*/
class ExamplePlugin implements Plugin{
private $api;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
}
public function init(){
$this->api->console->register("hb", "This command summons Herobrine", array($this, "handleCommand"));
}
public function __destruct(){
}
public function handleCommand($cmd, $arg, $params){
switch($cmd){
case "hb":
console("Herobrine has joined the game");
console("Herobrine: I AM HERE. I AM WATCHING. ALWAYS.");
break;
case "hbscare":
$user = strtolower(array_shift($params[0]));
$player = $this->server->api->player->get($user);
console("Herobrine: I have taken \"$player\". Better watch your back.");
break;
}
}
}
?>
I also have one more question: how would I make a plugin that renders the player invisible? Or is that extremely advanced?
Thanks for your help!
here
$this->api->console->register("hbscare", "'Text here", array($this, "handleCommand"));
Also: are you using quotation marks or double apostrophes? One more thing, your text will only be printed to the console. Is this what you want? Other than this, looks pretty good!
Java/C/C++ dev, server owner and hacker extraordinare
Java/C/C++ dev, server owner and hacker extraordinare
-themayorofspace