Solved Commands not doing anything

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 23, 2016.

  1. Some of my commands aren't doing anything when I try to use them. There isn't a error message or anything. The only command that is working is /kick. I have no idea why only one of them is working, it's probably really obvious xD. Here is my class and plugin.yml:
    Code (Text):
    package me.MaxKrissigo.Staff;

    import java.util.ArrayList;
    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener, CommandExecutor {
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
        String serverPrefix = ChatColor.GOLD + "" + ChatColor.BOLD + "Asteroid" + ChatColor.RED + "" + ChatColor.BOLD + " PVP " + ChatColor.GOLD + "" + ChatColor.GOLD + ">> ";

    private ArrayList<Player> vanished = new ArrayList<Player>();
    private ArrayList<Player> staffchat = new ArrayList<Player>();


    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("kick")) {    
                if (args.length == 0) {
                    p.sendMessage(serverPrefix + ChatColor.GREEN + "You must specify a player to kick!");
                } else if (args.length == 1) {
                    String name = args[0];
                    Player other = Bukkit.getServer().getPlayer(args[0]);
                    if (other == null) {
                        p.sendMessage(serverPrefix + ChatColor.YELLOW + name + ChatColor.GREEN + " is not online!");
                    } else {
                        other.kickPlayer(null);
                        p.sendMessage(serverPrefix + ChatColor.GREEN + "You have kicked " + ChatColor.YELLOW + name + ChatColor.GREEN + "!");
                        for(Player allPlayers : staffchat) {
                            allPlayers.sendMessage(ChatColor.RED + "[" + ChatColor.BOLD + "" + ChatColor.GOLD + "STAFF" + ChatColor.RED + "] " + ChatColor.WHITE + p.getName() + ChatColor.GREEN + " has kicked " + ChatColor.YELLOW  + name + ChatColor.GREEN + "!");
                        }
                    }
                } else {

                    StringBuilder buffer = new StringBuilder();

                    for(int i = 1; i < args.length; i++)
                    {
                        buffer.append(' ').append(args[i]);
                    }
                   
                    String name = args[0];
                    Player other = Bukkit.getServer().getPlayer(args[0]);
                    if (other == null) {
                        p.sendMessage(serverPrefix + ChatColor.YELLOW + name + ChatColor.GREEN + " is not online!");
                    } else {
                        other.kickPlayer("" + buffer);
                        p.sendMessage(serverPrefix + ChatColor.GREEN + "You have kicked " + ChatColor.YELLOW + name + ChatColor.GREEN + " for" + buffer + "!");
                        for(Player allPlayers : staffchat) {
                            allPlayers.sendMessage(ChatColor.RED + "[" + ChatColor.BOLD + "" + ChatColor.GOLD + "STAFF" + ChatColor.RED + "] " + ChatColor.WHITE + p.getName() + ChatColor.GREEN + " has kicked " + ChatColor.YELLOW  + name + ChatColor.GREEN + " for" + buffer + "!");
                        }
                    }
            }
            if (cmd.getName().equalsIgnoreCase("ban")) {

                if (args.length == 0) {
                    p.sendMessage(serverPrefix + ChatColor.GREEN + "You must specify a player to ban!");
                } else if (args.length == 1) {
                    String name = args[0];
                    OfflinePlayer player = Bukkit.getOfflinePlayer(args[0]);
                    if (player.hasPlayedBefore()) {
                        player.setBanned(true);
                        p.sendMessage(serverPrefix + ChatColor.GREEN + "You have banned " + ChatColor.GOLD + name);
                            for(Player allPlayers : staffchat) {
                                allPlayers.sendMessage(ChatColor.RED + "[" + ChatColor.BOLD + "" + ChatColor.GOLD + "STAFF" + ChatColor.RED + "] " + ChatColor.WHITE + p.getName() + ChatColor.GREEN + " has banned " + ChatColor.GOLD  + name);
                            }
                    } else {
                        p.sendMessage(serverPrefix + ChatColor.GOLD + name + ChatColor.GREEN + " has never joined the server!");
                    }
                }
            }
            if (cmd.getName().equalsIgnoreCase("staffchat")) {
                if (args.length == 0) {
                    if (staffchat.contains(p)) {
                        staffchat.remove(p);
                        p.sendMessage(serverPrefix + ChatColor.GREEN + "Staff chat has been disabled!");
                    } else {
                        staffchat.add(p);              
                        p.sendMessage(serverPrefix + ChatColor.GREEN + "Staff chat has been enabled!");
                        }
                }
                   
            }
            if (cmd.getName().equalsIgnoreCase("vanish")) {
                   
                    if (!vanished.contains(p)) {
                            for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                                    pl.hidePlayer(p);
                            }
                            vanished.add(p);
                            p.sendMessage(ChatColor.GREEN + "You have been vanished!");
                    } else {
                            for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                                    pl.showPlayer(p);
                            }
                            vanished.remove(p);
                            p.sendMessage(ChatColor.GREEN + "You have been unvanished!");
                    }
            }
            }
            return true;
    }
    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent e) {
        Player p = (Player) e.getPlayer();
        String message = e.getMessage();
        if (staffchat.contains(p)) {
            e.setCancelled(true);
            for(Player allPlayers : staffchat) {
                allPlayers.sendMessage(ChatColor.RED + "[" + ChatColor.BOLD + "" + ChatColor.GOLD + "STAFF" + ChatColor.RED + "] " + ChatColor.WHITE + p.getName() + ": " + message);
            }
           
        }
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
        if (e.getPlayer().hasPermission("staff.vanish.show")) {
            for (Player p : vanished) {
                p.showPlayer(e.getPlayer());
            }
        }
            for (Player p : vanished) {
                e.setJoinMessage(null);
                    e.getPlayer().hidePlayer(p);
            }
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e) {
        for (Player p : vanished) {
            e.setQuitMessage(null);
                e.getPlayer().hidePlayer(p);
        }
    }
    }
    Code (Text):
    name: Staff
    version: 1.0
    author: MaxKrissigo
    main: me.MaxKrissigo.Staff.Main

    commands:
      staffchat:
        description: Enables or disables staff chat.
        permission: staff.staffchat
      vanish:
        description: Enables of disabled vanish.
        permission: staff.vanish
      ban:
        description: Bans a player.
        permission: staff.ban
      kick:
        description: Kicks a player.
        permission: staff.kick

    permissions:
      staff.staffchat:
        description: Gives access to /staffchat.
        default: op
      staff.vanish:
        description: Gives access to /vanish.
        default: op
      staff.vanish.show:
        description: Allows players to see vanished players.
        default: op
      staff.ban:
        description: Gives access to /ban.
        default: op
      staff.kick:
        description: Gives access to /kick.
        default: op
     
  2. In you onEnable method set the executors for each of you command example:
    (If i have 1 command named "Boop" then i would write the following in my onEnable method)
    Code (Text):
    getCommand("Boop").setExecutor(new BoopCmd());

    //Make sure your giving it the right class and command label //for each command!
     
  3. So do that line of code for each command in your plugin.yml!
     
  4. My commands are in the same class as my Main class, so this shouldn't make a difference. At least I don't think it will.
     
  5. Nope you still need to register them in the onEnable i thought the samething when i started but once you add "getCommand("command name").setExecutor(<Command Class>) ;" everything will be registered and good to go (Don't forget to do it for each command though)! :)
     
  6. Looks like you've messed up all your brackets.

    @JKMODz you don't have to setExecutor if the command(s) is in your main class.
     
    • Like Like x 1
  7. Hmm, didn't end up doing anything when I registered the commands.
     
  8. Here this should help this is a example plugin:
    Code (Text):
    package XZot1K.plugins.factory;

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

    public class ExamplePlugin extends JavaPlugin implements CommandExecutor{
       
        @Override
        public void onEnable() {
            getCommand("boop").setExecutor(this);
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("boop")) {
                //Do Something...
            }
            return false;
        }
       
    }
     
     
  9. For one thing, you should use "else if". At the moment, your code goes "Is it /kick? Yes. Do that code. Is it /ban? No. Is it..." which is inefficient. Also your brackets are spaced out weird, though that may just have something to do with pasting it on the forum. Anyway, sorry for the nitpicking.

    As for the problem you're having, Edit: Ninja'd by JKMODz
     
  10. I stuffed up the ordering of my brackets. Thanks liveinacupboard for pointing that out. I simply just cleaned my code a bit and made sure all of the brackets made sense if you wanted to know how I solved it.
     
    • Like Like x 1
    • Friendly Friendly x 1