Expanding your code
Before testing, 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 an array of all the players that are online by calling Bukkit.getOnlinePlayers()
, if we wish to check if the length of the array is equal to 1, we can accomplish this with an if
/else
statement. This is shown in the following code:
if (Bukkit.getOnlinePlayers().length == 1) {//Only 1 player online //Say 'Hello' to the specific player } else { //Say 'Hello' to the Minecraft World broadcastToServer("Hello World!"); }
Within our if
statement, we will now get the first and only object in the player array. Once we have that, we can continue by broadcasting Hello
along with the player's name. After completing the if
statement your entire class will look as shown in the following code:
package com.codisimus.myfirstbukkitplugin; importorg.bukkit.Bukkit; importorg.bukkit.entity.Player; importorg...