Solved All my command will not work

Discussion in 'Spigot Plugin Development' started by Moose1031, Jun 22, 2019.

  1. I'm coding a plugin and when a load the plugin I get no errors. But when I run a command from my game in console it says "[13:35:41 INFO]: Moose1301 issued server command: /bc 1" and no errors when i run the command but nothing happens
    This is the /BC class oncommand
    Code (Java):
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            String prefixName = "ChatColor.RED + [BroadCast] ";
            if (p.hasPermission("BraveCore.BC")) {
                Bukkit.broadcastMessage(prefixName + args);
            }
            if (!p.hasPermission("BraveCore.BC")) {
                p.sendMessage("§cNo Permission");
            }
            return false;
    }
     
  2. Hello,

    Do you register your command? (except if it's in plugin.yml & in main class)
    Also, he permission check should be all lower case p.hasPermission("bravecore.bc").
     
  3. i have register the command in both
     
  4. Can you post some more of your code so I can try to help? For example, your whole main class, etc.
     
  5. Code (Java):
    package me.moose.BraveCore.Main;

    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import me.moose.BraveCore.*;
    public class Main extends JavaPlugin implements Listener{
        public static Main plugin;
        @Override
        public void onEnable() {
            plugin = this;
            System.out.print("Made by Moose1301");
            System.out.print("-----");
            this.getServer().getPluginManager().registerEvents(this, this);
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            new me.moose.BraveCore.Chat.PlayerChat();
        }
    }
    this is my main class
     
  6. Still very inconclusive but I would suggest registering your commands differently. Make sure you have your commands in your plugin.yml, I don't think they will work if you don't have them in there. I would suggest making separate classes for each command and listeners if you need any listeners and in the onEnable() method do something like this:

    Code (Text):
    @Override
        public void onEnable() {
            new PlayerCloseInventoryListener(this);
            new BackpackCommand(this); // Command
        }
    And in your commands / listeners:
    Code (Text):
    private EasyBackPacks plugin; // Your main class / plugin.

        public BackpackCommand(EasyBackPacks plugin) {
            this.plugin = plugin;

            plugin.getCommand("backpack").setExecutor(this);
        }
    And obviously, move your command code to your command class:
    Code (Text):
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            //code
        }
    This is some code from one of my plugins. Here's the source code if you would like more reference.
     
    • Agree Agree x 1
  7. All my commands are in different class
     
  8. Would you mind putting the code on github or posting all your classes and everything together? I can't really tell how you're doing your commands and stuff currently.
     
  9. SteelPhoenix

    Moderator

    You never set the command executor for your command. Use these:
    JavaPlugin#getCommand(String)
    PluginCommand#setExecutor(CommandExecutor)

    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        // You should check if the sender is a Player before casting
        Player p = (Player) sender;
        // You do realize that you can't use enum constants this way right?
        String prefixName = "ChatColor.RED + [BroadCast] ";
            if (p.hasPermission("BraveCore.BC")) {
                Bukkit.broadcastMessage(prefixName + args);
            }
            // You do realize that you can just use an else clause here right?
            if (!p.hasPermission("BraveCore.BC")) {
                // Don't use the '§' character, the ChatColor enum exists for a reason
                p.sendMessage("§cNo Permission");
            }
            return false;
    }
    Code (Java):
    // package names should be lowercase
    package me.moose.BraveCore.Main;

    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    // No reason for static imports
    import me.moose.BraveCore.*;
    // Main class names should be unique and descriptive
    public class Main extends JavaPlugin implements Listener{
        // Nice public static field access for no reason
        public static Main plugin;
        @Override
        public void onEnable() {
            plugin = this;
            // Bukkit already broadcasts the plugin's author when loading the plugin
            System.out.print("Made by Moose1301");
            System.out.print("-----");
            this.getServer().getPluginManager().registerEvents(this, this);
            // Why register this class twice?
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            new me.moose.BraveCore.Chat.PlayerChat();
        }
    }
     
    • Agree Agree x 1
  10. Didn't even realize that... I'm blind. Haha, hope this works for you @Moose1031. I would still recommend doing your commands and stuff differently.
     
    • Like Like x 1
  11. Glad I could help. Good luck with your future plugins and this one!