Solved Need help with simple plugin!

Discussion in 'Plugin Development' started by X23mark, Dec 30, 2017.

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

    X23mark

    Hello! I'm quite new to plugin development and I somewhat know java basics.
    I need some help with a simple plugin that I've created through a tutorial on youtube.

    Here's my code:

    Code:
    package me.markiscool.Plugin;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Plugin extends JavaPlugin {
    
        private String name = "Jeremy";
    
        public void onEnable() {
    
            this.getLogger().info(name + "'s plugin has been successfully enabled!");
    
        }
    
        public void onDisable() {
    
            this.getLogger().severe("The plugin has been successfully disabled!");
    
        }
    
        public boolean onCommand(Command cmd, CommandSender sender, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("fish") && sender instanceof Player) {
                
                Player player = (Player) sender;
                player.sendMessage("Hello " + player.getName());
                
                return true;
                
            } else {
                return false;
            }
        }
    }
    and here's my plugin.yml:

    Code:
    name: TestPlugin
    version: 1.1
    main: me.markiscool.Plugin.Plugin
    description: My awesome cool thing I made.
    commands:
      fish:
        description: Replies with something special.
    
    My plugin seems to be working fine. It displays the getLogger().info messages and everything. I am even able to type /? testplugin and view the "fish" command I made. I planned it to send a message back to the player by saying hello but it does not send anything back. Any idea why the command isn't working? When I do /fish, it does literally nothing; it doesn't even reply with "Unknown Command type /help....".

    I'd really appreciate the help! Thanks (Running Server on 1.8.8)
     
    Last edited by a moderator: Dec 30, 2017
  2. You need to register your command. In your onEnable method, put this:
    Code:
    getCommand("fish").setExecutor(this);
    
    What this does is tells Bukkit to execute your onCommand method when it receives the command "fish".
    Hope this helps :)

    Sent from my SM-G903F using Tapatalk
     
  3. Online

    timtower Administrator Administrator Moderator

    Executor defaults to main class though.

    @X23mark Could you add @Override
    Might have things in the wrong order.
     
    KingOfTheEast01 and CeramicTitan like this.
  4. Offline

    X23mark

    I've done both of what you have told me. The plugin still runs onEnable and onDisable ( I see it in console) but my command /fish still doesn't work.
     
  5. Offline

    KingOfTheEast01

    I noticed that you have a return statement in your else statement. This might be what is causing your problem. You see, if you make a return in your if statement, the method is no longer being used, so if you were to write anything outside of an if statement and the if statement's condition read as true, the return would take place, and the rest of the code would be skipped over. Therefore, you can simply keep the return statement in your else statement outside of one. Simply put your return false outside of any other block statements like below, otherwise your method won't be able to detect any sort of return if it's stuck inside of some block statement.

    Code:
    public boolean onCommand(Command cmd, CommandSender sender, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("fish") && sender instanceof Player) {
              
                Player player = (Player) sender;
                player.sendMessage("Hello " + player.getName());
              
                return true;
              
            }
            return false;
        }
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    X23mark


    This is my new code:

    Code:
    package me.markiscool.Plugin;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Plugin extends JavaPlugin {
    
        private String name = "Jeremy";
    
        @Override
        public void onEnable() {
    
            this.getLogger().info(name + "'s plugin has been successfully enabled!");
            getCommand("fish").setExecutor(this);
        }
    
        @Override
        public void onDisable() {
    
            this.getLogger().severe("The plugin has been successfully disabled!");
    
        }
    
        public boolean onCommand(Command cmd, CommandSender sender, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("fish") && sender instanceof Player) {
              
                Player player = (Player) sender;
                player.sendMessage("Hello " + player.getName());
              
                return true;
              
            }
                return false;
        }
    }
    
    and my plugin.yml:
    Code:
    name: TestPlugin
    version: 1.1
    main: me.markiscool.Plugin.Plugin
    description: My awesome cool thing I made.
    commands:
      fish:
        description: Replies with something special.
    Also I'd like to know how you guys put your code and other info into a separate box. Thanks for the help
     
    Last edited by a moderator: Jan 1, 2018
  8. Online

    timtower Administrator Administrator Moderator

    @X23mark [code] <code here> [/code]
     
  9. Offline

    KingOfTheEast01

    That all should work now. Is it not? Also, I believe it was timtower who taught me this yesterday, but I don't think you need to set the executor for your command if it's not in a different class.
     
  10. Offline

    X23mark

    It still doesn't work. I can confirm that onEnable and onDisable are running. It's just my command isn't working. Could it be that I used craftbukkit 1.8.8 as my external libraries when making my code and I put it into my Spigot 1.8.8 server?

    I'll include all details here:

    Code:
    package me.markiscool.Plugin;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Plugin extends JavaPlugin {
    
        private String name = "Jeremy";
    
        @Override
        public void onEnable() {
    
            this.getLogger().info(name + "'s plugin has been successfully enabled!");
        }
    
        @Override
        public void onDisable() {
    
            this.getLogger().severe("The plugin has been successfully disabled!");
    
        }
    
        public boolean onCommand(Command cmd, CommandSender sender, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("fish") && sender instanceof Player) {
             
                Player player = (Player) sender;
                player.sendMessage("Hello " + player.getName());
             
                return true;
             
            }
                return false;
        }
    }
    
    My plugin.yml:

    Code:
    name: TestPlugin
    version: 1.1
    main: me.markiscool.Plugin.Plugin
    description: My awesome cool thing I made.
    commands:
      fish:
        description: Replies with something special.
    
    My package explorer:
    [​IMG]

    What I see when I do /plugins
    [​IMG]

    what I see when I do /? testplugin
    [​IMG]

    and lastly: what prints in the console

    Code:
    02.01 12:33:54 [Server] INFO [TestPlugin] Enabling TestPlugin v1.1
    02.01 12:33:54 [Server] INFO [TestPlugin] Jeremy's plugin has been successfully enabled!
    


    Yet when I try to do /fish, it does not print anything. Doesn't even print the "Unknown Command type /help blah blah"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 2, 2018
  11. Try putting @Override over the onCommand
     
    KingOfTheEast01 likes this.
  12. Offline

    KingOfTheEast01

    As @awesomebutter234 suggested, you should use the @Override annotation. That's probably why it's not doing anything. The command works, but if it's not overriding anything than it will be overridden itself.

    Override or be overridden. :D
     
  13. Offline

    Drkmaster83

    Attach the jar file you're using. The code and plugin.yml that you provide should work, which leads me to believe one of the two isn't being exported properly.
     
  14. Offline

    X23mark

    When I try to add the @Override annotation over my onCommand, it gives me an error and tells me to remove the @Override.
    [​IMG]
     
  15. It should work, cause it works for me..
    [​IMG]

    Try renaming your class name to something else.. IDK if it'll work though
     
  16. Offline

    Horsey

    Your order of arguments is wrong. CommandSender is before Command.

    This is why you should always put the @Override annotation.
     
    X23mark and Drkmaster83 like this.
  17. Offline

    X23mark

    Yup! Thanks!
     
  18. Offline

    Machine Maker

    Please mark this thread as Solved if your issue has been resolved.
     
    X23mark likes this.
Thread Status:
Not open for further replies.

Share This Page