Hey, I need a bit of assistance. Been working on a plugin all week. What I'm trying to do is make a /welcomeback command that welcomes back the most recent player to join. I'm using a listener class to identify this player, but I have no idea how to take their name and put it into a broadcast. Any help would be appreciated.
@NukeDude If you store the player's instance, use Player#getName() to get their name, and just add that to the broadcasting message. If you store the player's UUID, use Bukkit.getOfflinePlayer(UUID) to get the player's instance, and then use getName to get their name.
Sorry, I meant use their name in a broadcast command. I know how to broadcast their name, but I want players to be the ones triggering it.
Little late to the party on this one but if you haven't already done this... On the player join event, store the player in a variable. In the related command, reference that variable for the broadcast. Example code Code: public class Welcomer extends JavaPlugin implements Listener { private Player lastPlayer; @Override public void onEnable(){ Bukkit.getPluginManager().registerEvents(this, this); } @EventHandler public void onPlayerJoin(PlayerJoinEvent event){ lastPlayer = event.getPlayer(); Bukkit.getScheduler().runTaskLater(this, new Runnable() { @Override public void run() { lastPlayer = null; } }, 10 * 20); // 10 Seconds } @Override public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (sender instanceof Player) { Player p = (Player) sender; if(commandLabel.equalsIgnoreCase("welcome") && lastPlayer != null){ Bukkit.getServer().broadcastMessage(p.getDisplayName() + " says, 'Welcome back, " + lastPlayer.getDisplayName() + "'"); } } return false; } }
Thank you so much! Fortunately, I solved this yesterday by sheer luck. Either way, this method is a lot more efficient. And better yet, this lets me do it all in one class! Upon further examination, that code you sent doesn't seem to be working for me. When a player issues the command it doesn't broadcast anything. Gonna troubleshoot. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
You have to set the welcome command in the plugin.yml if you haven't already! If you need help with this let me know Typically I use CommandExecutors now, but for short/quick results this'll do