Put multiple commands in different classes?

Discussion in 'Plugin Development' started by cdsboy2000, May 19, 2013.

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

    cdsboy2000

    Hello,
    I just started using Bukkit API, and I love it. I currently have a plugin that has /heal, /starve, and /bring. All of these commands are in the main class. However, this all looks very messy. Is there a way to seperate all these commands into different classes? (ex: Heal.java, Starve.java, Bring.java)

    Here's my code right now.
    Code:
    package net.aeroency.aeroency;
     
    import java.util.logging.Logger;
    import java.util.Scanner;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Aeroency extends JavaPlugin{
     
            static Scanner scanner = new Scanner( System.in );
       
            public final Logger logger = Logger.getLogger("Minecraft");
         
            public void onEnable() {
                    logger.info("Aeroency enabled!");
            }
         
            public void onDisable() {
                    logger.info("Aeroency disabled.");
            }
         
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
               
                    if (commandLabel.equalsIgnoreCase("heal")) {
                       
                        if(!(sender instanceof Player)) {
                           
                            if (args.length == 0) {
                               
                                sender.sendMessage("You're a console, you can't heal yourself.");
                               
                            }
                           
                            if (args.length == 1) {
                               
                                Player otherPlayer = Bukkit.getPlayer(args[0]);
                               
                                if (otherPlayer != null) {
                                        otherPlayer.setHealth(20);
                                        otherPlayer.setFoodLevel(20);
                                           
                                        otherPlayer.sendMessage(ChatColor.GREEN + "You have been healed by the console.");
                                        sender.sendMessage(ChatColor.GREEN + "You have healed " + otherPlayer.getName() + ".");
                                       
                                        }
                               
                                else {
                                    sender.sendMessage("Error: Player not found");
                                }
                                       
                                        }
     
                            else {
                                sender.sendMessage("Usage: /heal <player>");
                            } 
                           
                            }
                       
                        else {
                       
                        Player player = (Player) sender;   
                           
                        if (!player.isOp()) {
                            sender.sendMessage(ChatColor.RED + "You don't have permission to heal!");
                        }
                       
                        else {
                       
                            if (args.length == 0) {
                                         
                                 
                                    player.setHealth(20);
                                    player.setFoodLevel(20);
                                    player.sendMessage(ChatColor.GREEN + "You have been healed.");
                                         
                                    }
                                 
                            }
                         
                            if (args.length == 1) {
                                 
                                    Player otherPlayer = Bukkit.getPlayer(args[0]);
                                   
                                    if (otherPlayer != null) {
                                            otherPlayer.setHealth(20);
                                            otherPlayer.setFoodLevel(20);
                                           
                                            if (player.getName().equals(otherPlayer.getName())) {
                                                player.sendMessage(ChatColor.GREEN + "You have healed yourself.");
                                            }
                                           
                                            else {
                                               
                                            otherPlayer.sendMessage(ChatColor.GREEN + "You have been healed by " + player.getName() + ".");
                                            player.sendMessage(ChatColor.GREEN + "You have healed " + otherPlayer.getName() + ".");
                                           
                                            }
                                    }
                                 
                                    else {
                                            sender.sendMessage(ChatColor.RED + "Error: Player not found!");
                                    }
                                 
                            }
                         
                            if (args.length > 1) {
                                    sender.sendMessage(ChatColor.RED + "Usage: /heal [name]");
                            }
                         
                    }
                    }
                   
                    /*
                    * STARVE COMMAND
                    */
                   
                    if (commandLabel.equalsIgnoreCase("starve")) {
                       
                        if(!(sender instanceof Player)) {
                           
                            if (args.length == 0) {
                               
                                sender.sendMessage("You're a console, you can't starve yourself.");
                               
                            }
                           
                            if (args.length == 1) {
                               
                                Player otherPlayer = Bukkit.getPlayer(args[0]);
                               
                                if (otherPlayer != null) {
                                        otherPlayer.setFoodLevel(0);
                                           
                                        otherPlayer.sendMessage(ChatColor.GREEN + "You have been starved by the console.");
                                        sender.sendMessage(ChatColor.GREEN + "You have starved " + otherPlayer.getName() + ".");
                                       
                                        }
                               
                                else {
                                    sender.sendMessage("Error: Player not found");
                                }
                                       
                                        }
     
                            else {
                                sender.sendMessage("Usage: /starve <player>");
                            } 
                           
                            }
                       
                        else {
                       
                        Player player = (Player) sender;   
                           
                        if (!player.isOp()) {
                            sender.sendMessage(ChatColor.RED + "You don't have permission to starve!");
                        }
                       
                        else {
                       
                            if (args.length == 0) {
                                         
                                    player.setFoodLevel(0);
                                    player.sendMessage(ChatColor.GREEN + "You have been starved.");
                                         
                                    }
                                 
                            }
                         
                            if (args.length == 1) {
                                 
                                    Player otherPlayer = Bukkit.getPlayer(args[0]);
                                   
                                    if (otherPlayer != null) {
                                            otherPlayer.setFoodLevel(0);
                                           
                                            if (player.getName().equals(otherPlayer.getName())) {
                                                player.sendMessage(ChatColor.GREEN + "You have starved yourself.");
                                            }
                                           
                                            else {
                                               
                                            otherPlayer.sendMessage(ChatColor.GREEN + "You have been starved by " + player.getName() + ".");
                                            player.sendMessage(ChatColor.GREEN + "You have starved " + otherPlayer.getName() + ".");
                                           
                                            }
                                    }
                                 
                                    else {
                                            sender.sendMessage(ChatColor.RED + "Error: Player not found!");
                                    }
                                 
                            }
                         
                            if (args.length > 1) {
                                    sender.sendMessage(ChatColor.RED + "Usage: /starve [name]");
                            }
                         
                    }
                    }
                   
                    /*
                    * BRING COMMAND
                    */
                   
                    if (commandLabel.equalsIgnoreCase("bring")) {
                       
                        if(!(sender instanceof Player)) {
                           
                            sender.sendMessage("You're a console, you can't bring people to your dimension.");
                           
                            }
                       
                        else {
                       
                        Player player = (Player) sender;   
                           
                        if (!player.isOp()) {
                            sender.sendMessage(ChatColor.RED + "You don't have permission warp people.");
                        }
                       
                        else {
                       
                            if (args.length == 0) {
                                         
                                    sender.sendMessage(ChatColor.RED + "Usage: /bring <name>");
                                         
                                    }
                                 
                            }
                         
                            if (args.length == 1) {
                                 
                                    Player otherPlayer = Bukkit.getPlayer(args[0]);
                                   
                                    if (otherPlayer != null) {
                                           
                                            if (player.getName().equals(otherPlayer.getName())) {
                                               
                                                player.sendMessage("You can't summon yourself!");
                                               
                                            }
                                           
                                            else {
     
                                                otherPlayer.teleport(player.getLocation());
                                                otherPlayer.sendMessage(ChatColor.GREEN + "You have been summoned by " + player.getName() + ".");
                                           
                                            }
                                    }
                                 
                                    else {
                                            sender.sendMessage(ChatColor.RED + "Error: Player not found!");
                                    }
                                 
                            }
                         
                            if (args.length > 1) {
                                    sender.sendMessage(ChatColor.RED + "Usage: /bring <name>");
                            }
                         
                    }
                    }
           
                 
                    return false;
            }
    }
     
  2. Offline

    Tirelessly

    getCommand("heal").setExecutor(new Heal(), this); or something similar, where Heal implemented CommandExecutor.
     
  3. Offline

    cdsboy2000

    What would be in the Heal class?
     
  4. Offline

    skipperguy12

    Yes, there is.
    Let the command implement CommandExecutor, and move the onCommand() into that class. Example:
    Code:
    package net.github.skipperguy12.HungerGames.core.commands;
     
     
    public class CommandA implements CommandExecutor {
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
            return true;
        }
    }
    Then in onEnable:
    CommandA commandA= new CommandA();
    getCommand("somecommand").setExecutor(commandA);
     
  5. Offline

    Froshin

  6. Offline

    Tirelessly

    It would need to implement CommandExecutor, which means it would need to have an onCommand(...) method.
     
  7. Offline

    skipperguy12

    Something like what I posted above.
     
  8. Offline

    cdsboy2000

    I feel like I did this wrong. This is my onEnable()...
    Code:
    public void onEnable() {
                    logger.info("Aeroency enabled!");
                    Heal heal= new Heal();
                    getCommand("heal").setExecutor(Heal);
            }
    This is my Heal class...
    Code:
    package net.aeroency.aeroency;
     
    import java.util.logging.Logger;
    import java.util.Scanner;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Heal implements CommandExecutor {
    Is this wrong?
     
  9. Offline

    Froshin

  10. Offline

    skipperguy12

    cdsboy2000
    Heal class variable needs to be outside (above) the onEnable class, then in onEnable initialize the variable, so instead of Heal heal= new Heal();, you can do heal= new Heal();

     
  11. Offline

    cdsboy2000

    I made the CommandExecutor class and it works, but it says to put the commands there so it doesn't clutter up the main class. Can I make multiple CommandExecutor classes?
     
  12. Offline

    Skyshayde

    I would expect so, haven't tried it myself though. Have you tested it yet?
     
  13. Yes, yes you can. You can have a different class for every command
     
  14. Offline

    cdsboy2000

    I just tried to make a "Heal" class, here's the code...
    Code:
    package net.aeroency.aeroency;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class Heal implements CommandExecutor {
     
        private Aeroency plugin;
     
        public Heal(Aeroency plugin) {
            this.plugin = plugin;
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
             
                if(!(sender instanceof Player)) {
                 
                    if (args.length == 0) {
                     
                        sender.sendMessage("You're a console, you can't heal yourself.");
                     
                    }
                 
                    if (args.length == 1) {
                     
                        Player otherPlayer = Bukkit.getPlayer(args[0]);
                     
                        if (otherPlayer != null) {
                                otherPlayer.setHealth(20);
                                otherPlayer.setFoodLevel(20);
                                 
                                otherPlayer.sendMessage(ChatColor.GREEN + "You have been healed by the console.");
                                sender.sendMessage(ChatColor.GREEN + "You have healed " + otherPlayer.getName() + ".");
                             
                                }
                     
                        else {
                            sender.sendMessage("Error: Player not found");
                        }
                             
                                }
     
                    else {
                        sender.sendMessage("Usage: /heal <player>");
                    }
                 
                    }
             
                else {
             
                Player player = (Player) sender; 
                 
                if (!player.isOp()) {
                    sender.sendMessage(ChatColor.RED + "You don't have permission to heal!");
                }
             
                else {
             
                    if (args.length == 0) {
                               
                       
                            player.setHealth(20);
                            player.setFoodLevel(20);
                            player.sendMessage(ChatColor.GREEN + "You have been healed.");
                               
                            }
                       
                    }
               
                    if (args.length == 1) {
                       
                            Player otherPlayer = Bukkit.getPlayer(args[0]);
                         
                            if (otherPlayer != null) {
                                    otherPlayer.setHealth(20);
                                    otherPlayer.setFoodLevel(20);
                                 
                                    if (player.getName().equals(otherPlayer.getName())) {
                                        player.sendMessage(ChatColor.GREEN + "You have healed yourself.");
                                    }
                                 
                                    else {
                                     
                                    otherPlayer.sendMessage(ChatColor.GREEN + "You have been healed by " + player.getName() + ".");
                                    player.sendMessage(ChatColor.GREEN + "You have healed " + otherPlayer.getName() + ".");
                                 
                                    }
                            }
                       
                            else {
                                    sender.sendMessage(ChatColor.RED + "Error: Player not found!");
                            }
                       
                    }
               
                    if (args.length > 1) {
                            sender.sendMessage(ChatColor.RED + "Usage: /heal [name]");
                    }
               
            }
         
            return false;
        }
    
    I get an error in my main class saying "The constructor Heal(Aeroency) is undefined". Eclipse gives me the option to auto-create a constructor, but doing so gives the Heal class errors.

    Here's the main class...
    Code:
    package net.aeroency.aeroency;
     
    import java.util.logging.Logger;
    import java.util.Scanner;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Aeroency extends JavaPlugin{
     
            static Scanner scanner = new Scanner( System.in );
       
            public final Logger logger = Logger.getLogger("Minecraft");
         
            public void onEnable() {
           
                    logger.info("Aeroency enabled!");
                    getCommand("heal").setExecutor(new Heal (this));
            }
         
            public void onDisable() {
                    logger.info("Aeroency disabled.");
            }
         
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
               
                   
                   
                    /*
                    * STARVE COMMAND
                    */
                   
                    if (commandLabel.equalsIgnoreCase("starve")) {
                       
                        if(!(sender instanceof Player)) {
                           
                            if (args.length == 0) {
                               
                                sender.sendMessage("You're a console, you can't starve yourself.");
                               
                            }
                           
                            if (args.length == 1) {
                               
                                Player otherPlayer = Bukkit.getPlayer(args[0]);
                               
                                if (otherPlayer != null) {
                                        otherPlayer.setFoodLevel(0);
                                           
                                        otherPlayer.sendMessage(ChatColor.GREEN + "You have been starved by the console.");
                                        sender.sendMessage(ChatColor.GREEN + "You have starved " + otherPlayer.getName() + ".");
                                       
                                        }
                               
                                else {
                                    sender.sendMessage("Error: Player not found");
                                }
                                       
                                        }
     
                            else {
                                sender.sendMessage("Usage: /starve <player>");
                            } 
                           
                            }
                       
                        else {
                       
                        Player player = (Player) sender;   
                           
                        if (!player.isOp()) {
                            sender.sendMessage(ChatColor.RED + "You don't have permission to starve!");
                        }
                       
                        else {
                       
                            if (args.length == 0) {
                                         
                                    player.setFoodLevel(0);
                                    player.sendMessage(ChatColor.GREEN + "You have been starved.");
                                         
                                    }
                                 
                            }
                         
                            if (args.length == 1) {
                                 
                                    Player otherPlayer = Bukkit.getPlayer(args[0]);
                                   
                                    if (otherPlayer != null) {
                                            otherPlayer.setFoodLevel(0);
                                           
                                            if (player.getName().equals(otherPlayer.getName())) {
                                                player.sendMessage(ChatColor.GREEN + "You have starved yourself.");
                                            }
                                           
                                            else {
                                               
                                            otherPlayer.sendMessage(ChatColor.GREEN + "You have been starved by " + player.getName() + ".");
                                            player.sendMessage(ChatColor.GREEN + "You have starved " + otherPlayer.getName() + ".");
                                           
                                            }
                                    }
                                 
                                    else {
                                            sender.sendMessage(ChatColor.RED + "Error: Player not found!");
                                    }
                                 
                            }
                         
                            if (args.length > 1) {
                                    sender.sendMessage(ChatColor.RED + "Usage: /starve [name]");
                            }
                         
                    }
                    }
                   
                    /*
                    * BRING COMMAND
                    */
                   
                    if (commandLabel.equalsIgnoreCase("bring")) {
                       
                        if(!(sender instanceof Player)) {
                           
                            sender.sendMessage("You're a console, you can't bring people to your dimension.");
                           
                            }
                       
                        else {
                       
                        Player player = (Player) sender;   
                           
                        if (!player.isOp()) {
                            sender.sendMessage(ChatColor.RED + "You don't have permission warp people.");
                        }
                       
                        else {
                       
                            if (args.length == 0) {
                                         
                                    sender.sendMessage(ChatColor.RED + "Usage: /bring <name>");
                                         
                                    }
                                 
                            }
                         
                            if (args.length == 1) {
                                 
                                    Player otherPlayer = Bukkit.getPlayer(args[0]);
                                   
                                    if (otherPlayer != null) {
                                           
                                            if (player.getName().equals(otherPlayer.getName())) {
                                               
                                                player.sendMessage("You can't summon yourself!");
                                               
                                            }
                                           
                                            else {
     
                                                otherPlayer.teleport(player.getLocation());
                                                otherPlayer.sendMessage(ChatColor.GREEN + "You have been summoned by " + player.getName() + ".");
                                           
                                            }
                                    }
                                 
                                    else {
                                            sender.sendMessage(ChatColor.RED + "Error: Player not found!");
                                    }
                                 
                            }
                         
                            if (args.length > 1) {
                                    sender.sendMessage(ChatColor.RED + "Usage: /bring <name>");
                            }
                         
                    }
                    }
           
                 
                    return false;
            }
    }
     
  15. Have you saved all the resources? Sometimes eclipse goes crazy :p
     
  16. Offline

    cdsboy2000

    Lol, that was the problem. It's funny how simple things like that destroy the whole program. Thanks so much for your help.
     
  17. No problem at all.
     
  18. Offline

    cdsboy2000

    Oh noez! I tried to export the .jar file, and I got this error!

    Can someone try to run my code (its in a previous post) and see what's wrong?
    Thank you in advance. :)
     
  19. Offline

    Skyshayde

    Warnings and errors are entirely different things. Warnings are things like unused variables. Try running the plugin and see what happens?
     
  20. Offline

    cdsboy2000

    Wow, it works perfectly. I should've tried that before bothering you guys. Sorry! D:
     
Thread Status:
Not open for further replies.

Share This Page