Before testing the code, let's improve on the onEnable
method by implementing an if
statement. If there is only one player online, then why not say hello to that specific player? We can get a collection of all the players that are online by calling Bukkit.getOnlinePlayers
. If we wish to check whether the size of the collection is equal to 1, we can accomplish this by using an if
/else
statement. This is demonstrated in the following code:
if (Bukkit.getOnlinePlayers().size() == 1) {//Only 1 player online //Say 'Hello' to the specific player } else { //Say 'Hello' to the Minecraft World broadcastToServer("Hello World!"); }
Within the if
statement, we will now get the first and the only object in the collection of players. Once we have it, we can continue by broadcasting Hello
along with the player's name. After completing the if
statement, the entire class will look like the following code:
package com.codisimus.myfirstbukkitplugin; importorg.bukkit.Bukkit; importorg...