Template needed for plugin with command(S)

Discussion in 'Plugin Development' started by Milkywayz, Jan 27, 2012.

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

    Milkywayz

    Alright so what I did was make a few "stupid" commands for my server to get the hang on making plugins. /egg, /donate, /opme, and /die. /opme for co-owners so if they de-op they can reop incase they wanna play legit. Anyways I had 4 separate plugins, 4 different plugin.ymls, and 4 different permissions, what i attempted to do was combine it all into 1 plugin, all i did was take the method (i think) from each and paste it in, there was no compiling errors. What i thought i could do once the commands that i used were very messed up, /die would display the { else } for /donate?, would be maybe have donate.java, opme.java, egg.java, and die.java. But that wouldnt work because the plugin.yml wants one main, and that'd be four. So next i thought i could have 1 main file, then have the 4 others who the main imports its stuff from. like what you do for a block listener. But anyways I'm stuck there. I just want to make some commands for my public but private use (i wouldn't realease it since it'd be not that cool, and it has no config so the people would get my messages) and i just need some help. I do good with like an outline of what i should do, I don't want to copy it i just want a template to fill in if that's possible, all help will be greatly appreciated. P.S. If you want my code for it just ask, ill reply with it. *EDIT* I know the template for 1 command in a plugin, but if i add more then 1 command it gets hella screwy.

    Code:
    package net.milkycraft;
      import java.util.logging.Logger;
     
      import org.bukkit.ChatColor;
      import org.bukkit.command.Command;
      import org.bukkit.command.CommandSender;
      import org.bukkit.entity.Player;
      import org.bukkit.plugin.PluginDescriptionFile;
      import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
      public class Commands extends JavaPlugin
      {
          Logger log = Logger.getLogger("Minecraft");
          public void onDisable()
          {
              log.info("MilkyCommands Disabled");
          }
          public void onEnable()
          {
              PluginManager pm = this.getServer().getPluginManager();
              PluginDescriptionFile pdfFile = this.getDescription();
          }
          @Override // Donate command
          public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
          {
              String commandName = command.getName().toLowerCase();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandName.equals("donate") && sender.hasPermission("Milkc.donate")) {
              player.sendMessage(ChatColor.GOLD + " Milkycraft Donations!");
              player.sendMessage(ChatColor.RED + " Visit milkycraft.net!");
              player.sendMessage(ChatColor.RED + " Click Donation tab ");
              player.sendMessage(ChatColor.GOLD + " Consider helping milkycraft!");
              return true;
          }
          else {
              player.sendMessage(" No permission to view donate"); }
              return false;
          }
          public void test(CommandSender sender, String[] args)
          {
              Player player = (Player)sender; // This is your player object
              //You can now use this to manipulate the player
              player.setFoodLevel(20); //example of manipulation
          }
          // Egg command
          public boolean onCommand1(CommandSender sender, Command command, String commandLabel, String[] args)
          {
              String commandName = command.getName().toLowerCase();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              String PlayerName = player.getName();
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandName.equals("egg") && sender.hasPermission("Milkc.egg")) {
              this.getServer().broadcastMessage(ChatColor.YELLOW + PlayerName + " Threw an egg!");
              return true;
          }
          else {
              player.sendMessage(" Donate to throw an egg!"); }
              return false;
          }
          public void test1(CommandSender sender, String[] args)
          {
              Player player = (Player)sender; // This is your player object
              //You can now use this to manipulate the player
              player.throwEgg(); //example of manipulation
          }
         
          public boolean onCommand2(CommandSender sender, Command command, String commandLabel, String[] args)
          {
              String commandName = command.getName().toLowerCase();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              String PlayerName = player.getName();
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandName.equals("opme") && sender.hasPermission("Milkc.opme")) {
              this.getServer().broadcastMessage(ChatColor.DARK_RED + PlayerName + " Made self an OP!");
              return true;
          }
          else {
              player.sendMessage(ChatColor.RED + " Not enough permission to OP!"); }
              return false;
          }
          public void test21(CommandSender sender, String[] args)
          {
              Player player = (Player)sender; // This is your player object
              //You can now use this to manipulate the player
              player.setOp(true); //example of manipulation
          }
          public boolean onCommand21(CommandSender sender, Command command, String commandLabel, String[] args)
          {
              String commandName = command.getName().toLowerCase();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              String PlayerName = player.getName();
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandName.equals("die") && sender.hasPermission("Milkc.die")) {
              this.getServer().broadcastMessage(ChatColor.DARK_AQUA + PlayerName + " Took cowards way out!");
              return true;
          }
          else {
              player.sendMessage(ChatColor.RED + " Failed to kill yourself, lol"); }
              return false;
          }
          public void test2(CommandSender sender, String[] args)
          {
              Player player = (Player)sender; // This is your player object
              //You can now use this to manipulate the player
              player.setHealth(0); //example of manipulation
          }
         
      }
    Bump?, After looking at my code, i've realized its rather messy, once i get it working ill add more comments and make it more neat. :p For example the public void (test / on command) is very unorganized and probably caused the problem.

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

    caldabeast

    Probably because you changed to to "onCommandx". Im pretty sure you need to leave it at onCommand. Instead of doing that, just use multiple "if(command.equalsIgnoreCase("egg") {}" instead.
     
  3. Offline

    Milkywayz

    Thanks, ill give that a try then re-post code and results.

    Ok so i went through and added that line you suggest, but i also had to change the string for it, And i also changed the public void test to public void <command>, because i don't think it should be called test, correct me if I'm wrong because what i used for the template was a test plugin. May i add that the public boolean... line is red and has errors since they are the same still, any other suggestions?

    Code:
    package net.milkycraft;
      import java.util.logging.Logger;
      import org.bukkit.ChatColor;
      import org.bukkit.command.Command;
      import org.bukkit.command.CommandSender;
      import org.bukkit.entity.Player;
      import org.bukkit.plugin.PluginDescriptionFile;
      import org.bukkit.plugin.PluginManager;
      import org.bukkit.plugin.java.JavaPlugin;
      public class Commands extends JavaPlugin
      {
          Logger log = Logger.getLogger("Minecraft");
          public void onDisable()
          {
              log.info("MilkyCommands Disabled");
          }
          public void onEnable()
          {
              PluginManager pm = this.getServer().getPluginManager();
              PluginDescriptionFile pdfFile = this.getDescription();
          }
          @Override // Donate command
          public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
          {
              String commandN = command.getName();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandN.equalsIgnoreCase("donate") && sender.hasPermission("Milkc.donate")) {
              player.sendMessage(ChatColor.GOLD + " Milkycraft Donations!");
              player.sendMessage(ChatColor.RED + " Visit milkycraft.net!");
              player.sendMessage(ChatColor.RED + " Click Donation tab ");
              player.sendMessage(ChatColor.GOLD + " Consider helping milkycraft!");
              return true;
          }
          else {
              player.sendMessage(" No permission to view donate"); }
              return false;
          }
          public void donate(CommandSender sender, String[] args)
          {
              Player player = (Player)sender; // This is your player object
              //You can now use this to manipulate the player
              player.setFoodLevel(20); //example of manipulation
          }
          // Egg command
          public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
          {
              String commandN = command.getName();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              String PlayerName = player.getName();
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandN.equalsIgnoreCase("egg") && sender.hasPermission("Milkc.egg")) {
              this.getServer().broadcastMessage(ChatColor.YELLOW + PlayerName + " Threw an egg!");
              return true;
          }
          else {
              player.sendMessage(" Donate to throw an egg!"); }
              return false;
          }
          public void egg(CommandSender sender, String[] args)
          {
              Player player = (Player)sender;
              player.throwEgg();
          }
          // Opme command
          public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
          {
              String commandN = command.getName();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              String PlayerName = player.getName();
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandN.equalsIgnoreCase("opme") && sender.hasPermission("Milkc.opme")) {
              this.getServer().broadcastMessage(ChatColor.DARK_RED + PlayerName + " Made self an OP!");
              return true;
          }
          else {
              player.sendMessage(ChatColor.RED + " Not enough permission to OP!"); }
              return false;
          }
          public void opme(CommandSender sender, String[] args)
          {
              Player player = (Player)sender;
              player.setOp(true);
          }
          public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
          {
              String commandN = command.getName();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              String PlayerName = player.getName();
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandN.equalsIgnoreCase("die") && sender.hasPermission("Milkc.die")) {
              this.getServer().broadcastMessage(ChatColor.DARK_AQUA + PlayerName + " Took cowards way out!");
              return true;
          }
          else {
              player.sendMessage(ChatColor.RED + " Failed to kill yourself, lol"); }
              return false;
          }
          public void die(CommandSender sender, String[] args)
          {
              Player player = (Player)sender; // This is your player object
              //You can now use this to manipulate the player
              player.setHealth(0); //example of manipulation
          }
       
      }
    Im sorry if this code is riddled with errors, but I'm trying to learn how to do this, i mean honestly who wants a plugin for every command? Essentials would have over 100 jars...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
    caldabeast likes this.
  4. Offline

    caldabeast

    My suggestion was to put them all in the same onCommand(~,~,~,~){} area with the if(~~~){} separating them.
     
  5. Offline

    Milkywayz

    At first i was like.. wut?, then i figured out what you meant and I'm gonna try it out, thanks for the help man no one else came to help
     
  6. Offline

    ThatBox

    You need an else statement. I wrote out the code but bukkit's [!syntax] things messed it up :/
     
  7. Offline

    Milkywayz

    This is what i have so far, whenever i try to add the else its just kinda bugged.
    Code:
    package net.milkycraft;
      import java.util.logging.Logger;
      import org.bukkit.ChatColor;
      import org.bukkit.command.Command;
      import org.bukkit.command.CommandSender;
      import org.bukkit.entity.Player;
      import org.bukkit.plugin.PluginDescriptionFile;
      import org.bukkit.plugin.PluginManager;
      import org.bukkit.plugin.java.JavaPlugin;
      public class Commands extends JavaPlugin
      {
          Logger log = Logger.getLogger("Minecraft");
          public void onDisable()
          {
              log.info("MilkyCommands Disabled");
          }
          public void onEnable()
          {
              PluginManager pm = this.getServer().getPluginManager();
              PluginDescriptionFile pdfFile = this.getDescription();
          }
          @Override // Commands
          public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
          {
              String commandName = command.getName();
              String[] trimmedArgs = args;
              Player player = ((Player)sender);
              String PlayerName  = player.getName();
              //sender.sendMessage(ChatColor.GREEN + trimmedArgs[0]);
              if(commandName.equalsIgnoreCase("donate") && sender.hasPermission("Milkc.donate")) {
              player.sendMessage(ChatColor.GOLD + " Milkycraft Donations!");
              player.sendMessage(ChatColor.RED + " Visit milkycraft.net!");
              player.sendMessage(ChatColor.RED + " Click Donation tab ");
              player.sendMessage(ChatColor.GOLD + " Consider helping milkycraft!");
              player.setFoodLevel(20);
              }
              if(commandName.equalsIgnoreCase("egg") && sender.hasPermission("Milkc.egg")) {
              this.getServer().broadcastMessage(ChatColor.YELLOW + PlayerName + " Threw an egg!");
              player.throwEgg();
              }
              if(commandName.equalsIgnoreCase("opme") && sender.hasPermission("Milkc.opme")) {
              this.getServer().broadcastMessage(ChatColor.DARK_RED + PlayerName + " Made self an OP!");
              player.setOp(true);
              }
              if(commandName.equalsIgnoreCase("die") && sender.hasPermission("Milkc.die"))  {
              player.setHealth(0);
              this.getServer().broadcastMessage(ChatColor.DARK_AQUA + PlayerName + " Took cowards way out!");
              return true;
              }
     
            
    At line 21 theres a syntax error, and at the end its syntax crazy, this is why i need structure :p
     
  8. Offline

    ThatBox

    Before the command if's (except the first one) put else.
     
  9. Offline

    IcyRelic

    remove @Override

    also make it like this u dont need an else or an elseif
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
     
            if (cmd.getName().equalsIgnoreCase("command1")) {
                  //do something
           }
     
            if (cmd.getName().equalsIgnoreCase("command2")) {
                //do something 
           }
     
            if (cmd.getName().equalsIgnoreCase("command3")) {
                //do something 
           }
     
            if (cmd.getName().equalsIgnoreCase("command4")) {
                //do something 
           }
     
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  10. you need @Override because you override the onCommand-function from the command executor class.
    it's fine to use else if. It depends on the coding style. If you want to use else if, you can (in this case). If you find it easier to ready why shouldn't you use else if?

    BTT: You only return a value on time (in your last if statement) either return a value in all if's or make a return at the end of the function.
     
  11. Offline

    Aetherspawn

    else/if is actually marginally faster. Not enough to matter, but marginally.
     
  12. Offline

    IcyRelic

    i have NEVER used @Override onCommand and it works fine so i have no idea what your talking about
     
  13. Offline

    Milkywayz

    Alright I "fixed " the code, still errors though, sender.setHealth for example setHealth is red.
    Code:
    package net.milkycraft;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Commands extends JavaPlugin {
        Logger log = Logger.getLogger("Minecraft");
     
        public void onEnable() {
            PluginManager pm = this.getServer().getPluginManager();
            PluginDescriptionFile pdfFile = this.getDescription();
        }
     
        public void onDisable() {
            log.info("MilkyCommands Disabled");
        }
     
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            if( !(sender instanceof Player) ){
                sender.sendMessage("Sorry but the console cannot use these commands.");
                return true;
            }
     
            String commandName = command.getName();
            String PlayerName  = sender.getName();
     
            //sender.sendMessage(ChatColor.GREEN + args[0]);
            if(commandName.equalsIgnoreCase("donate") && sender.hasPermission("Milkc.donate")) {
                sender.sendMessage(ChatColor.GOLD + " Milkycraft Donations!");
                sender.sendMessage(ChatColor.RED + " Visit milkycraft.net!");
                sender.sendMessage(ChatColor.RED + " Click Donation tab ");
                sender.sendMessage(ChatColor.RED + " Consider donating to milkycraft!");
                sender.sendMessage(ChatColor.GOLD + " All proceedings go to the server!");
                sender.setFoodLevel(20);
                return true;
            }else if(commandName.equalsIgnoreCase("egg") && sender.hasPermission("Milkc.egg")){
                this.getServer().broadcastMessage(ChatColor.YELLOW + PlayerName + " Threw an egg!");
                sender.throwEgg();
                return true;
            }else if(commandName.equalsIgnoreCase("opme") && sender.hasPermission("Milkc.opme")){
                this.getServer().broadcastMessage(ChatColor.DARK_RED + PlayerName + " Made self an OP!");
                sender.setOp(true);
                return true;
            }else if(commandName.equalsIgnoreCase("die") && sender.hasPermission("Milkc.die")){
              sender.setHealth(0);     
                this.getServer().broadcastMessage(ChatColor.DARK_AQUA + PlayerName + " Took cowards way out!");
                return true;
            }else{
                sender.sendMessage("Not enough permission to use this command!");
                return false;
            }
        }
    }
            
     
  14. i think you can only use setHealth for a player so type-cast it
    Code:
    ((Player)sender).setHealth(0);
     
  15. Offline

    Technius

    You should check if it's a player before you do anything player related. Do so like this:
    Code:java
    1. Player player = null;
    2. if(sender instanceof Player)player = (Player) sender;


    And from that code, check if it's a player
    Code:java
    1. if(player == null)//if the player is the console
    2. {
    3. //do something
    4. }
    5. else if(player.hasPermission("somepermission.node")//we know it's a player, so we check for permissions
    6. {
    7. //do something
    8. }
    9. else //we know that they aren't allowed to do this
    10. {
    11. //tell them they have no permission
    12. }
     
  16. Offline

    Milkywayz

    Wow Thanks so much, error free code, time to test it out, ill post results :D

    You sir are my hero, thank you. Everyone thank you for your help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  17. sure, but since at the beginning of the command he's checking if it's not a player and if so return. meaning that it only can be a player if the code is executes further.
     
Thread Status:
Not open for further replies.

Share This Page