Multiple Commands. Need help.

Discussion in 'Plugin Development' started by TheLazeboy, Dec 27, 2012.

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

    TheLazeboy

    So it seems like it is impossible to find any good tutorials on this or the 7th Wonder Of The World, CommandExecutor. I want to be able to make my commands go into their own, nice little class, but they are bad students. :3 I've got a heal command and a teleport to player command. How, in the name of Notch, do I do this?

    P.S. Do I even need the Block and Player Listeners?!
     
  2. Offline

    RealDope

    Make the class implement CommandExectuor and I'm not sure if it's necessary, but in the onEnable() of your main, do this:
    getServer().getPluginCommand("<CommandName>").setExecutor(new <ClassName>());
     
  3. Offline

    gomeow

  4. Offline

    TheLazeboy

    What about these errors? They don't recognize args or commandLabel, aswell as other errors.

    Here is the SuperpowersCommandExecutor class (Superpowers is the plugin's name :3):
    Code:
    package me.TheLazeboy.superpowers;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class SuperpowersCommandExecutor implements CommandExecutor {
     
        private Superpowers plugin; // pointer to your main class, unrequired if you don't need methods from the main class
     
        public SuperpowersCommandExecutor(Superpowers plugin) {
            this.plugin = plugin;
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            // implementation exactly as before...
        }
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("heal") | commandLabel.equalsIgnoreCase("spheal")){
                if(args.length == 0){
                    player.setHealth(20);
                    player.setFireTicks(0);
                    player.setFoodLevel(20);
                    player.sendMessage(ChatColor.GREEN + "Healed!");
                }else if(args.length == 1){
                    if(player.getServer().getPlayer(args [0]) !=null){
                    Player targetPlayer = player.getServer().getPlayer(args [0]);
                    targetPlayer.setHealth(20);
                    targetPlayer.setFireTicks(0);
                    targetPlayer.setFoodLevel(20);
                    player.sendMessage(ChatColor.GREEN + "Healed!");
                    }else{
                        player.sendMessage(ChatColor.RED + "Player is not online!");
                    }}else if(commandLabel.equalsIgnoreCase("tp") | commandLabel.equalsIgnoreCase("sptp")){
                    if(args.length == 0){
                        player.sendMessage(ChatColor.RED + "Too little arguments!");
                    }else if(args.length == 1){
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        Location targetPlayerLocation = targetPlayer.getLocation();
                        player.teleport(targetPlayerLocation);
                    }else if(args.length == 2){
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        Player targetPlayer1 = player.getServer().getPlayer(args[1]);
                        Location targetPlayer1Location = targetPlayer1.getLocation();
                        targetPlayer.teleport(targetPlayer1Location);
                        targetPlayer.sendMessage(ChatColor.GREEN + "You have been to teleported to " + player.getName());
        }
     
    }
    }
    }}
    
     
  5. Offline

    gomeow

    In the onCommand, you have it as label, then try to use commandLabel.

    Change it to this:
    Show Spoiler

    Code:java
    1. package me.TheLazeboy.superpowers;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class SuperpowersCommandExecutor implements CommandExecutor {
    11.  
    12. private Superpowers plugin; // pointer to your main class, unrequired if you don't need methods from the main class
    13.  
    14. public SuperpowersCommandExecutor(Superpowers plugin) {
    15. this.plugin = plugin;
    16. }
    17.  
    18. @Override
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    20. // Fixed to correct implementation
    21. }
    22. Player player = (Player) sender;
    23. if(commandLabel.equalsIgnoreCase("heal") | commandLabel.equalsIgnoreCase("spheal")){
    24. if(args.length == 0){
    25. player.setHealth(20);
    26. player.setFireTicks(0);
    27. player.setFoodLevel(20);
    28. player.sendMessage(ChatColor.GREEN + "Healed!");
    29. }else if(args.length == 1){
    30. if(player.getServer().getPlayer(args [0]) !=null){
    31. Player targetPlayer = player.getServer().getPlayer(args[0]);
    32. targetPlayer.setHealth(20);
    33. targetPlayer.setFireTicks(0);
    34. targetPlayer.setFoodLevel(20);
    35. player.sendMessage(ChatColor.GREEN + "Healed!");
    36. }else{
    37. player.sendMessage(ChatColor.RED + "Player is not online!");
    38. }}else if(commandLabel.equalsIgnoreCase("tp") | commandLabel.equalsIgnoreCase("sptp")){
    39. if(args.length == 0){
    40. player.sendMessage(ChatColor.RED + "Too little arguments!");
    41. }else if(args.length == 1){
    42. Player targetPlayer = player.getServer().getPlayer(args[0]);
    43. Location targetPlayerLocation = targetPlayer.getLocation();
    44. player.teleport(targetPlayerLocation);
    45. }else if(args.length == 2){
    46. Player targetPlayer = player.getServer().getPlayer(args[0]);
    47. Player targetPlayer1 = player.getServer().getPlayer(args[1]);
    48. Location targetPlayer1Location = targetPlayer1.getLocation();
    49. targetPlayer.teleport(targetPlayer1Location);
    50. targetPlayer.sendMessage(ChatColor.GREEN + "You have been to teleported to " + player.getName());
    51. }
    52.  
    53. }
    54. }
    55. }}
    56.  
     
  6. Offline

    TheLazeboy

    I still have these errors; the args, sender, and the second commandLabel. Using your code. :/
     
  7. Offline

    RainoBoy97

    You dont have the commands inside the onCommad() method.
    Where you have the //implementsion as before, theres the place you need to have the commands in.
     
  8. Offline

    TheLazeboy

    Oh lol. Thanks, but I sill have the commandLabel issues in each of my command methods.
     
  9. Offline

    RainoBoy97

    TheLazeboy
    Where you check if the command is ("heal") | ("spheal") try use 2 | between them.
     
  10. Offline

    TheLazeboy

    But I'm trying to alternate what the play can type to achieve the same command. But I will try if I get any errors.

    EDIT:
    The commandLabel errors are still the same. And I took out the extra | you suggested for the commands.
     
  11. Offline

    fireblast709

    OR gates use || (twice the |). Also, the code gomeow sent contains an error. Change all the 'commandLabel' for 'label'
     
  12. Offline

    RainoBoy97

    Yes, i know.
    || = or

    I dont see any problems that will make this error.

    + itsnot inside the onCommand block :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  13. Offline

    TheLazeboy

    Okay so what is it exactly I need to do? Here is the current code for the Command Executor class, http://pastebin.com/tCW44qr4 .
     
  14. Offline

    RainoBoy97

    Well, i dont see any errors. Should work..
     
  15. Offline

    TheLazeboy

    [​IMG]

    fireblast709 bump :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  16. Offline

    fireblast709

    TheLazeboy
     
  17. Offline

    TheLazeboy

    So what you are saying is change commandLabel to commandlabel. That is the only thing I think you could be telling me. :mad: Elaborate.
     
  18. Offline

    fireblast709

    no change 'commandLabel' to 'label'
     
  19. Offline

    TheLazeboy

    No errors in the code, but the console does. Does it have to do with the Main class code? http://pastebin.com/DjdFX2WA

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  20. Offline

    ImDeJay

    whats the error in console?
     
  21. Offline

    TheLazeboy

    [​IMG]
     
  22. Offline

    ImDeJay

    you've got something wrong inside your plugin.yml.
     
  23. Offline

    TheLazeboy

  24. Offline

    ImDeJay

    ok so here is the best was i can explain this.

    Where it says "main:" everything has to be EXACTLY how it shows in your plugin

    Example:

    Code:
    package testPlugin;
     
    public class testMain extends JavaPlugin implements Listener{
    
    from there i see that "testPlugin" is the package i am in, and "testMain" is my class name.

    now in my plugin.yml i would put

    Code:
    main: testPlugin.testMain
    see how it says "testPlugin.testMain"?

    this is case sensitive so remember that.

    now in your plugin.yml you need to put your packagename then your MAIN class name

    *EDIT*

    your plugin.yml would read like this

    Code:
    name: Superpowers
    main: me.TheLazeboy.superpowers.SuperpowersCommandExecutor
    version: 0.3
    description: >
                 Heroes in Minecraft!
    commands:
      heal:
        description: Heals a player.
      spheal:
        description: Heals a player.
      tp:
        description: Teleports to a player.
      sptp:
        description: Teleports to a player.
     
  25. Offline

    TheLazeboy

    I changed my "main: ... "to what you said, but I still have a console error. And as I also stated in the original post,
    I don't have the listener classes, I found them useless and removed them.
     
  26. Offline

    ImDeJay

    unless you plan on using these events/classes they are useless.

    and what console error do you have now?
     
  27. Offline

    TheLazeboy

    [​IMG]
     
  28. Offline

    fireblast709

    that is your CommandExecutor class. The plugin.yml's main value should point to the class that extends JavaPlugin (aka the main class)
     
  29. Offline

    TheLazeboy

    Okay, it all ended well when I changed the plugin.yml's main to what I knew it should have been. :)
     
Thread Status:
Not open for further replies.

Share This Page