Help with new command thing

Discussion in 'Plugin Development' started by Saturisk, Mar 1, 2011.

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

    Saturisk

    Okay, a week ago I finally started learning bukkit and made a few plugins.. Now they are all broke because of the command thing and i have no idea how it works!
    Can anyone explain to me?

    Mostly i'm trying to get this thing to work where i say '/fire <name>' and it lights the person on fire.
    It doesn't work now. How do i set it up without the setExecutor thing but with the if(!(instance of player) thing...

    Sorry if i'm not that descriptive ill try to give more data if needed
     
  2. Offline

    SunShe

    i see, wich part of your code your need to update it ot be work again?

    Give me the broken code ($.$)' i gonna fix it.
     
  3. Offline

    Saturisk

    uhm okay:

    Code:
    package com.saturisk.shawn.BasicPlugin;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    /**
     * Basic for Bukkit
     *
     * @author Saturisk
     */
    //Starts the class
    public class BasicPlugin extends JavaPlugin{
    
        //Links the BasicPlayerListener
        private final BasicPluginPlayerListener playerListener = new BasicPluginPlayerListener();
     
        @Override
        //When the plugin is disabled this method is called.
        public void onDisable() {
    
        }
    
        @Override
        //When the plugin is enabled this method is called.
        public void onEnable() {
            //Create the pluginmanage pm.
            PluginManager pm = getServer().getPluginManager();
            //Create PlayerCommand listener
            pm.registerEvent(Event.Type.PLAYER_COMMAND, this.playerListener, Event.Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this);
           //Get the infomation from the yml file.
            PluginDescriptionFile pdfFile = this.getDescription();
            //Print that the plugin has been enabled!
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    
        }
     
    }
    and the player listener

    Code:
    package com.saturisk.shawn.BasicPlugin;
    
    //All the imports
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.ChatColor;
    //Starts the class BasicPlayer listener
    public class BasicPluginPlayerListener extends PlayerListener{
    
          //This method is called whenever a player uses a command.
              public void onPlayerJoin(PlayerLoginEvent event) {
    
                  Player player = event.getPlayer();
    
                  player.sendMessage(ChatColor.GREEN + "Welcome to the server " + player.getName() + ", please type '/rules' for the guidelines on this server");
     
              }
    
            public void onPlayerCommand(PlayerChatEvent event) {
              //Make the message a string.
                String[] s = event.getMessage().split(" ");
                //Get the player that talked.
                Player player = event.getPlayer();
                //If the first part of the string is /basic or /b then do this.
                if (s[0].equalsIgnoreCase("/rules")) {
                    player.sendMessage(ChatColor.GREEN + "[1] Don't destroy what isn't yours without permission");
                    player.sendMessage(ChatColor.GREEN + "[2] Don't nag other players to help you or give you stuff");
                    player.sendMessage(ChatColor.GREEN + "[3] Have fun");
                    player.sendMessage(ChatColor.GREEN + "[4] No Griefing");
                    player.sendMessage(ChatColor.GREEN + "[5] To see a list of commands type '/commands'");
     
                }
    
                if (s[0].equalsIgnoreCase("/commands")) {
    
                    player.sendMessage(ChatColor.GREEN + "Coming soon once i set up everything.");
    
                }
    
                if(event.getPlayer().isOp() && s[0].equalsIgnoreCase("/fire")){
                    //Define a variable of type Player for storing the person to set on fire (initially set to null).
                    Player target = null;
                    //Check whether there command includes an extra argument for player name
                    if(s.length > 1)
                        //If so, set target to be the player with the name given
                        target = event.getPlayer().getServer().getPlayer(s[1]);
                    //if by this point target is still equal to null, either a name argument wasn't supplied or there was no player with that name.
                    if(target == null)
                        //in that case, target the player calling the command.
                        target = event.getPlayer();
                    //set target on fire.
                    target.setFireTicks(100);
                }
    
          }
    }
    
    I know my problem is just the command part i don't understand it...
     
  4. Offline

    SunShe

    i see no errors, but its from bukkit-0.0.1-SNAPSHOT #418. For #440
    --- merged: Mar 2, 2011 4:21 AM ---
    But if you are one a more high version, probably need to change "Event.Type.PLAYER_COMMAND"
    --- merged: Mar 2, 2011 4:25 AM ---
    i think you need to remove the "registerEvent" for;
    public boolean onCommand(CommandSender sender, Command incomingCommand, String commandLabel, String args[])
    {

    }
     
  5. Offline

    Saturisk

    I said i was trying to update this to work with new bukkit
     
  6. Offline

    AaronLLF

    Code:
    package com.saturisk.shawn.BasicPlugin;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    /**
     * Basic for Bukkit
     *
     * @author Saturisk
     */
    //Starts the class
    public class BasicPlugin extends JavaPlugin{
    
        //Links the BasicPlayerListener
        private final BasicPluginPlayerListener playerListener = new BasicPluginPlayerListener();
     
        @Override
        //When the plugin is disabled this method is called.
        public void onDisable() {
    
        }
    
        @Override
        //When the plugin is enabled this method is called.
        public void onEnable() {
            //Create the pluginmanage pm.
            PluginManager pm = getServer().getPluginManager();
            //Create PlayerCommand listener
            pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, this.playerListener, Event.Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this);
           //Get the infomation from the yml file.
            PluginDescriptionFile pdfFile = this.getDescription();
            //Print that the plugin has been enabled!
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    
        }
     
    }
    
    Code:
    package com.saturisk.shawn.BasicPlugin;
    
    //All the imports
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.ChatColor;
    //Starts the class BasicPlayer listener
    public class BasicPluginPlayerListener extends PlayerListener{
    
          //This method is called whenever a player uses a command.
              public void onPlayerJoin(PlayerLoginEvent event) {
    
                  Player player = event.getPlayer();
    
                  player.sendMessage(ChatColor.GREEN + "Welcome to the server " + player.getName() + ", please type '/rules' for the guidelines on this server");
     
              }
    
            public void onPlayerCommandPreprocess(PlayerChatEvent event) {
              //Make the message a string.
                String[] s = event.getMessage().split(" ");
                //Get the player that talked.
                Player player = event.getPlayer();
                //If the first part of the string is /basic or /b then do this.
                if (s[0].equalsIgnoreCase("/rules")) {
                    player.sendMessage(ChatColor.GREEN + "[1] Don't destroy what isn't yours without permission");
                    player.sendMessage(ChatColor.GREEN + "[2] Don't nag other players to help you or give you stuff");
                    player.sendMessage(ChatColor.GREEN + "[3] Have fun");
                    player.sendMessage(ChatColor.GREEN + "[4] No Griefing");
                    player.sendMessage(ChatColor.GREEN + "[5] To see a list of commands type '/commands'");
     
                }
    
                if (s[0].equalsIgnoreCase("/commands")) {
    
                    player.sendMessage(ChatColor.GREEN + "Coming soon once i set up everything.");
    
                }
    
                if(event.getPlayer().isOp() && s[0].equalsIgnoreCase("/fire")){
                    //Define a variable of type Player for storing the person to set on fire (initially set to null).
                    Player target = null;
                    //Check whether there command includes an extra argument for player name
                    if(s.length > 1)
                        //If so, set target to be the player with the name given
                        target = event.getPlayer().getServer().getPlayer(s[1]);
                    //if by this point target is still equal to null, either a name argument wasn't supplied or there was no player with that name.
                    if(target == null)
                        //in that case, target the player calling the command.
                        target = event.getPlayer();
                    //set target on fire.
                    target.setFireTicks(100);
                }
    
          }
    }
    
    This should work.
     
  7. Offline

    petteyg359

    See scrapbukkit, chatbukkit, and sampleplugin on http://github.com/bukkit.

    onCommand is for commands in current recommended build #440. onCommand is not an event.
    For builds 454+, use getCommand, setExecutor, and define a CommandExecutor per command.
     
  8. Offline

    SunShe

    Ah, all that for say to change from "PLAYER_COMMAND" to "PLAYER_COMMAND_PREPROCESS". Easy to update.
     
  9. Offline

    ceulemans

    first see this thread.
    Code:
    public void onEnable() {
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this);
        getCommand("rules").setExecutor(new EXEMPLECOMMANDEXECUTOR(this));
          //this makes it so only if the first part of a command is rules, it will go to the
              commandexecutor
        PluginDescriptionFile pdfFile = this.getDescription();
        System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    }
    
    and make a CommandExecutor (=new class)

    Code:
    public class EXEMPLECOMMANDEXECUTOR implements CommandExecutor {
        //here you can do your command handeling, don't use onPlayerCommand
            but use onCommand.
    }
    
    also don't forget to change you plugin.yml (see spoiler in here)
     
  10. Offline

    phondeux

    The sample provided on your link uses onPlayercommand, doesn't use getCommand, or define a CommandExecutor.

    Otherwise pretty nice though! I'll look there in the future for reference.
     
  11. Offline

    petteyg359

    Ignore SamplePlugin then, until sombody fixes it :) Look at ScrapBukkit instead.
     
  12. Offline

    ceulemans

    I used scrapbukkit too.
     
  13. Offline

    Plague

    PLAYER_COMMAND_PREPROCESS is to be used only when really necessary, not as a quick and dirty solution to not using onCommand. Yes it works, but I think Dinnerbone knows why it should not be done when he says so.
     
  14. Offline

    Saturisk

    I didn't even know PLAYER_COMMAND_PREPROCESS exists. I checked the wiki that they never seem to update. Is there a new place for all these new registerEvent items?

    Plague, i tried working with the sender instanceof part, it didn't work out to well, i must not be setting up my events properly.
     
  15. Offline

    SunShe

    ok i see, need to put commands in the .yml watched at other plugins But, where is the benefit to do like that? im not sur to understand... "quick and dirty solution"... yea like that that feel really dirty. Maybe clean and so beautifull at the .yml but how about hooking? im curious to see how that work in the bukkit. i gonna watch that...
     
  16. Offline

    Plague

    The pros are that you can easily see them when looking at the plugin.yml and you can also EDIT the names, which is pretty great I think. You don't have to contact the developer for renaming the commands.
     
  17. Offline

    Saturisk

    Ugh, so how do i set it up without preprocess? :confused: I'm still really confused with commands, I think im going to quit using them. haha.
     
  18. Offline

    Plague

    Just look at some plugin and use it's code, that's the best way since you have a fully working code in your hands.
     
  19. Offline

    SunShe

    I have begin a blank plugin projet with latests bukkit/craftbukkit and after many tests, im not be able to make work something, lol.

    In OnEnable i have tested that,

    Code:
            getCommand("marco").setExecutor(new CommandExecutor() {
                public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                    sender.sendMessage("Polo");
                    return true;
                }
            });
    But red line error at: setExecutor and CommandExecutor

    I have tested like you said within making a new class and write that,
    Code:
    getCommand("test").setExecutor(new TestCommandListener(this));
    But at console a message ( is updated? ) error and nothing happend when i try the command "/test"

    In my CommandListener class:

    Code:
    public class TestCommandListener implements CommandExecutor {
    
        public static Test plugin;
         public TestCommandListener(Test instance) {
             plugin = instance;
         }
    
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String args[]){
            String trimmedArgs[] = args;
            String commandName = command.getName().toLowerCase();
            if(sender instanceof Player){
    
                if(commandName.equals("Test")){
                    Player player = (Player)sender;
                    player.sendMessage("You have tested");
                    return true;
                }
    
            }
            return false;
        }
    
    }
    And in the .yml,

    Code:
    name: Test
    main: com.bukkit.sunshe.Test.Test
    version: 1.0
    
    commands:
    test:
    description: nodesc
    usage: /<command>
    I look so noob but im not be able to make something work. :mad::p xD

    Can i have some help please ? [cake] :rolleyes:
     
  20. Offline

    Plague

    indent the yml with a few spaces for the command and its description/usage

    it may have nothing to do with it, but since I have it that way it's something I'd test first.
     
  21. Offline

    ceulemans

    use 4 space, no tabs
     
Thread Status:
Not open for further replies.

Share This Page