Solved In-game reload command

Discussion in 'Plugin Development' started by KarimAKL, Mar 8, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    KarimAKL

    I've tried looking around for a few hours but everytime i find something it seems it doesn't work for me, either that or i'm doing it wrong, anyway this is my code:
    Code:
    public class CommandReload implements CommandExecutor {
     
        @SuppressWarnings("unused")
        private Main plugin;
     
        public CommandReload(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("kitpvp reload").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender.hasPermission("kitpvp.reload")) {
                if (args.length == 1) {
                    sender.sendMessage(Utils.chat("&aReloading plugin..."));
                    reloadConfig();
                    sender.sendMessage(Utils.chat("&aReload complete!"));
                } else if (args.length < 1) {
                    sender.sendMessage(Utils.chat("&cNot enough arguments! Use /kitpvp help for more help."));
                } else if (args.length > 1) {
                    sender.sendMessage(Utils.chat("&cToo many arguments! Use /kitpvp help for more help."));
                }
            } else {
                sender.sendMessage(Utils.chat("&cYou do not have permission to use this command!"));
            }
            return false;
        }
    
    }
    
    EDIT: Forgot to mention that the error is at line 25 ("reloadConfig();")
     
    Last edited by a moderator: Mar 8, 2018
  2. Online

    timtower Administrator Administrator Moderator

    @KarimAKL And what does reloadConfig do?
     
  3. Offline

    KarimAKL

    From what i understood it should reload the config file.
     
  4. Online

    timtower Administrator Administrator Moderator

    That method doesn't exist in a Commandexecutor though, so you had to implement it yourself.
    Please post your full class.
     
  5. Offline

    KarimAKL

    Full class? That was all from public class implements CommandExecutor and down.
     
  6. What is means is that reloadConfig isn't a function in CommandExecutor, meaning that you must have some custom implementation in that class, therefore you're not posting your full class.

    Sent from my SM-G903F using Tapatalk
     
  7. Offline

    KarimAKL

    That is everything in that class(other than the imports and the package) but when i hold my mouse over the reloadConfig in the class i can click: create method 'reloadConfig()'
     
  8. Online

    timtower Administrator Administrator Moderator

    @KarimAKL So it isn't compiling?
    Then you need to call reloadConfig on the plugin.
     
  9. Offline

    KarimAKL

    call reloadConfig? How do i do that? Can you either explain it or send a link that explains it? Thanks in advance. :)
     
  10. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You have it in your code right? So you know how to call functions.
    Now you need to call one on plugin
    A link doesn't explain it.
     
  11. Offline

    KarimAKL

    Would this work?
    Code:
    public class CommandReload implements CommandExecutor {
      
        @SuppressWarnings("unused")
        private Main plugin;
      
        public CommandReload(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("kitpvp reload").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender.hasPermission("kitpvp.reload")) {
                if (args.length == 1) {
                    sender.sendMessage(Utils.chat("&aReloading plugin..."));
                    reloadConfig();
                    sender.sendMessage(Utils.chat("&aReload complete!"));
                } else if (args.length < 1) {
                    sender.sendMessage(Utils.chat("&cNot enough arguments! Use /kitpvp help for more help."));
                } else if (args.length > 1) {
                    sender.sendMessage(Utils.chat("&cToo many arguments! Use /kitpvp help for more help."));
                }
            } else {
                sender.sendMessage(Utils.chat("&cYou do not have permission to use this command!"));
            }
            return false;
        }
    
        private void reloadConfig() {
            // TODO Auto-generated method stub
          
        }
    
    }
    
     
  12. Online

    timtower Administrator Administrator Moderator

    @KarimAKL The method does nothing though.
     
  13. Offline

    KarimAKL

    Yeah.. :/ Could you explain to me how you would make an in-game reload command?
     
  14. Offline

    MattTheBeast

    @KarimAKL It seems to me that you think of reloading as a command more than as a feature your plugin has. A reload command should be used to call a safe method that clear's memory, saves data and load's data. If you whant to correctly have a reloading feature in your plugin you should have structured your plugin in a way that it can safely. This is how I recommend you do it:

    In your main class

    Code:
    
        public void onDisable() {
            clearMemory();
        }
    
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            //register listeners here
            pm.registerEvents(new PlayerListener(this), this);
            startup(true);
        }
    
        public void reload() {
            clearMemory();
            startup(false);
        }
    
        private void startup(boolean init) {
          
          
            plugin = this;
            if (init) {
                config.load(); //load config here
            }
            config.reload(); //reload config here
          
        }
    
        private void clearMemory() {
          
            config.save(); //save config here
          
            plugin = null;
        }
    
    
    This way to structuring your main class is clean because instead of just bunching things off in onEnable and onDisable your calling additionnal methods that can be called in a reload method.

    Now if you whant to reload your plugin, in your command class, get your plugin, then do plugin.reload() and tada
     
    Last edited: Mar 8, 2018
  15. Offline

    KarimAKL

    Okay, i'll try this. Thanks so much. :D
     
  16. Online

    timtower Administrator Administrator Moderator

    @MattTheBeast Why the static main? 1. You never need it. 2. He already had a main instance.
     
  17. Offline

    MattTheBeast

    True, removed it.
     
  18. Offline

    KarimAKL

    I just tried this but it comes with errors.
    Here is what it looks like now:
    Finally done! Wrote all of this on mobile without any copy and paste. Damn it took a long time.
     
  19. Offline

    AdamDev

    @KarimAKL
    Hmm... I don't quite see what your trying to do. I know your trying to reload a config, but is it custom or just a normal config? If its just a normal config then thats pretty easy.
    Code:
    plugin.reloadConfig();
    
    As simple as that.

    I just don't quite see what would be the big problem
     
  20. Offline

    KarimAKL

    What do you mean ‘custom’? I just have messages with #first and then the strings. Nothing else.

    Okay i tried doing as you said but i can't even test it because everytime i do the command for it it just returns the usage of the command from the plugin.yml to me. :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 9, 2018
  21. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Code blocks please, not quote.
    Please post the Commandexecutor code
     
  22. Offline

    KarimAKL

    How do i make code block instead?
     
  23. Online

    timtower Administrator Administrator Moderator

  24. Offline

    KarimAKL

    Code:
    public class CommandHelp implements CommandExecutor {
       
        private Main plugin;
       
        public CommandHelp(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("kitpvp")) {
                    if (args[1].equalsIgnoreCase("help")) {
                        if (args.length == 1) {
                            if (sender.hasPermission("kitpvp.help")) {
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp help - Displays this info page"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp reload - Reloads the config.yml"));
                                sender.sendMessage(Utils.chat("&b- &f/broadcast <message> - Broadcasts a message to the server"));
                                sender.sendMessage(Utils.chat("&b- &f/setspawn - sets the spawnpoint"));
                                sender.sendMessage(Utils.chat("&b- &f/spawn - teleports to the spawnpoint"));
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        } else if (args.length > 1) {
                            if (sender.hasPermission("kitpvp.help")) {
                                sender.sendMessage(Utils.chat("&cToo many arguments! Correct Usage: /kitpvp help"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        } else if (args.length < 1) {
                            if (sender.hasPermission("kitpvp.help")) {
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp help - Displays this info page"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp reload - Reloads the config.yml"));
                                sender.sendMessage(Utils.chat("&b- &f/broadcast <message> - Broadcasts a message to the server"));
                                sender.sendMessage(Utils.chat("&b- &f/setspawn - sets the spawnpoint"));
                                sender.sendMessage(Utils.chat("&b- &f/spawn - teleports to the spawnpoint"));
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        }
                    } else if (args[1].equalsIgnoreCase("reload")) {
                        if (args.length == 1) {
                            if (sender.hasPermission("kitpvp.reload")) {
                                Bukkit.broadcast(org.bukkit.ChatColor.GREEN + "[KitPvP] Reloading plugin...", "kitpvp.reload");
                                plugin.reloadConfig();
                                Bukkit.broadcast(org.bukkit.ChatColor.GREEN + "[KitPvP] Reload complete!", "kitpvp.reload");
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                            }
                        } else if (args.length > 1) {
                            if (sender.hasPermission("kitpvp.reload")) {
                                sender.sendMessage(Utils.chat("&cToo many arguments! Correct Usage: /kitpvp reload"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        } else if (args.length < 1) {
                            if (sender.hasPermission("kitpvp.reload")) {
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp help - Displays this info page"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp reload - Reloads the config.yml"));
                                sender.sendMessage(Utils.chat("&b- &f/broadcast <message> - Broadcasts a message to the server"));
                                sender.sendMessage(Utils.chat("&b- &f/setspawn - sets the spawnpoint"));
                                sender.sendMessage(Utils.chat("&b- &f/spawn - teleports to the spawnpoint"));
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        }
                    }
                }
            return false;
        }
       
    }
    
     
  25. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You have your arguments wrong.
    args[0] is the first thing after kitpvp, not args[1]
    Check the length before you check the argument itself.
     
  26. Offline

    KarimAKL

    Okay, would that mean that args.length should be 0 aswell?
     
  27. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You should check before you use args[0] or other ones.
    And java starts counting at 0, not at 1 (like you are doing)
    It could be 0 as well yes, that is when you run /kitpvp
     
  28. Offline

    KarimAKL

    Well i changed all the 1's to 2's but it didn't change a thing in-game.
    New code:
    Code:
    public class CommandHelp implements CommandExecutor {
       
        private Main plugin;
       
        public CommandHelp(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("kitpvp")) {
                    if (args[0].equalsIgnoreCase("help")) {
                        if (args.length == 0) {
                            if (sender.hasPermission("kitpvp.help")) {
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp help - Displays this info page"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp reload - Reloads the config.yml"));
                                sender.sendMessage(Utils.chat("&b- &f/broadcast <message> - Broadcasts a message to the server"));
                                sender.sendMessage(Utils.chat("&b- &f/setspawn - sets the spawnpoint"));
                                sender.sendMessage(Utils.chat("&b- &f/spawn - teleports to the spawnpoint"));
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        } else if (args.length > 0) {
                            if (sender.hasPermission("kitpvp.help")) {
                                sender.sendMessage(Utils.chat("&cToo many arguments! Correct Usage: /kitpvp help"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        } else if (args.length < 0) {
                            if (sender.hasPermission("kitpvp.help")) {
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp help - Displays this info page"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp reload - Reloads the config.yml"));
                                sender.sendMessage(Utils.chat("&b- &f/broadcast <message> - Broadcasts a message to the server"));
                                sender.sendMessage(Utils.chat("&b- &f/setspawn - sets the spawnpoint"));
                                sender.sendMessage(Utils.chat("&b- &f/spawn - teleports to the spawnpoint"));
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        }
                    } else if (args[0].equalsIgnoreCase("reload")) {
                        if (args.length == 0) {
                            if (sender.hasPermission("kitpvp.reload")) {
                                Bukkit.broadcast(org.bukkit.ChatColor.GREEN + "[KitPvP] Reloading plugin...", "kitpvp.reload");
                                plugin.reloadConfig();
                                Bukkit.broadcast(org.bukkit.ChatColor.GREEN + "[KitPvP] Reload complete!", "kitpvp.reload");
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                            }
                        } else if (args.length > 0) {
                            if (sender.hasPermission("kitpvp.reload")) {
                                sender.sendMessage(Utils.chat("&cToo many arguments! Correct Usage: /kitpvp reload"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        } else if (args.length < 0) {
                            if (sender.hasPermission("kitpvp.reload")) {
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp help - Displays this info page"));
                                sender.sendMessage(Utils.chat("&b- &f/kitpvp reload - Reloads the config.yml"));
                                sender.sendMessage(Utils.chat("&b- &f/broadcast <message> - Broadcasts a message to the server"));
                                sender.sendMessage(Utils.chat("&b- &f/setspawn - sets the spawnpoint"));
                                sender.sendMessage(Utils.chat("&b- &f/spawn - teleports to the spawnpoint"));
                                sender.sendMessage(Utils.chat("&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-&f&m-&b&m-"));
                                return true;
                            } else {
                                sender.sendMessage(Utils.chat("&cYou do not have permission for this command!"));
                                return true;
                            }
                        }
                    }
                }
            return false;
        }
       
    }
    
     
  29. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You still check the args[0] before you check the length.
    Check the length first to see if it is > 0.
    Then you can use the args[0]
     
  30. Offline

    MattTheBeast

    @KarimAKL you have a few errors in your code:

    1) I wrote config.load .save etc, but that was just to show what you need to do. To reload your config just use reloadConfig(), to save just do saveConfig(), and to load, its basically just reading your config and getting the information you need..

    2) Idk if this is a typo but in your onEnable, when you call startup(Boolean init) your feeding it your class.. its startup(true) since onEnable is only called when you start your server not reload, so we need to know what to initialize.

    3) Everything you just wrote means nothing if your not gonna use it... Use the reload() methode in your main class, why are you still trying to reload only your config, your using plugin.reloadConfig() instead of plugin.reload()...
     
Thread Status:
Not open for further replies.

Share This Page