Solved Kick with a reason

Discussion in 'Plugin Development' started by phadol2013, Jan 8, 2019.

  1. Offline

    phadol2013

    Hello to all you reading this,
    I've been trying to make a kick command that work with reason but i only found 2 tutorials wthat explain how, and yet its still have problem

    When i kick without a reason it work but when it has a random word after args[0] it has problem
    [​IMG]
    and the console error

    Reason (open)

    [12:48:29] [Server thread/INFO]: phadol2013 issued server command: /k phadol2013 nope
    [12:48:29] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'k' in plugin ADMINTOOLS v0.1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:628) ~[craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1340) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1200) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_191]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_191]
    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:696) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:371) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:651) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:555) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
    at admintool.commands.Kicks.onCommand(Kicks.java:38) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    ... 15 more


    Multiple reason (open)

    [12:49:47] [Server thread/INFO]: phadol2013 issued server command: /k phadol2013 nope nope
    [12:49:47] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'k' in plugin ADMINTOOLS v0.1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:628) ~[craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1340) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1200) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_191]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_191]
    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:696) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:371) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:651) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:555) [craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
    at admintool.commands.Kicks.onCommand(Kicks.java:38) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.12.2.jar:git-Bukkit-e60fc34]
    ... 15 more


    And my code is
    Code:
    package admintool.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.entity.Player;
    
    import admintool.Main;
    
    public class Kicks implements CommandExecutor{
      
        @SuppressWarnings("unused")
        private Main plugin;
      
        public Kicks(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("kicks").setExecutor(this);
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
          
            if(p.hasPermission("admintool.kick")) {
                if(args.length == 0) {
                    p.sendMessage(ChatColor.RED + "usage: /kicks <player> <reason>");
                }else {
                Player target = Bukkit.getServer().getPlayerExact(args[0]);
                String reason = "";
                if(target == null) {
                    p.sendMessage(ChatColor.RED + " [ADMIN TOOLS] Can't find the specify target!");
                }
              
                if(args.length > 1) {
                    for(int i = 1; 1 < args.length; ++i) {
                        reason = args[i] + " ";
                    }
                }else if(args.length == 1){
                    reason ="unspecify";
                }
              
                target.kickPlayer("You have been kicked from this server \n\n\n" + ChatColor.RED + "REASON:\n\n" + ChatColor.WHITE + reason);
              
                p.sendMessage(ChatColor.GREEN + args[0] + "has been kicked from the server");
              
                }
            }else {
                p.sendMessage(ChatColor.RED + " [ADMINTOOLS] You have no permission to use this command!");
            }
          
            return false;
        }
    
    }
    
    im sorry, but i couldnt understand the error
    Thats mainly the reason i came here
    I dont know if im doing something wrong or just plain dumb

    I dont know why the image isnt loaded but this is the link
    https://imgur.com/a/UkhwJdL

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Jan 9, 2019
  2. Offline

    KarimAKL

    @phadol2013
    1. Don't case 'sender' to a Player before checking if it's a player first.
    2. Remember to return true when you are done executing code.
    Example for point 2:
    Code:Java
    1. if(args.length == 0) {
    2. p.sendMessage(ChatColor.RED + "usage: /kicks <player> <reason>");
    3. return true; //Remember to add this, this makes sure the code is not continuing after you do that.
    4. }

    Now do the same for 'target == null', at the end of the command (even if no errors occured) etc.
     
  3. Offline

    phadol2013

    I mainly making it only for player, and thanks
    It fix the non existing/online player

    But i still confuse on the reason part
     
  4. Offline

    SavageAvocado

    1 is always less then args.length.
     
  5. Offline

    phadol2013

    Do i need to put it like this?
    Code:
    for (int i = 1; i < args.Length; i++){
         reason = args[i] + " ";
    }
    EDIT:
    I meant
    i = 2 sorry just relized i type it wrong

    Just tried it, and it work Thanks
    Sorry for wasting your time

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 9, 2019

Share This Page