- Building Minecraft Server Modifications
- Cody M. Sommer
- 545字
- 2021-08-04 10:09:53
Making and calling new methods
Let's create a new method which will broadcast a message to the server. The following diagram labels various parts of a method in case you are not familiar with them:

Create a new method named broadcastToServer
. We will place it within our MyFirstBukkitPlugin
class under the onEnable()
method. We only want to call this method from inside the MyFirstBukkitPlugin
class so the access modifier will be private
. If we want to call this method from other classes in our plugin we 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, a string named msg
. After creating this second method, your class will look similar to the following code:
public class MyFirstBukkitPlugin extends JavaPlugin { @Override public void onEnable() { } private void broadcastToServer(String msg) { } }
We will write the code within the body of our new method to accomplish its task. We want to broadcast a message to the server. We could call the getServer()
method on our plugin. However, for convenience, the Bukkit
class contains a number of the server methods in a static context. You may have seen the methods we need when you were looking through the Bukkit
class of the API during the previous chapter; if not, browse through the methods in the Bukkit
class at http://jd.bukkit.org/rb/doxygen/db/dc0/classorg_1_1bukkit_1_1Bukkit.html to find the broadcastMessage(String message)
method. We will call the broadcastMessage
method from our own broadcastToServer
method. In your IDE, type Bukkit
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 notice that a list of available methods will appear and we can simply scroll through them and choose the one we want. This is shown in the following screenshot:

Click to select the broadcastMessage
method, the API documentation for the method will be displayed. You may notice that to the right of the method it says int. This informs us that this method returns an integer
type value. If we click on the See Also: link as shown in the 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 so 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 place msg
as the parameter. If not, simply type msg
in yourself. This completes our broadcast method so now we can call it from our onEnable()
method. We will pass the string Hello World!
as an argument.
Adding the following line of code will result in our 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.