Commands not registering

Discussion in 'Plugin Development' started by Xp10d3, Jan 24, 2020.

  1. Offline

    Xp10d3

    *sigh* This feels really stupid for someone who is always using commands. But anyways, I have my commands registered in my Main Class and for some reason getting a null error for my command. I have it all registered correctly in my plugin.yml, but for some reason I still get that null error. I don't know what's going on, since I've tried registering the command (only one command with a lot of sub commands) in my main class AND my commands class.

    Commands.java:
    Code:
    package play.corelia.online;
    
    import java.util.List;
    
    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.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor {
     
        public Commands(Core core) {
            this.core = core;
        }
        /*
        protected abstract String getName();
        public final void register(Core core) {
            core.getCommand(getName()).setExecutor(this);
        }
        */
        private Core core;
     
        //FileConfiguration config = core.getConfig();
     
        @SuppressWarnings("unlikely-arg-type")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to send commands!");
            }
            Player player = (Player) sender;
            ConfigurationSection targetPlugin = core.config.getConfigurationSection("location.world");
            for (String key : targetPlugin.getKeys(false)) {
                List<String> targetWorld = targetPlugin.getStringList(key); 
                if (!player.hasPermission("dp.bypasscmd") && targetWorld.contains(player.getWorld())) {
                    if (cmd.getName().equalsIgnoreCase("dp") && player.hasPermission("dp.all")) {
                        if (args.length == 1) {
                            if (args[0] == "disableall" && player.hasPermission("dp.disableall")) {
                                Bukkit.getServer().getPluginManager().disablePlugins();
                                player.sendMessage(ChatColor.GREEN + "Successfully disabled all plugins!");
                            } else if (!player.hasPermission("dp.disableall")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.disableall! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            } else if (args[0] == "servreload" && player.hasPermission("dp.enableall")) {
                                Bukkit.getServer().reload();
                                player.sendMessage(ChatColor.GREEN + "Successfully reloaded the server! All plugins should be reloaded.");
                            } else if (!player.hasPermission("dp.enableall")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.enableall! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            } else if (args[0] == "help" && player.hasPermission("dp.help")) {
                                player.sendMessage(
                                        ChatColor.DARK_GREEN + "|COMMANDS| \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp disableall | " + ChatColor.RESET + ChatColor.AQUA + "Disables all plugins. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp servreload | " + ChatColor.RESET + ChatColor.AQUA + "Reloads the server/enables all plugins. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp help | " + ChatColor.RESET + ChatColor.AQUA + "Shows the help menu. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp permissions | " + ChatColor.RESET + ChatColor.AQUA + "Shows the permissions for each command. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp reload | " + ChatColor.RESET + ChatColor.AQUA + "Reloads the config."
                                        );
                            } else if (!player.hasPermission("dp.help")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.help! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            } else if (args[0] == "permissions" && player.hasPermission("dp.permissions")) {
                                player.sendMessage(
                                        ChatColor.DARK_GREEN + "|PERMISSIONS \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.all | " + ChatColor.RESET + ChatColor.AQUA + "Gives access to all commands. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.disableall | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to disabling all plugins. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.servreload | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to reload the server or enable all plugins. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.help | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to the help menu. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.permissions | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to the permissions menu." +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.reload | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to the reload the config."
                                        );
                            } else if (!player.hasPermission("dp.permissions")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.permissions! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            } else if (args[0] == "reload" && player.hasPermission("dp.reload")) {
                                core.reloadConfig();
                                player.sendMessage(ChatColor.GREEN + "Successfully reloaded the config!");
                            } else if (!player.hasPermission("dp.reload")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.reload! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            }
                        } else {
                            player.sendMessage(ChatColor.RED + "Incorrect usage! Do /dp help to see the commands.");
                        }
                    } else if (!player.hasPermission("dp.all")) {
                        player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.all! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                    }
                } else if (!player.hasPermission("dp.bypasscmd")){
                    player.sendMessage(ChatColor.RED + "You don't have permission to talk in this world! Contact Administrators if you think this is a bug.");
                }
            }
            return false;
        }
    
    }
    
    Core.java:
    Code:
    package play.corelia.online;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.function.BiPredicate;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerPortalEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredListener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
     
        // Get the value "config"
        FileConfiguration config = getConfig();
     
        public void onEnable() {
            config = getConfig();
            this.saveDefaultConfig();
            loadConfig();
            System.out.println("Config: " + config);
            System.out.println(Bukkit.getPluginCommand("dp") == null);
            this.getCommand("dp").setExecutor(new Commands(this));
        }
     
        public void loadConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
     
        @SuppressWarnings("unchecked")
        static <Type extends Event> void filter(Class<Type> type, BiPredicate<Plugin, Type> filter) {
              HandlerList handler;
            try {
                handler = (HandlerList) type.getDeclaredMethod("getHandlerList").invoke(null);
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
              Arrays.asList(handler.getRegisteredListeners()).forEach(listener -> {
                handler.unregister(listener);
                handler.register(new RegisteredListener(listener.getListener(), ($, event) -> {
                  if (filter.test(listener.getPlugin(), (Type) event)) listener.callEvent(event);
                }, listener.getPriority(), listener.getPlugin(), false));
              });
        }
     
        @SuppressWarnings("unlikely-arg-type")
        public void supress() {
            ConfigurationSection targetPlugin = config.getConfigurationSection("location.world");
            for (String key : targetPlugin.getKeys(false)) {
                List<String> targetWorld = targetPlugin.getStringList(key);
                filter(PlayerInteractEvent.class, (plugin, event) -> plugin == targetPlugin && event.getPlayer().getWorld().equals(targetWorld));
            }
        }
        public void repeat(PlayerPortalEvent event) {
            for (int i = 0; i >= 0; i++) {
                supress();
            }
        }
    }
    
    Plugin.yml:
    Code:
    name: DisablePlugin
    version: 0.1
    api-version: 1.15
    author: Xp10d3
    main: play.corelia.online.Core
    permissions:
      dp.all:
        description: Allows access to all commands.
        default: op
      dp.disableall:
        description: Allows access to disable all plugins.
        default: op
      dp.servreload:
        description: Allows access to reload the server.
        default: op
      dp.help:
        description: Allows access to view the help.
        default: op
      dp.permissions:
        description: Allows access to view the permissions for each command.
        default: op
      dp.bypasscmd:
        description: Allows access to execute commands in blocked worlds.
        default: op
      dp.reload:
        description: Allows player to reload the config.
        default: op
    commands:
      dp:
        description: The command that does all.
    
    EDIT: Wrong plugin.yml sorry. Added the correct one (I think).
     
    Last edited: Jan 24, 2020
  2. Offline

    caderapee

    @Xp10d3 and what is the error ? stack trace
     
  3. Offline

    Xp10d3

    Sorry didn't have access to error. Here it is.
    Code:
    java.lang.NullPointerException: null
    at play.corelia.online.Core.onEnable(Core.java:29) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.15.jar:git-Spigot-f39a89e-4633e6c]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [spigot-1.15.jar:git-Spigot-f39a89e-4633e6c]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot-1.15.jar:git-Spigot-f39a89e-4633e6c]
    at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:462) [spigot-1.15.jar:git-Spigot-f39a89e-4633e6c]
    at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:376) [spigot-1.15.jar:git-Spigot-f39a89e-4633e6c]
    at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:456) [spigot-1.15.jar:git-Spigot-f39a89e-4633e6c]
    at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:266) [spigot-1.15.jar:git-Spigot-f39a89e-4633e6c]
    at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:783) [spigot-1.15.jar:git-Spigot-f39a89e-4633e6c]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_212]
    
    EDIT: The error is a bit outdated since I changed my code and didn't test the most recent version of the plugin (deleted a few comments) so the line where it points to the code is incorrect, but all the same it's a null/NPE error.
     
  4. Offline

    caderapee

    @Xp10d3 try to remove one by one the line on your onEnable for see where is the error.
    The command is not the problem i think
     
  5. Offline

    Xp10d3

    Is it the plugin.yml then? Cause it says the command is null. I don't think the problem is with my core.java; it worked fine before I added the commands class and the commands.
    EDIT: Yeah doesn't look like its an issue with my Core/main class.
     
  6. Offline

    KarimAKL

    If that’s the case then we don’t know what line is throwing the exception.
     
  7. Offline

    Xp10d3

    Core.java line 26
     
  8. Offline

    caderapee

    @Xp10d3 only you can see what is the line 26
     
  9. Offline

    Xp10d3

    Sorry here:
    this.getCommand("dp").setExecutor(new Commands(this));
    Thats line 26 ^
     
  10. Offline

    caderapee

    @Xp10d3 I dun see why this line throw an error.
     
  11. Offline

    Strahan

    That's odd. For that line to throw a null, the "dp" would have to not be registered in the plugin.yml, though you show it in your post above. When the server loads, are there any other console errors? Sure there are no tabs in the plugin.yml?
     
  12. Offline

    Xp10d3

    I triple checked the console. Only errors pointing to the Core and Commands class saying there is a null error at
    Code:
    this.getCommand("dp").setExecutor(new Commands(this));
    . I've resulted to just going without the Commands class to see if there is anything to do with the Core class itself, but then there are no errors. Although there is something in that the canceling events doesn't work at all (as in the whole plugin isn't working at all/it's useless), but that's another issue that I might use another thread for.
     
  13. Online

    CraftCreeper6

    @Xp10d3
    Use WinRAR or alternative to open the jar file and check that the plugin.yml definitely contains the command. I've had issues in the past with the plugin.yml/my entire plugin not updating when I export it.
     
  14. Offline

    Xp10d3

    Sorry finally got a chance to try. The plugin.yml seems to be fine; has all the updated commands/permissions.
     

Share This Page