Command Never Recognised?

Discussion in 'Plugin Development' started by Nineza, Mar 4, 2011.

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

    Nineza

    Well, I'm having a little problem with my first plugin.
    It loads with no errors on the console, but when you try to use the 'testzz' command, it just outputs '/testzz', rather than messaging the player back.
    For example, a chat log from my point of view on my server:
    ChatLog (open)



    Just before the /testzz, I used the /testzz command. I think this is trying to tell me that I'm using it incorrectly, but my YAML file is saying that's how to use it? :/
    Here's my main file, and my plugin.yaml file:

    OtherStuph.java (open)
    Code:
    package com.Nineza.otherstuph;
    
    import java.io.File;
    import java.util.HashMap;
    
    import org.bukkit.command.Command;
    import org.bukkit.entity.Player;
    import org.bukkit.Server;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    /**
    * Plugin for Bukkit
    *
    * @author Nineza
    */
    public class OtherStuph extends JavaPlugin {
        private final OSPlayerListener playerListener = new OSPlayerListener(this);
        private final OSBlockListener blockListener = new OSBlockListener(this);
        private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
    
        // NOTE: There should be no need to define a constructor any more for more info on moving from
        // the old constructor see:
        // http://forums.bukkit.org/threads/too-long-constructor.5032/
    
        public void onDisable() {
            // TODO: Place any custom disable code here
    
            // NOTE: All registered events are automatically unregistered when a plugin is disabled
    
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            System.out.println("Goodbye world!");
        }
    
        public void onEnable() {
            // TODO: Place any custom enable code here including the registration of any events
    
            // Register our events
            PluginManager pm = getServer().getPluginManager();
            /*pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.BLOCK_PHYSICS, blockListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.BLOCK_CANBUILD, blockListener, Priority.Normal, this);*/
    
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            PluginDescriptionFile pdfFile = this.getDescription();
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
        }
    
        public boolean isDebugging(final Player player) {
            if (debugees.containsKey(player)) {
                return debugees.get(player);
            } else {
                return false;
            }
        }
    
        public void setDebugging(final Player player, final boolean value) {
            debugees.put(player, value);
        }
        public boolean onCommand(Player player, Command command, String commandLabel, String[] args) {
            String[] split = args;
            String commandName = command.getName().toLowerCase();
            
            if (commandName.equals("testzz")){
                player.sendMessage("Hi there, " + player.getDisplayName() + "! =D");
                return true;
            }
            return false;
        }
    }
    plugin.yml (open)
    Code:
    name: OtherStuph
    main: com.Nineza.otherstuph.OtherStuph
    version: 0.01
    author: Nineza
    description: A bunch of useful stuff for Bukkit users.
    commands:
        testzz:
            description: A test for the OtherStuph plugin
            usage: /<command>
    


    Source folder hierarchy:
    Hierarchy (open)
    Code:
    --src
    ----plugin.yml
    ----com
    ------Nineza
    --------otherstuph
    ----------OtherStuph.java
    ----------OSBlockListener.java
    ----------OSPlayerListener.java
    


    So yeah, I'm probably making a silly mistake, but that's what learning's for. xD
    If you need me to post any of the other source codes, feel free to ask. But I don't think they'd make much difference since their part in the event registration is commented out until it's needed. :/

    Thanks in advance!
     
  2. Offline

    Lavamike

    Hey, I got it working using this code for onCommand:

    Code:
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
    {
            String[] split = args;
            String commandName = command.getName().toLowerCase();
            
            if(sender instanceof Player)
            {
                Player player = (Player) sender;
                
                if (commandName.equals("testzz")) {
                    player.sendMessage("Hi there, " + player.getDisplayName() + "! =D");
                    return true;
                }
            }
    	return false;
    }
    
    I found that your code wasn't being called when typing the /testzz at all, so i looked at my command method and saw it was different.

    Yours: public boolean onCommand(Player player, Command command, String commandLabel, String[] args)
    Mine: public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)

    I'm guessing it had something to do with the part i put in bold.

    Anyway I added if(sender instanceof Player) which I guess makes sure the send is actually a Player. I'm not extremely good at java yet but I'm learing so I hope this helped! :p
     
  3. Offline

    xZise

    Yep this is relevant. Because then the original method is only called. May tip to prevent changes like this is to mark all methods, which are overrided with the “@override”:

    Code:
    	@Override
    	public void onDisable() {
    	    […]
    
    	@Override
    	public void onEnable() {
    		[…]
    	@Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    	[…]
    Then the compiler fails if there is nothing to override.

    Fabian
     
  4. Offline

    Nineza

    Ok, it works now.
    Thanks guys. =D
     
Thread Status:
Not open for further replies.

Share This Page