Help with redistering command with spigot.

Discussion in 'Spigot Plugin Development' started by AncientTom, Apr 30, 2017.

  1. I am trying to get up to speed with Java coding and creating my own plugins for spigot. I am having difficulty in getting spigot to recognize the command I am trying to work with.

    My plugin.yml file:
    Code (Text):

    name: FeedMe
    version: 0.1
    author: AncientTom
    main: net.sleepyvalley.AncientTom.FeedMe.FeedMe
    command:
      FeedMe:
      description: This will recover your health by 10 hearts.
      usage: /feedme
      aliases: [FeedMe,fm]
     
    My FeedMe.class file
    Code (Text):

    /* FeedMe.java */

    package net.sleepyvalley.AncientTom.FeedMe;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class FeedMe extends JavaPlugin {

        @Override
        public void onEnable() {
            getServer().getLogger().info("FeedMe has been loaded.");
        }
       
        @Override
        public void onDisable() {
            getServer().getLogger().info("FeedMe is now unloaded.");
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            sender.sendMessage("[FedMe] command came from: " + sender);
            sender.sendMessage("[FedMe] command entered = " + command);
            if(command.getName().equalsIgnoreCase("feedme")) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                    player.setFoodLevel(20);
                    player.sendMessage("You have been fed!");
                } else {
                    sender.sendMessage("You must be a player to be fed!");
                }
                return true;
            }
            return false;
        }
       
    }
     
  2. I think you have to change "command" to "commands" in plugin.yml, call getCommand() method,
    Also the naming conventions...
    A 1 second Google search would help.
     
    • Agree Agree x 1
  3. Change 'command' to 'commands' (plugin.yml)
     
  4. Please learn java before attempting to write plugins.

    Also, please encapsulate your code in code blocks.
     
    #4 Lori00, Apr 30, 2017
    Last edited: May 1, 2017
    • Agree Agree x 1
  5. Thank you so much for your quick response. My 71 year old eyes are not what they used to be. With a cataract blinding my left eye and bad astigmatism in the right, it is sometimes hard to catch small type-o errors such as this one. What is funny though is that I passed my yaml through the bukkit plugin.yml checker on line and it rejected the use of the "name:" declaration and said nothing about the "command:" declaration.
    Since I'm new to coding with Java, I have to ask why would you suggest that I make a purposeful call to getCommand(). Doesn't the bukket API automatically register this method when it knows that commands exist in this plugin class as it is defined in the plugin.yml?
    Also, please expound on what you mean by naming conventions.
    You have no idea as to how many days I have been treading across Google, sorting through all of the tutorials on registering commands with bukket/spigot. The actual failing type-o in the yaml suggests that my efforts to exhaustively research the issue first was in vane and all I needed was a 2nd set of eyes to look at it.
     
  6. Wow! How quick do we jump on the chance to belittle someone to make ourselves seem smarter than we actually are. Comments to put someone down without offering any help does not belong in a public forum such as this. You didn't even recognize that the two lines that you refer to were merely there for troubleshooting purposes and wrapping them in some sort of encapsulation would tend to isolate them from the actual call to the method. That is the whole purpose of putting them as the first two lines in the function.

    While I am new to Java, I am no stranger to programming. I first started programming way back before the time of your parents' birth and most certainly longer than you have been alive, even before there was such a term as encapsulation.
     
  7. 1) You misunderstood me. I was just telling you to put the code in [code] blocks to preserve indentation:
    Code (YAML):

    name
    : FeedMe
    version
    : 0.1
    author
    : AncientTom
    main
    : net.sleepyvalley.AncientTom.FeedMe.FeedMe
    command
    :
      FeedMe
    :
      description
    : This will recover your health by 10 hearts.
      usage
    : /feedme
      aliases
    : [FeedMe,fm]
    No need to make such a fuss about it. Your issue could have been related to wrong indentation. And I was not referring to the "encapsulation" term related to programming
    2) I do not doubt you are an experienced programmer, but this
    Code (Java):
    sender.sendMessage("[FedMe] command came from: " + sender);
    shows that you might want to learn more about Java before doing it. (is sender.getName() what you're looking for?) Not related to bukkit, you are trying to print out the string representation of the CommandSender object, not the actual name of the player.

    Also, I am willing to help, but I didn't see the need for re-stating what someone else had said. Sorry if I sounded salty/rude, it wasn't my intention.
     
  8. Alright. Sorry about taking such offense over your first post but the comment "Please learn java before attempting to write plugins." was not helpful and could only have been an attempt to make me feel inadequate. As I stated in my Post, I AM learning Jave as a new language and trying to get familiar with the bukket/spigot APIs. This is just a test plugin that I am working with as part of the learning process. It would have been more appropriate if you would simply pointed out that you didn't think those two lines of code were corret and why. That would have engaged us in a fruitful exchange.

    The two lines that you bring into question are doing exactly what they are intended to do. That is, display what the values of these two objects actually are by outputting them to the console when the command that I have created us called by the API. They are only for test purposes.
     
  9. By the way;
    I did wrap my code in code blocks after trying to format the code in the BB editor. It didn't do what it was intended to do the first time I published the post so I edited the post using the normal editor with code tages. You read this post before I could redo it.
     
    #9 AncientTom, May 1, 2017
    Last edited: May 1, 2017
  10. I'm sorry, but there's one more possibility you mentioned that I don't understand which was "Your issue could have been related to wrong indentation". Even if all indentation was removed in the code file, the compiler parser would care less. I don't remember any programming language that Indentation was a factor in parsing the code except for Yaml. Indentation policies have been historically to establish accepted coding practices for human recognition and easy understanding of the code. Please enlighten me if this is not the case with Java also.
     
  11. I was, indeed, talking about the plugin.yml file. If indentation is wrong, some things break.
    Python cares about indentation
     
  12. Understood. I have done a lot of work with Yaml since I started working on MC bukkit servers.
     
  13. That's understandable. Yaml isn't used a lot outside Bukkit because it has several limitations that other data notations such as JSON do not have. I'm not entirely sure why it is this overused in Bukkit. I assume people do not like all those braces and double quotes in JSON, and find Yaml more friendly. But, that's just personal taste.
     
  14. Same here. I find Yaml to be primitive and very underdeveloped, as if the creators were too lazy to compile a complete work and just left it to the users to suffer with dealing with debugging their work . There aren't any fail-safes for structural errors and just bail out of parsing the text without any explanation when the parser can't recognize one line. That's why there are so many Yaml checkers out on the Internet to provide the error checking that should be in the Yaml parser. I personally think that xml would have been a good choice for config files. XML uses opening and closing tags to absolutely define blocks of instruction which makes troubleshooting problems easy and text format is not an issue. But, json has it's advantages also.
     
  15. This is the usage of getCommand:
    Code (Text):
    getCommand("command").setExecutor(new CommandClass());
    Replace command with the command (ie. feedme, without the /) and replace CommandClass with the class that implements CommandExecutor that the command is in.
    Naming conventions should always be followed: https://www.spigotmc.org/threads/java-naming-conventions-why-and-how.179146/
     
  16. Didn't care for it when I looked at the language about 30 years ago and never used it.
    I still don't see your point to this. This is but just one bit of a registration process done in a coded construct that could be used instead of using a Yaml file This is not needed since adding the 's' to the command's': declaration fixed the problem and the command now loads in at server startup and works just fine without it.
    This guy is just a conventions policeman and by posting his sentiments over any actual direction leaves his credibility to be questioned. I doubt if he has anybody read more than the first paragraph. After all, just because something is published, it doesn't make it right or true.
    Thanks for the link. I read it and it gave me something to think about. I don't think that I will ever create a Class named AncientTom in a package with the fully qualified package name, 'net.sleepyvalley' but anything is possible.
     
  17. I just want to thank everyone that participated in this thread. Our exchanges have increased my understanding of Java, such as it is.
    I now have to call it a day and go out and tend to the chickens. They're hungry.