Question

Discussion in 'Plugin Development' started by Tolerance, Jul 8, 2013.

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

    Tolerance

    Hello. I am new to Java and I am just confused about where/what I would add to my code to add a command. I'm trying to add the command /basic but I don't know where to add it, or the code to add.

    Here is my code:

    Code:
    package me.arrived.Testing;
     
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Testing extends JavaPlugin {
       
        Logger log = Logger.getLogger("Minecraft");
       
        public void onEnable(){
            log.info("Your plugin has been enabled!");
        }
     
        public void onDisable(){
            log.info("Your plugin has been disabled.");
        }
       
       
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                    Player player = null;
                    if (sender instanceof Player) {
                        player = (Player) sender;
                    }
               
                    if (cmd.getName().equalsIgnoreCase("basic")){ // If the player typed /basic then do the following...
                        // do something...
                        return true;
                    } else if (cmd.getName().equalsIgnoreCase("basic2")) {
                        if (player == null) {
                            sender.sendMessage("this command can only be run by a player");
                        } else {
                            // do something else...
                        }
                        return true;
                    }
                    return false;
                }
        }
    Thanks!!! :)
     
  2. Offline

    Ibix13

    What do you mean by adding it? You already have the command.
     
  3. Offline

    LinearLogic

    You've got it right:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("basic")){ // If the player typed /basic then do the following...
    2. // do something, for example:
    3. player.sendMessage(ChatColor.GREEN + "You just ran the 'basic' command!");
    4. return true;
    5. }

    And if you haven't already, I'd recommend registering the /basic command in your project's plugin.yml file, so you can add a description (displayed when entering the command /? Testing) and usage line (displayed if the onCommand(...) method returns 'false').
     
  4. Offline

    Ibix13

    I wan gonna say.. This looks perfectly fine... Does he mean by the registering it in plugin.yml?
     
  5. Offline

    LinearLogic

    Ibix13
    You know, the YAML file CB goes to for plugin info...
    Code:
    name: Testing
    main: me.arrived.testing.Testing
    version: 1.0
    author: Tolerance
    commands:
        basic:
            description: Sends a message to the player
            usage: /<command>
        complex:
            description: Hi, I'm an example!
            usage: /<command> <arg1> [arg2]
     
  6. Offline

    Tolerance

    LinearLogic Ibix13

    When I run it on a server and do /basic it just says unknown command. :(
     
  7. Offline

    SnipsRevival

  8. Offline

    LinearLogic

    Are you entering the command from console? If so, drop the '/' before the command. Otherwise, repaste your onCommand(...) method and the contents of your plugin.yml
     
  9. Offline

    Tolerance

    Shit. I just added a new plugin.yml, thinking my old one was wrong and now when I do /plugins it's not even there anymore. Lol. Damn, thanks for all the help though. :)
    LinearLogic SnipsRevival (btw I did it ingame)

    Here is the code once more, just incase I accidentally changed something lol
    Code:
    package me.arrived.Testing;
     
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Testing extends JavaPlugin {
       
        Logger log = Logger.getLogger("Minecraft");
       
        public void onEnable(){
            log.info("Your plugin has been enabled!");
        }
     
        public void onDisable(){
            log.info("Your plugin has been disabled.");
        }
       
       
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                    Player player = null;
                    if (sender instanceof Player) {
                        player = (Player) sender;
                    }
               
                    if (cmd.getName().equalsIgnoreCase("basic")){ // If the player typed /basic then do the following...
                        // do something...
                        return true;
                    } else if (cmd.getName().equalsIgnoreCase("basic2")) {
                        if (player == null) {
                            sender.sendMessage("this command can only be run by a player");
                        } else {
                            // do something else...
                        }
                        return true;
                    }
                    return false;
                }
        }
    And here is my plugin.yml:

    Code:
    name: Testing
    main: me.arrived.testing.Testing
    version: 1.0
    author: arrived
    Thanks again :)
     
  10. Offline

    SnipsRevival

  11. Offline

    Tolerance

    Oh, lol. I tried to do it on my own as much as possible so I didn't really see that. So would it be like this?

    Code:
    name: Testing
    main: me.arrived.testing.Testing
    version: 1.0
    author: arrived
    commands:
      basic:
          description: This is a demo command.
          usage: /<basic> [player]
          permission: <Testing>.basic
          permission-message: You don't have <permission>
     
  12. Offline

    SnipsRevival

  13. Offline

    LinearLogic

    It works fine for me. Although setting the permission plugin.yml node to <Testing>.basic makes that the literal node, and something along the lines of testing.basic would be a more conventional/intuitive name.
     
  14. Offline

    Tolerance

    What the hell. :( It still doesn't work for me. Here's my plugin.yml

    Code:
    name: Testing
    main: me.arrived.Testing.Testing
    version: 1.0
    author: arrived
    commands:
      basic:
          description: This is a demo command.
          usage: /<basic> [player]
          permission: Testing.basic
          permission-message: You don't have <basic>
    ...Help? :'(

    The problem atm is that it's not showing up when I /plugins

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  15. Offline

    MrDynamo

    Change the permission line to permissions:
     
  16. Offline

    SnipsRevival

    Tolerance Are you getting any messages or errors?
     
  17. Offline

    Tolerance

    MrDynamo Still didn't work.
    SnipsRevival 22:53:26 [SEVERE] Could not load 'plugins/Testing.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidDescriptionException: Invalid plugin.yml
    at org.bukkit.plugin.java.JavaPluginLoader.getPluginDescription(JavaPluginLoader.java:247)
     
  18. Offline

    LinearLogic

    In this case, where the node is nested in the "basic" command node, it's actually right the way it is.

    Tolerance, if your jar is compiling and exporting (to the right location...) without issue and you aren't getting any errors whilst starting your server, then it must be divine intervention that it's still not showing up.
     
  19. Offline

    SnipsRevival

    Tolerance Are you sure you exported your plugin.yml with your plugin?
     
  20. Offline

    Vortex_pvp

    How do i post a thread please tell me thank you!
     
  21. Offline

    LinearLogic

    Tolerance, This is what I'm using and it works perfectly. Just replace my packaging with yours.
    Code:
    name: Testing
    main: com.entrocorp.linearlogic.Testing
    version: 1.0
    author: arrived
    commands:
      basic:
          description: This is a demo command.
          usage: /<basic> [player]
          permission: testing.basic
          permission-message: You don't have the <permission> node!
     
  22. Offline

    Tolerance

  23. Offline

    SnipsRevival

    Tolerance It should be outside the src folder.
     
  24. Offline

    Tolerance

    SnipsRevival
    Wait... I left clicked source and clicked File, New File. Am I supposed to create it somewhere else?
     
    xTrollxDudex likes this.
  25. Offline

    SnipsRevival

    Tolerance make it under your project name not src
     
  26. Offline

    tills13

    put getCommand("basic").setExecutor(this); and getCommand("basic2").setExecutor(this); in your onEnable() method.
     
  27. Offline

    SnipsRevival

    I don't think that is necessary when the commands are in the main class
     
  28. Offline

    tills13

    Then should it not extend Listener or CommandExecutor?
     
  29. Offline

    LinearLogic

    Nope. onCommand(...) is a JavaPlugin method. CommandExecutors really only come into play when there are a large number of commands handled by a plugin, or the onCommand(...) method is large enough that you'd wish to move it to its own class to reduce the clutter in your main class.
     
  30. Offline

    Tolerance

    Still not working....

    Argh, I'll just restart as it's very basic and quick to make then test it again. Maybe it's just one of those "unlucky" problems.

    Thanks for the help, though. ^.^
     
Thread Status:
Not open for further replies.

Share This Page