Solved How to se If Arg[0] if an Online Player in an onCommand

Discussion in 'Plugin Development' started by mkezar, Jul 24, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    mkezar

    Hi! I am working on a plugin that when a player types /punch, it will say: "Sender has punched Punchee." I have it so when I type /punch, it says "Craftplayer=(Player) has punched Whatevertheytyped." I really want to fix the Craftplayer=(player) part but I dont know how. But my main problem is if the arg[0] is an Online Player. How would I do that? Here is my code so far:
    Code:
    package plugins.mkezar;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class UppercutStaff extends JavaPlugin implements Listener {
       
        public void onEnable() {
             Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public boolean onCommand (CommandSender sender, Command command, String commandLabel, String[] args){
            Player sender1 = (Player)sender;
            if(commandLabel.equalsIgnoreCase("punch")){
                if(args[0].equalsIgnoreCase(Bukkit.getOnlinePlayers().toString())) {
                    Bukkit.broadcastMessage(sender1.toString() + "Has Punched " + args[0]);
                }
            }
            return false;
           
        }
    
    }
    Please help! :( (Maybe even help with the CraftPlayer=(player) too! :p)
     
  2. Offline

    sam_0208

    try using player.getName() instead for the chat problem and for online check cast args[0] to a player first:
    Code:
    Player target = Bukkit.getServer().getPlayer(args[0])
    and check if the player is null
    Code:
    if (target==null){
    //target is offline
    }
    
     
  3. Offline

    WPM

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("punch")){
                if(args.length == 0){
                    sender.sendMessage("Usage: /punch <Player>");
                    return true;
                }
                Player target = Bukkit.getPlayer(args[0]);
                if(target == null){
                    sender.sendMessage(args[0] + " is not online!");
                    return true;
                }
                Bukkit.broadcastMessage(sender.getName() + " has punched " + target.getName());
            }
            return false;
        }
    }
    I am feeling really happy today so I decided to spoonfeed you :3
     
  4. Offline

    mkezar

    wow... That means a lot... It all makes sense now! you crate a method of target, if its null theyre not online and if it doesnt equal null then it means theyre online! Spoonfeeding actually helps me learn for some reason. I see how to do it and remember it for future times! Thank you kind sir!
     
  5. Offline

    WPM

    Perfect.
     
  6. Offline

    DoggyCode™

    @WPM Lol.. I just spoonfed this other guy, and I have 2 angry people at my door now xD They got mad cuz I did it.. So we gotta stop spoonfeeding and actually try to explain the problem instead.
     
    sam_0208 likes this.
Thread Status:
Not open for further replies.

Share This Page