Making and calling new methods

Let's create a new method that will broadcast a message to the server. The following diagram labels the various parts of a method in case you are not familiar with them:

Making and calling new methods

Create a new method named broadcastToServer. We will place it in the MyFirstBukkitPlugin class under the onEnable method. We only want to call this method from inside the MyFirstBukkitPlugin class so that the access modifier will be private. If you want to call this method from other classes in the plugin, you can remove the modifier or change it to public. The method will not return anything and thus will have a return type of void. Finally, the method will have one parameter, namely a string named msg. After creating this second method, your class will look like the following code:

public class MyFirstBukkitPlugin extends JavaPlugin {
  @Override
  public void onEnable() {

  }

  private void broadcastToServer(String msg) {

  }
}

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

We will write the code within the body of the new method to accomplish its task. We want to broadcast a message to the server. We could call the getServer method on the plugin. However, for convenience, the Bukkit class contains a number of server methods in a static context. You may have come across the methods that we need when you were going through the Bukkit class of the API in the previous chapter; if you have not stumbled upon them, browse through the methods in the Bukkit class at https://hub.spigotmc.org/javadocs/spigot/index.html?org/bukkit/Bukkit.html until you find the broadcastMessage(String message) method. We will call the broadcastMessage method from our own broadcastToServer method. In the IDE, type Bukkit, which is the name of the class, to indicate that you will be accessing the Bukkit class from a static context. Continue by typing a period (.) in order to call a method from that class. You will see that a list of available methods will appear, and we can simply scroll through them and choose the one that we want. This is shown in the following screenshot:

Making and calling new methods

Click to select the broadcastMessage method. The API documentation for the method will be displayed. Note that to the right of the method, it says int. This informs us that this method returns an integer type value. When you click on the See Also: link, as shown in the preceding screenshot, the documentation will tell us that the number that is returned is the number of players that the message was sent to. We don't really care about this number. Therefore, we will not assign it to a variable.

After selecting the method from the list, the IDE fills the parameters with variables that it believes we will use. In this case, it should use msg as the parameter. If not, simply type msg as parameter in broadcastMessage method This completes the broadcast method. Now, we can call it from the onEnable method. We will pass the Hello World! string as an argument.

Adding this line of code will result in the class containing the following code:

public class MyFirstBukkitPlugin extends JavaPlugin {
  @Override
  public void onEnable() {
    broadcastToServer("Hello World!");
  }

    /**
    * Sends a message to everyone on the server
    *
    * @param msg the message to send
    */
  private void broadcastToServer(String msg) {
    Bukkit.broadcastMessage(msg);
  }
}

If we test this plugin, then it will print Hello World! once it is enabled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.35.148