Solved Why isnt this working?

Discussion in 'Spigot Plugin Development' started by Husly, Jan 16, 2019.

  1. .
    #1 Husly, Jan 16, 2019
    Last edited: Jan 21, 2019
    • Winner Winner x 1
  2. Define not working. Errors? Simply just not doing anything? Server saying it's an unknown command?
    Make sure you registered your command in the main class as well. You're also setting the command sender as the player before checking if the sender is a player.
    • Agree Agree x 2
    • Like Like x 1
    • Winner Winner x 1
  3. Commands are defined in the plugin.yml file, not your config.yml file.
    • Agree Agree x 1
  4. Removing Problem is fixed
    #4 Husly, Jan 17, 2019
    Last edited: Jan 21, 2019
  5. that was a misstypo my bad
  6. Do not use coding style like
    Code (Java):
    if (boolean)
    because it is chaotic and extremly hard to read in some cases, for example
    Code (Java):
    if (something)
      if (notsomething)
    Just try to code like this:
    Code (Java):
    if (something) {
  7. but i like coding style like that, sorry.
  8. Strahan


    I agree, I despise the brackets on their own line style but that is really personal preference. It's not "wrong".
  9. This is the solution to your problem ^. To register your command in the main class, you must do getCommand("command").setExecutor(this); If you have multiple commands, you got to register both for the same class.
  10. Write all is the same class is a very poor choice. Set executors in differents classes to improve your competences and your code visibility.

    If the plugin source is just for you, do it too.
    it's better that you take this habit right now
    • Informative Informative x 1
  11. Try instantiating your strings from the config after the plugin is enabled and has called saveDefaultConfig(), perhaps? You should also return a default value if the path isn't in the config.
    You still haven't told us the issue, how your command "isn't working". The errors are helpful, they aren't just random walls of text sent to annoy you.
  12. Use your plugin.yml to define the permissions needed for a command as well as the permissions' defaults. (take a look at this).

    If you define the permissions needed for a certain command, you won't have to check for it yourself, it'll be done for you. In other words, when you get to the onCommand method, you can be sure the user has the proper permissions.

    If you define your permissions (with a default) in your plugin.yml, you can make sure you know who has a permission by default. You can set this to "op" so only players with OP would have this permission by default. The advantage of this is that an OP player can chose to negate a permission. With your system of checking for OP, you remove this option from the player.

    As was mentioned before, make sure to check if the sender is an instance of Player before casting it as such.
    If you've got no errors, but you're not getting the result you're expecting, try debugging the code by adding messages (i.e getLogger().info("Here now - doing X");) in several places and see which ones get fired.

    Commands are registered to your plugin by default so if that's where you handle them, there is no need to register commands' executors. And since the OP said the code is in "", I'm guessing this is the case here as well.
  13. You can't tell someone not to use a coding style. Their coding style is their personal difference. If they like the style, let them use it. I personally don't like the OP's style too, but they are free to format their code to their liking.
    • Like Like x 1
  14. Pro Tip #1: Keep your onCommand in a separate class, it will keep you organized.
    Pro Tip #2: Don’t use Main as your main class name, because in some API "Main" could be used and will crash your plugin, or other developers using Main as rheir main class name, will not be able to implement your plugin.
    • Agree Agree x 1
  15. No, you absolutely don't need to set the executor on main class.

    Now, about your organization, like many others have mentioned it is wise to keep your commands in seperated classes so as to be as organized as possible. This will make your life a lot easier. In case you do move your commads in a seperate class, you'd need to register that class as an executor (from the above quote, that'd be correct in this case).

    In any case, you still haven't told us what's actually happening apart from "not working". Could you please go in a more detailed explanation?
  16. Not really on topic but these are some other curiosities I noticed:

    Code (Java):
        @SuppressWarnings({ "unchecked", "rawtypes" })
        ArrayList<String> nofall = new ArrayList(); // Just do new ArrayList<>(); to get rid of warnings instead of suppressing. Also unless you explicitly want to use ArrayList features, you can just use List<String> list = ...
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender; // You don't check if it even is a player before casting
            if ((cmd.getName().equalsIgnoreCase("nv")) && ((sender instanceof Player))) // Why so many extra brackets? not really a mistake but still
                if ((player.hasPermission("nvp")) || (player.isOp())) {
                    if (label.equalsIgnoreCase("nv")) { // You already checked if the command is 'nv'
                        if (args.length == 0) { // there isn't really a need to check for args because you never use args anyway?
                            if (player.hasPotionEffect(PotionEffectType.NIGHT_VISION)) {
                                player.sendMessage( + " " + nmessageoff));
                                return false; // Any reason you're returning false? Most devs always return true to prevent getting a usage message
                            { // What are these brackets doing here?
                                player.sendMessage( + " " + nmessageon));
                                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 100000, 1));
                                return true;
                        if ((cmd.getName().equalsIgnoreCase("vspeed")) && ((sender instanceof Player))) // you put this in the if statement checking if the command and label is 'nv' so this never fires
                            if ((player.hasPermission(speedp)) || (player.isOp())) {
                                if (label.equalsIgnoreCase("vspeed")) { // This can't happen because you checked if the label is 'nv' first
                                    if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                        player.sendMessage( + " " + smessageoff));
                                        return false;
                                    { // What are these brackets doing here
                                        player.sendMessage( + " " + smessageon));
                                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100000, 3));
                                        return true;
                        // You're missing a bracket here?
                } else {
                    player.sendMessage( + " " + noperm));
            // You're missing an other bracket here?
            return false;

        } // A bracket that shouldn't be here
    • Agree Agree x 1
  17. if (label.equalsIgnoreCase("whatever")) is most likely empty, as most users never assign the label.
    It certainly has not been assigned in the plugin.yml !

  18. The problem is now fixed, sorry for the late respond.
  19. Why quoting everyone?
  20. Mark the thread as solved and perhaps close it.

Share This Page