Solved Tried CommandExecutor, still not working.

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, May 26, 2018.

  1. Ok so i have a problem where i do a command and it just says usage: {usage message for /report}
    i tried commandexecutor but same thing except this time i can do xenos:alert or xenos:discord but it still just shows me the usage...
    Any help?
    {if youre confused read the classes}

    ALSO: the main class is in one package and the other 3 are in a another package!

    Code (Text):
    package net.thesilentpro.org;

    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;
    import net.thesilentpro.org.commands.Alert;
    import net.thesilentpro.org.commands.Discord;
    import net.thesilentpro.org.commands.Report;

    public class Main extends JavaPlugin {
     
        public void onEnable() {
            getCommand("alert").setExecutor(new Alert());
            getCommand("discord").setExecutor(new Discord());
            getCommand("report").setExecutor(new Report());
        }
     
        public void onDisable() {
            getLogger().warning(ChatColor.RED + "Plugin is shutting down! If this is an error please contact me on discord >> TheSilentPro#9920");
        }
     
    }
     

    Code (Text):
    package net.thesilentpro.org.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;

    public class Alert implements CommandExecutor {
     
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
            if (cmd.getName().equalsIgnoreCase("alert")) {
                if (!sender.hasPermission("admin.alert")) {
                    if (args.length < 2) {
                        sender.sendMessage(ChatColor.RED + "[Xenos] " + ChatColor.GRAY + "/alert <message>");
                    }else {
                        Bukkit.getServer().broadcastMessage(ChatColor.GRAY + "[" + ChatColor.RED + "ALERT" + ChatColor.GRAY + "] " + ChatColor.RED + args[0].split(" "));
                    }
                }
            }    
        return true;
    }
     
    }
     

    Code (Text):
    package net.thesilentpro.org.commands;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Discord implements CommandExecutor {
     
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
            if (cmd.getName().equalsIgnoreCase("discord")) {
                if (sender instanceof Player) {
                    Player p = (Player) sender;
                    p.sendMessage(ChatColor.BLUE + "Discord: " + ChatColor.YELLOW + "https://discord.gg/pzTTXQx");
                }
            }
            return true;
        }


    }
     

    Code (Text):
    package net.thesilentpro.org.commands;

    import static org.bukkit.ChatColor.BOLD;
    import static org.bukkit.ChatColor.GRAY;
    import static org.bukkit.ChatColor.RED;

    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;

    public class Report implements CommandExecutor {
     
        public static String prefix = GRAY + "[" + RED + BOLD + "Report" + GRAY + "] " + RED;
     
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
            if (cmd.getName().equalsIgnoreCase("report")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(prefix + "Only players can use this command!");
                return true;
            }
            if (args.length >= 2) {
                Player target = sender.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(prefix + ChatColor.RED + " Player " + args[0] + " not found!");
                }else {
                    String reason = "";
                    int x = 0;
                    for (String a : args) {
                        if (x == 0) {
                            x++;
                            continue;
                        }
                        reason = reason + " " + a;
                    }
                    reason = reason.trim();
                    sender.sendMessage(prefix + ChatColor.GREEN + "Report sent!");
                    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                        if (p.hasPermission("admin.reports.see")) {
                            p.sendMessage(prefix + ChatColor.RED + ChatColor.BOLD + sender.getName() + " has reported " + target.getName() + " for " + ChatColor.DARK_RED + reason);
                        }
                    }
                }
            }else {
                sender.sendMessage(prefix + "Usage: /report <player> <reason>");
            }
            return true;
            }
            return true;
        }
     
    }
     

    Code (Text):
    name: Xenos
    main: net.thesilentpro.org.Main
    version: 1.0
    commands:
      alert:
        usage: /<command> <message>
        description: Alert the server!
      discord:
        usage: /<command>
        description: See our discord!
      report:
        usage: /<command> <player> <reason>
        description: Report a player!
    permissions:
      admin.alert:
        default: op
        description: Access to /alert
      admin.reports.see:
        default: op
        description: See report
     
    #1 UnlimitedNinjas, May 26, 2018
    Last edited: May 31, 2018
  2. Remove the usage from plugins.yml, have only description if thats your issue.
     
  3. That fixed /discord but what about /alert? {it didnt fix /alert}
     
  4. Did u remove the usage from all of them?

    Code (Text):
    name: Xenos
    main: net.thesilentpro.org.Main
    version: 1.0
    commands:
      alert:
        description: Alert the server!
      discord:
        description: See our discord!
      report:
        description: Report a player!
     
  5. Why do you check if the sender doesn't have the permission?

    change this to return false; and try again
     
  6. yes i did that
     
  7. what class is that?
     
  8. It's Alert.java.

    Spigot has a very sensitive CommandExecutor, change the boolean in every CommandExecutor to false
     
  9. Try this onCommand Method if it still don't work

    Code (Java):
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender.hasPermission("admin.alert")) {
                if(args.length > 0) {
                    String message = "";
                    for(int i = 0; i < args.length; i++) message = message + args[i] + " ";
                    Bukkit.getServer().broadcastMessage("§7[§4ALERT§7] " + message);
                } else sender.sendMessage("§4[Xenos] /alert <message>");
            }
            return false;
        }
     
  10. You should be using this :)
     
  11. works on /xenos:alert but it shows the announcement and the usage..
    and /alert doesnt work
     
  12. still it is not working
     
  13. Please all of you who are trying to help stop. This guy should first learn Java and then try to make a plugin!
     
  14. You also might want to import the right color. This is wrong "import net.md_5.bungee.api.ChatColor;"
     
  15. There is a lot of things wrong with this plugin... If they don't have permission let them do it anyway??
     
  16. Wym? It doesnt let them if they dont
     
  17. Please answer, I watched a tutorial and its still not working
     
  18. Learn Java and then try again thx
     
  19. If that's all you're going to post, don't post at all.

    If a usage message is returned for a command, the executor is returning false (this prompts the usage message to display to the user). But you're missing a few things. In your Alert class, you're checking if the player does NOT have permission, and then executing the code. That sounds like a design flaw. In the same class, you're checking if the arguments are less than 2 to send the usage message, but you only need 1 argument. So if you type /alert hello, it will always give you that usage prompt, because there's only 1 argument, but you're checking for 2. It would be worth trying to understand Java a little more, especially since these are extremely entry level mistakes.
     
    • Like Like x 1