Expanding your code

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 ...

Get Building Minecraft Server Modifications - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.