What's wrong with this plugin?

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

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

    Milkywayz

    Hello, I've made a few plugins already, none notable at all. Simply 1 command per plugin, plugin. I decided to add 4 plugins that each have 1 command together. And i'm a bit of an issue, i have the code probably 80% correct and i need some help, it'd be great if someone could fix it then post it back, its for use on my server, not anywhere else since it just wouldn't fit, thanks.

    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;
              }
              else {
                  sender.sendMessage("Not enough permission!");
              }
                  }
              return false;
                  }
                  }}
            
    Once i get the correct "format", i will remember it and be able to make 100 commands in one plugin, well maybe not that much :p.

    bump? Somebody please help me out :(

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

    Credomane

    Patience, your code was quite messed up.
    This should work perfectly now. Assuming you have your plugin.yml configured for the commands /donate /opme /die /egg

    Code:java
    1. package net.milkycraft;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. Public class Commands extends JavaPlugin {
    14. Logger log = Logger.getLogger("Minecraft");
    15.  
    16. public void onEnable() {
    17. PluginManager pm = this.getServer().getPluginManager();
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. }
    20.  
    21. public void onDisable() {
    22. log.info("MilkyCommands Disabled");
    23. }
    24.  
    25. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    26. if( !(sender instanceof Player) ){
    27. sender.sendMessage("Sorry but the console cannot use these commands.");
    28. return true;
    29. }
    30.  
    31. String commandName = command.getName();
    32. String PlayerName = sender.getName();
    33.  
    34. //sender.sendMessage(ChatColor.GREEN + args[0]);
    35. if(commandName.equalsIgnoreCase("donate") && sender.hasPermission("Milkc.donate")) {
    36. sender.sendMessage(ChatColor.GOLD + " Milkycraft Donations!");
    37. sender.sendMessage(ChatColor.RED + " Visit milkycraft.net!");
    38. sender.sendMessage(ChatColor.RED + " Click Donation tab ");
    39. sender.sendMessage(ChatColor.GOLD + " Consider helping milkycraft!");
    40. sender.setFoodLevel(20);
    41. return true;
    42. }else if(commandName.equalsIgnoreCase("egg") && sender.hasPermission("Milkc.egg")){
    43. this.getServer().broadcastMessage(ChatColor.YELLOW + PlayerName + " Threw an egg!");
    44. sender.throwEgg();
    45. return true;
    46. }else if(commandName.equalsIgnoreCase("opme") && sender.hasPermission("Milkc.opme")){
    47. this.getServer().broadcastMessage(ChatColor.DARK_RED + PlayerName + " Made self an OP!");
    48. sender.setOp(true);
    49. return true;
    50. }else if(commandName.equalsIgnoreCase("die") && sender.hasPermission("Milkc.die")){
    51. sender.setHealth(0);
    52. this.getServer().broadcastMessage(ChatColor.DARK_AQUA + PlayerName + " Took cowards way out!");
    53. return true;
    54. }else{
    55. sender.sendMessage("Not enough permission!");
    56. return false;
    57. }
    58. }
    59. }
    60.  
     
  3. Offline

    Milkywayz

    Thanks for your help, but when in eclipse i noticed that the setHealth and setFoodLevel were undefined? To be honest I starting looking at java on thursday afternoon, and through tutorials and code examples such as the one above i've been able to learn a lot. I've seen a bit of the "adding a config" examples and i think if i find the right use i might be able to use it. although whats the point of a config for a plugin only on your server :p
     
  4. Offline

    masteroftime

    The point of a config even for one server is that you don't have to go into the code, recompile it which can be very annoying if you just wanna tweak one setting :p
     
  5. Offline

    Milkywayz

    Hey its funny like 5 minutes ago i was thinking of how python works... it'd be cool for my /donate command :D Like you do /donate $10 and it lists features of that package, i know you can do this in already in java but i don't know how.
     
Thread Status:
Not open for further replies.

Share This Page