How to set executor?

Discussion in 'Spigot Plugin Development' started by zSebbenOG, May 3, 2019.

  1. Hey, I am trying to code a command in main class.
    The problem is that I don't know how to set the executor of the command (What to type to make the command work)
    Anyone got any clue?

    As you can see I tried to use "if(cmd.getName().equalsIgnoreCase("/staffbc"));"
    But that did not work.

    Thanks in advance.

    Code (Java):
    package net.Soliux.ModMSG;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class ModMSG extends JavaPlugin implements CommandExecutor {
     
        @Override
        public void onEnable() {
            getLogger().info("StaffBroadcast by Soliux > Enabled");
            //getCommand("staffbc").setExecutor(this);
        }
        @Override
        public void onDisable() {
            getLogger().info("StaffBroadcast by Soliux > Disabled");
                }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
        if(cmd.getName().equalsIgnoreCase("/staffbc"));  
     
            //Moderator starter her
            if(sender.hasPermission("Goli.ModStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§c§lMOD §c" + sender.getName() + "§7: §f" + args[1]);
                Bukkit.broadcastMessage("");
         
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
            }
        }
     
            //Senior Moderator starter her
            else if(sender.hasPermission("Goli.SrModStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§c§lSR.MOD §c" + sender.getName() + "§7: §f" + args[1]);
                Bukkit.broadcastMessage("");
         
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
         
            }
        }      
            //Admin starter her
            else if(sender.hasPermission("Goli.AdminStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§4§lADMIN §4" + sender.getName() + "§7: §f" + args[1]);
                Bukkit.broadcastMessage("");
         
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
            }
        }
     
            //Developer starter her
            else if(sender.hasPermission("Goli.DeveloperStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§9§lDEVELOPER §9" + sender.getName() + "§7: §f" + args[1]);
                Bukkit.broadcastMessage("");
     
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
            }
        }
     
            //Owner starter her
            else if(sender.hasPermission("Goli.OwnerStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§e§lOWNER §e" + sender.getName() + "§7: §b" + args[1]);
                Bukkit.broadcastMessage("");
         
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
            }
        }
     
            else {
                sender.sendMessage("§cNo permission.");
        }
            return true;
        }
    }
     
  2. Take away the /, then this should work after uncommenting the executor line.

    edit:
    Code (Text):
       if(cmd.getName().equalsIgnoreCase("/staffbc"));  
    should be:

    Code (Text):
       if(cmd.getName().equalsIgnoreCase("staffbc")){


    //code here
    }
    Your brackets are missing & it doesn't need a semi colon
     
  3. Well for me, the best would be:
    Code (Java):
    if(!cmd.getName().equalsIgnoreCase("staffbc")) return;

    //do stuff here
    BTW: The command class automatically knows you have / so if you add another the command wouldn't be /staffbc but rather //staffbc.

    Edit:

    And don't use the special character for colors, there is the Chatcolor enum for a reason.
     
    #3 Zastrix, May 3, 2019
    Last edited: May 3, 2019
  4. When I add:

    Code (Java):
    commands:
        staffbc:
    In my plugin.yml file, the plugin disappears from /plugins???
    Any idea why?

    When I remove it, it suddenly appears
     
  5. Did you full stop/start the server or just reload it? Do a full stop/start. And make sure you registered everything completely. Don't miss any command name (change all /staffbc to staffbc in the Java code of your plugin)
     
  6. I changed it from /staffbc to staffbc already
    And yes I did restart AND reload just to test
     
  7. Oh I just realized, your command is incomplete. Here an example:
    Code (YAML):
    commands:
      discordchat
    :
        permission
    : chatcord.use
        description
    : Enable discord chat
        aliases
    : dchat, chatcord
        usage
    : /<command>
     
  8. This does not affect its performance. Although, maybe I just do not see it, because I just use
    commands:
    test:
     
  9. Thanks, that worked.
    I can execute the command now.

    But I still have an issue.
    Now I'm getting an internal error when I type the command.
     
  10. Yeah a haste bin of the console and the code would be good?
     
  11. Write here ...
     
  12. not performance but it shouldn't really work. After a column YAML does expect some value (which isn't null). As you can see it has fixed the error of the plugin not starting up.
     
  13. Does it even affect the work? I have never used it ...
     
  14. Unfortunately, I have not received the console yet.
    I only have the FTP at the moment.

    How necessary is it? I understand it would be useful to see console
     
  15. Logs, please...
     
  16. I will send the code (I'll edit this msg)

    Code (Java):
    package net.Soliux.ModMSG;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class ModMSG extends JavaPlugin implements CommandExecutor {
       
        @Override
        public void onEnable() {
            getLogger().info("StaffBroadcast by Soliux > Enabled");
            //getCommand("staffbc").setExecutor(this);
        }
        @Override
        public void onDisable() {
            getLogger().info("StaffBroadcast by Soliux > Disabled");
                }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
        if(cmd.getName().equalsIgnoreCase("staffbc")) {
           
        }
       
            //Moderator starter her
            if(sender.hasPermission("Goli.ModStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§c§lMOD §c" + sender.getName() + "§7: §f" + args[1]);
                Bukkit.broadcastMessage("");
           
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
            }
        }
       
            //Senior Moderator starter her
            else if(sender.hasPermission("Goli.SrModStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§c§lSR.MOD §c" + sender.getName() + "§7: §f" + args[1]);
                Bukkit.broadcastMessage("");
           
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
           
            }
        }      
            //Admin starter her
            else if(sender.hasPermission("Goli.AdminStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§4§lADMIN §4" + sender.getName() + "§7: §f" + args[1]);
                Bukkit.broadcastMessage("");
           
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
            }
        }
       
            //Developer starter her
            else if(sender.hasPermission("Goli.DeveloperStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§9§lDEVELOPER §9" + sender.getName() + "§7: §f" + args[1]);
                Bukkit.broadcastMessage("");
       
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
            }
        }
       
            //Owner starter her
            else if(sender.hasPermission("Goli.OwnerStaffBC")) {
                Bukkit.broadcastMessage("");
                Bukkit.broadcastMessage("§e§lOWNER §e" + sender.getName() + "§7: §b" + args[1]);
                Bukkit.broadcastMessage("");
           
                if(args.length == 0) {
                    sender.sendMessage("§c§oUsage: /staffbc <Message>");
            }
        }
       
            else {
                sender.sendMessage("§cNo permission.");
        }
            return true;
        }
    }
     
  17. We literally can't know what the error is. Start a local server and run it on the server.
    Well... The command with the incomplete won't work. Maybe with not registering it on YAML it would work but not show up with tabbing the command. Either way, use that format.
     
  18. Your getCommand is commented. Uncomment it.

    Edit:

    In the if statement where you check your command, the body is empty. Even if you don't get errors, it won't work how it is coded now.
     
  19. Latest log:

    Code (Text):
    [00:38:11] [Server thread/INFO]: Soliux issued server command: /staffbc t
    [00:38:11] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'staffbc' in plugin GoliStaffBC v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_191]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_191]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_191]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at net.Soliux.ModMSG.ModMSG.onCommand(ModMSG.java:29) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
    [00:38:21] [Server thread/INFO]: Soliux issued server command: /help
    [00:38:31] [Server thread/INFO]: Soliux issued server command: /help GoliStaffBC
     
  20. Yes but I changed getCommand for if(cmd.getName().equalsIgnoreCase("staffbc")) {
    Do I have to use getCommand also?