Solved Executing commands from console

Discussion in 'Plugin Development' started by Forword, Apr 14, 2018.

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

    Forword

    How would I execute a command from console and as a player?

    For example, for a /kill command, how would I make it from console and player?

    Although this seems very simple, I can't do it :/

    EDIT: Furthermore, how would I make console get a player?
    Such as, /kill Forword
    How would i make console detect Forword?
    Player target = Bukkit.getServer().getPlayer(args[0]); ?
     
    Last edited: Apr 14, 2018
  2. Offline

    timtower Administrator Administrator Moderator

    @Forword As developer? In that case: what do you have so far?
     
  3. Offline

    Forword

    I was just testing around of what I've seen online and here's what I got:
    Code:
    package me.forword.server.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    
    import me.forword.server.Message;
    
    public class Kill implements CommandExecutor {
    
        String prefix = ChatColor.GOLD + "Essentials> ";
        Message messages = Message.getInstance();
    
        @SuppressWarnings({ })
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      
            if (!(sender.hasPermission("kill")) || !sender.hasPermission("all")) {
                sender.sendMessage(ChatColor.RED + messages.getConfig().getString("noperms"));
                return true;
            }
            if (args.length == 0) {
                if (sender instanceof Player) {
                }
                    Player p = (Player) sender;
                    p.setHealth(0);
                    sender.sendMessage(prefix + ChatColor.GREEN + messages.getConfig().getString("killed"));
                    return true;
                }
            if (sender instanceof ConsoleCommandSender) {
          
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + messages.getConfig().getString("noplayer"));
                return true;
            }
            else if(args.length == 2) {
          
                if (sender instanceof Player) {
              
                    @SuppressWarnings("deprecation")
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage(ChatColor.RED + messages.getConfig().getString("notplayer"));
                        return true;
                    }
                    target.setHealth(0);
                    sender.sendMessage(prefix + ChatColor.GREEN + " " + messages.getConfig().getString("kill_others") + ChatColor.BOLD + ChatColor.BLUE + target.getName());
                    if (target.hasPermission("kill.view")) {
                        target.sendMessage(prefix + ChatColor.GREEN + " " + messages.getConfig().getString("killed"));
                        return true;
                    }
                    if (sender instanceof ConsoleCommandSender) {
                        @SuppressWarnings("deprecation")
                        Player target2 = Bukkit.getServer().getPlayer(args[0]);
                        if (target2 == null) {
                            sender.sendMessage(ChatColor.RED + messages.getConfig().getString("notplayer"));
                            return true;
                    }
                        target2.setHealth(0);
                        Bukkit.getConsoleSender().sendMessage(prefix + ChatColor.GREEN + " " + messages.getConfig().getString("kill_others") + ChatColor.BOLD + ChatColor.BLUE + target.getName());
                        if (target2.hasPermission("kill.view")) {
                            target2.sendMessage(prefix + ChatColor.GREEN + " " + messages.getConfig().getString("killed"));
                            return true;
                        }
                    }
                }
            }
            return true;
        }
    }
    
    and yes, I am well aware this won't work due to the fact it has a Player variable.
    I am looking for a way to set a target, as a console
    Thanks, Forword

    EDIT: Ill try coding something else
     
  4. Offline

    timtower Administrator Administrator Moderator

    Moved to plugin development.
    @Forword Why do you need to be a player when you run the command with arguments?
     
  5. Offline

    Forword

    What do you mean? I would like to run this as both Console and Player
    and I'm declaring the player variable to set Health level
     
  6. Offline

    timtower Administrator Administrator Moderator

    else if(args.length == 2) {

    if (sender instanceof Player)
    That piece makes no sense, you don't need to check for a Player anymore.
     
  7. Offline

    Forword

    Well if the args is equal to 2, I would need to check for both console and player?
    Otherwise, If console ran the command it would pick up as a player
    Am i getting this all wrong? Sorry if i am, I am not having a clue to how to send a command via console

    EDIT: Would I just have to do:
    (args.length == 0 && sender instanceof Player) {
    (args.length == 1 && sender instanceof Player) {
    (args.length == 0 && sender instanceof ConsoleCommandSender) {
    (args.length == 1 && sender instanceof ConsoleCommandSender) {
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Forword Why would it be 2? /kill <player> is an args.length of 1.
     
  9. Offline

    Forword

    Sorry, didn't realize that when coding it, anyway if I'm coding a separate kill command and I think it should function. How would i make console execute a command though? Is it the same as if a player..?

    EDIT: such as Player target = Bukkit.getServer().getPlayer(args[0]);
    Would that work? Or does it have to be something else.
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    Forword

    Why won't what I made work?

    Code:
    package me.forword.server.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    
    import me.forword.server.Message;
    
    public class Kill2 implements CommandExecutor {
    
        String prefix = ChatColor.GOLD + "Essentials> ";
        Message messages = Message.getInstance();
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (!(sender.hasPermission("kill")) || !sender.hasPermission("all")) {
                sender.sendMessage(ChatColor.RED + messages.getConfig().getString("noperms"));
                return true;
            }
            if (args.length == 0 && sender instanceof Player) {
                Player p = (Player) sender;
        
                p.setHealth(0);
                p.sendMessage(prefix + ChatColor.GREEN + messages.getConfig().getString("killed"));
                return true;
            }
            else if (args.length == 1 && sender instanceof Player) {
        
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + messages.getConfig().getString("notplayer"));
                    return true;
                }
                target.setHealth(0);
                target.sendMessage(prefix + ChatColor.GREEN + messages.getConfig().getString("killed"));
            }
            if (args.length == 0 && sender instanceof ConsoleCommandSender) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + messages.getConfig().getString("noplayer"));
                return true;
            }
            else if (args.length == 1 && sender instanceof ConsoleCommandSender) {
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayer(args[0]);
                if (target == null) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + messages.getConfig().getString("notplayer"));
                    return true;
                }
                target.setHealth(0);
                target.sendMessage(prefix + ChatColor.GREEN + messages.getConfig().getString("killed"));
                return true;
            }
            return true;
        }
    }
    
    In console it says Line 28 is wrong, although I don't see how.
    Line 28: p.setHealth(0);

    EDIT:

    Code:
    package me.forword.server.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    
    import me.forword.server.Message;
    
    public class Kill2 implements CommandExecutor {
    
        String prefix = ChatColor.GOLD + "Essentials> ";
        Message messages = Message.getInstance();
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
       
            if (!(sender.hasPermission("kill")) || !sender.hasPermission("all")) {
                sender.sendMessage(ChatColor.RED + messages.getConfig().getString("noperms"));
                return true;
            }
            if (args.length == 0 && sender instanceof Player) {
                Player p = (Player) sender;
           
                p.setHealth(0);
                p.sendMessage(prefix + ChatColor.GREEN + messages.getConfig().getString("killed"));
                return true;
            }
            if (args.length == 0 && sender instanceof ConsoleCommandSender) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + messages.getConfig().getString("noplayer"));
                return true;
            }
            else if (args.length == 1 ) {
           
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + messages.getConfig().getString("notplayer"));
                    return true;
                }
                target.setHealth(0);
                target.sendMessage(prefix + ChatColor.GREEN + messages.getConfig().getString("killed"));
            }
            return true;
        }
    }
    
    The error I get for Line 28 is:
    org.bukkit.craftbukkit.v1_8_R3.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    (I am well aware of what that error means, but can't seem to fix it)
    @timtower, is this what you were talking about? (the code above)
     
    Last edited by a moderator: Apr 14, 2018
  12. Offline

    timtower Administrator Administrator Moderator

    @Forword You don't need to check who is what when args.length == 1
     
  13. Offline

    Forword

    @timtower, if you wouldn't mind, could you make a /kill <player> sample command for me to look at :) ?
    and could you take a look at the code(2nd) I posted above
     
  14. Offline

    timtower Administrator Administrator Moderator

    @Forword 1. Don't back edit.
    2. Is the code the exact code for the error?
     
  15. Offline

    Forword

    @timtower, Just ignore all the code posted above, Is there a way you could explain to me about how to make a command executable by both Player and Console?

    EDIT: Solved the error, thanks!
     
    Last edited: Apr 14, 2018
  16. Offline

    Alabingu

    @Forword Then please mark this as solved :)
     
Thread Status:
Not open for further replies.

Share This Page