Solved No output from commands (help)

Discussion in 'Spigot Plugin Development' started by sahil04, May 20, 2016.

  1. Hello,

    I am new to Spigot community and I'm learning to work on java plugins for Spigot. However, I'm also learning java and I am not very experienced with it.

    I tried making a simple, sort of a "hello world!" program, but somehow I managed to mess this up xD

    Here's the code: http://pastebin.com/97aQefyh

    As you can see in the program, I have defined (or at least attempted to), a command "hello" which returns a greeting to the player and broadcasts a test message on the server if executed by the player, or returns a stack trace "test" and a greeting if executed by the console.

    The problem is when I execute this command, whether I am in-game, or from the console, it returns no output. The console shows a line [Info] /hello but that's it. For some reason, it does not show any greeting or broadcasts any message at all.
    I'm using this website as a guide, and I pretty much did exactly as instructed:
    http://www.elfin8er.net/tutorials/bukkit-for-dummies/commands/

    Any help on this topic would be greatly appreciated.

    PS. I accidently deleted a line "return true;" on line 28, please ignore that error

    my plugin.yml file is as follows:

    name: MyFirstPlugin
    main: myPlugin.myFirstPlugin
    version: 1.0
    commands:
    hello:
    description: It replies with a greeting.
    usage: /<command>
     
    #1 sahil04, May 20, 2016
    Last edited: May 20, 2016
  2. You messed up the onCommand parameters. It should be this.
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String arg, String[] args) {
    You probably removed @Override because it gave an error? Yes, that fixes the error (because instead of "Overriding" the command method, you just create your new method which never gets called), but it doesn't do what you'd like (because you want to override the existing one). The names of the parameters are not important. However, the order and types are. So change that and to verify you did it right, put @Override above it. If it doesn't give an error, good job.
     
    • Agree Agree x 3
  3. Yes, putting @Override above onCommand method and re-arranging the order of the parameters worked just as you had said, thanks a lot for your quick answer.
    Though just to be clear; Will I have to put @Override on every method that I wish to be invoked?
     
  4. About the getName() method --

    It may seem strange that you're using the method on both a Command and a Player, two very different objects. The fact that these methods are named the same thing does not mean they are the same method. Check the documentation (you'll have to get used to reading this stuff, even if it seems intimidating now):

    Command - gets the name of a command
    Player - gets the name of a player

    As for the main issue, as @bigteddy98 said, you are implementing the CommandExecutor interface which expects (actually, which requires) you to implement its onCommand(...) method, and which will insist that your arguments are of the same type and in the same order as its original ones.

    You only need to use @Override when you implement an interface or extend a class with abstract methods (like JavaPlugin). The next time you use an interface will likely be when you learn about Listeners. Most methods you will write will not implement interfaces.
     
    • Informative Informative x 1
  5. You extend JavaPlugin. JavaPlugin contains many empty methods. For example, onEnable, onDisable, onCommand. When they get called, they don't do anything, because they have an empty body. However, when you wish to "override" the default empty behavior, you put the method in your main class as well and put @Override above it, meaning that you want to override the empty method.
     
    • Informative Informative x 1
  6. I feel sorry for this guy :p
     
    • Like Like x 1
    • Funny Funny x 1
  7. Just to expand on what has been said above, the @Override annotation is a flag to the compiler telling it that this methods is intended to override a method with the same signature in a superclass. This has to do with object oriented programming(OOP) and specifically inheritance, and is not exclusive to interfaces.

    Since you are in the process of learning I will leave this link here that you might find of interest. It will give you some more info. on Java's predefined annotations. https://docs.oracle.com/javase/tutorial/java/annotations/predefined.html
     
    • Like Like x 1
  8. Yes, thank you for elaborating on the getName() method, although it did cross my mind to read the documentation about it instead of bothering others on forums so, I removed it shortly afterwards I posted this. And thanks again to you and bigteddy for answering my latter question :p

    +1

    edit: way too many "thank you" 's, this is getting awkward xD