[BUG] First plugin - Help with the coding

Discussion in 'Plugin Development' started by linkrock4, Nov 5, 2012.

Thread Status:
Not open for further replies.
  1. Hello, i started learning java 2 days ago and started making this bukkit plugin, i dont understand alot but there is a bug in my coding and i need help fixing it

    CODE:
    Code:
    package net.skydevs.src;
     
    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 ChatPlugin extends JavaPlugin{
       
        public void onEnable(){ //Server Starts
            getLogger().info("ChatPlugin has loaded!");
        }
       
        public void onDisable(){ //Server Stops
            getLogger().info("ChatPlugin has been disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            //Code
     
            if (cmd.getName().equalsIgnoreCase("kiss") && args.length == 1) {
                Player kisser = (Player)sender;
                String kisserName = kisser.getName();
               
                String kissedName = args[0];
                Player kissed = kisser.getServer().getPlayer(kissedName);
               
                if(kissed == null){
                    //Code
                    kisser.sendMessage(ChatColor.DARK_RED + "You cannot kiss an offline player!");
                } else {
                    //Code for Kiss
                    int currentHealth = kissed.getHealth();
                    currentHealth++;
                    kissed.setHealth(currentHealth);
                   
                    kisser.sendMessage(ChatColor.DARK_RED + "You have kissed " + kissedName);
                    kissed.sendMessage(ChatColor.DARK_RED + "You have been kissed by " + kisserName);
                   
                }
               
            }
           
            if (cmd.getName().equalsIgnoreCase("kick") && args.length == 1) {
                Player kicker = (Player)sender;
                String kickerName = kicker.getName();
               
                String kickedName = args[0];
                Player kicked = kicker.getServer().getPlayer(kickedName);
               
                if(kicked == null){
                    //Code
                    kicker.sendMessage(ChatColor.DARK_RED + "You cannot kick an offline player!");
                } else {
                    //Code for Kicks
                    int currentHealth = kicked.getHealth();
                    currentHealth--;
                    kicked.setHealth(currentHealth);
                   
                    kicker.sendMessage(ChatColor.DARK_RED + "You have kicked " + kickedName);
                    kicked.sendMessage(ChatColor.DARK_RED + "You have been kicked by " + kickerName);
                   
                }
               
            }
                   
            if (cmd.getName().equalsIgnoreCase("hug") && args.length == 1) {
                Player hugger = (Player)sender;
                String huggerName = hugger.getName();
               
                String huggeeName = args[0];
                Player huggee = hugger.getServer().getPlayer(huggeeName);
               
                if(huggee == null){
                    //Code
                    hugger.sendMessage(ChatColor.DARK_RED + "You cannot hug an offline player!");
                } else {
                    //Code for Hugs
                    int currentHealth = huggee.getHealth();
                    currentHealth++;
                    huggee.setHealth(currentHealth);
                   
                    hugger.sendMessage(ChatColor.DARK_RED + "You have hugged " + huggeeName);
                    huggee.sendMessage(ChatColor.DARK_RED + "You have been hugged by " + huggerName);
                   
                }
               
            }
           
            return false;
        }
       
    }
    THANK YOU!
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    Do you know what is wrong with it?
     
  3. No, thats why im here ._.
    Do you need the error it gives in the console when i type in the command?
     
  4. For a beginner in Java and Bukkit: good job :3. Now lets get to business: you forget to actually check if the commandSender is a Player before you cast it to Player. I have got 2 solutions:
    Player only command:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. //Code
    3. if(!(sender instanceof Player))
    4. {
    5. sender.sendMessage("This is a player-only command");
    6. return true;
    7. }
    8. Player p = (Player) sender;
    9. String pName = p.getName();
    10. if (cmd.getName().equalsIgnoreCase("kiss") && args.length == 1) {
    11. String kissedName = args[0];
    12. Player kissed = p.getServer().getPlayer(kissedName);
    13.  
    14. if(kissed == null){
    15. //Code
    16. p.sendMessage(ChatColor.DARK_RED + "You cannot kiss an offline player!");
    17. } else {
    18. //Code for Kiss
    19. int currentHealth = kissed.getHealth();
    20. currentHealth++;
    21. kissed.setHealth(currentHealth);
    22.  
    23. p.sendMessage(ChatColor.DARK_RED + "You have kissed " + kissedName);
    24. kissed.sendMessage(ChatColor.DARK_RED + "You have been kissed by " + pName);
    25.  
    26. }
    27.  
    28. }
    29.  
    30. if (cmd.getName().equalsIgnoreCase("kick") && args.length == 1) {
    31. String kickedName = args[0];
    32. Player kicked = p.getServer().getPlayer(kickedName);
    33.  
    34. if(kicked == null){
    35. //Code
    36. p.sendMessage(ChatColor.DARK_RED + "You cannot kick an offline player!");
    37. } else {
    38. //Code for Kicks
    39. int currentHealth = kicked.getHealth();
    40. currentHealth--;
    41. kicked.setHealth(currentHealth);
    42.  
    43. p.sendMessage(ChatColor.DARK_RED + "You have kicked " + kickedName);
    44. kicked.sendMessage(ChatColor.DARK_RED + "You have been kicked by " + pName);
    45.  
    46. }
    47.  
    48. }
    49.  
    50. if (cmd.getName().equalsIgnoreCase("hug") && args.length == 1) {
    51. String huggeeName = args[0];
    52. Player huggee = p.getServer().getPlayer(huggeeName);
    53.  
    54. if(huggee == null){
    55. //Code
    56. p.sendMessage(ChatColor.DARK_RED + "You cannot hug an offline player!");
    57. } else {
    58. //Code for Hugs
    59. int currentHealth = huggee.getHealth();
    60. currentHealth++;
    61. huggee.setHealth(currentHealth);
    62.  
    63. p.sendMessage(ChatColor.DARK_RED + "You have hugged " + huggeeName);
    64. huggee.sendMessage(ChatColor.DARK_RED + "You have been hugged by " + pName);
    65.  
    66. }
    67.  
    68. }
    69.  
    70. return false;
    71. }
    72.  
    73. }

    Allowing console to perform the commands:
    Code:java
    1. package net.skydevs.src;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class ChatPlugin extends JavaPlugin{
    10.  
    11. public void onEnable(){ //Server Starts
    12. getLogger().info("ChatPlugin has loaded!");
    13. }
    14.  
    15. public void onDisable(){ //Server Stops
    16. getLogger().info("ChatPlugin has been disabled!");
    17. }
    18.  
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    20. //Code
    21.  
    22. String senderName = sender.getName();
    23.  
    24. if (cmd.getName().equalsIgnoreCase("kiss") && args.length == 1) {
    25.  
    26. String kissedName = args[0];
    27. Player kissed = sender.getServer().getPlayer(kissedName);
    28.  
    29. if(kissed == null){
    30. //Code
    31. sender.sendMessage(ChatColor.DARK_RED + "You cannot kiss an offline player!");
    32. } else {
    33. //Code for Kiss
    34. int currentHealth = kissed.getHealth();
    35. currentHealth++;
    36. kissed.setHealth(currentHealth);
    37.  
    38. sender.sendMessage(ChatColor.DARK_RED + "You have kissed " + kissedName);
    39. kissed.sendMessage(ChatColor.DARK_RED + "You have been kissed by " + senderName);
    40.  
    41. }
    42.  
    43. }
    44.  
    45. if (cmd.getName().equalsIgnoreCase("kick") && args.length == 1) {
    46. String kickedName = args[0];
    47. Player kicked = sender.getServer().getPlayer(kickedName);
    48.  
    49. if(kicked == null){
    50. //Code
    51. sender.sendMessage(ChatColor.DARK_RED + "You cannot kick an offline player!");
    52. } else {
    53. //Code for Kicks
    54. int currentHealth = kicked.getHealth();
    55. currentHealth--;
    56. kicked.setHealth(currentHealth);
    57.  
    58. sender.sendMessage(ChatColor.DARK_RED + "You have kicked " + kickedName);
    59. kicked.sendMessage(ChatColor.DARK_RED + "You have been kicked by " + senderName);
    60.  
    61. }
    62.  
    63. }
    64.  
    65. if (cmd.getName().equalsIgnoreCase("hug") && args.length == 1) {
    66. String huggeeName = args[0];
    67. Player huggee = sender.getServer().getPlayer(huggeeName);
    68.  
    69. if(huggee == null){
    70. //Code
    71. sender.sendMessage(ChatColor.DARK_RED + "You cannot hug an offline player!");
    72. } else {
    73. //Code for Hugs
    74. int currentHealth = huggee.getHealth();
    75. currentHealth++;
    76. huggee.setHealth(currentHealth);
    77.  
    78. sender.sendMessage(ChatColor.DARK_RED + "You have hugged " + huggeeName);
    79. huggee.sendMessage(ChatColor.DARK_RED + "You have been hugged by " + senderName);
    80.  
    81. }
    82.  
    83. }
    84.  
    85. return false;
    86. }
    87.  
    88. }

    To both I added a little improvement (only 1 sender assignment, saves some memory and lines of code :3)
     
  5. Thanks alot man! Its really appreciated! :D

    I just tried the code and i get an internal error :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  6. post the error on here.
    also remove the logger its unneeded. bukkit has a built in logger now
     
  7. ERROR:
    Code:
    2012-11-05 17:57:10 [INFO] Starting minecraft server version 1.4.2
    2012-11-05 17:57:10 [INFO] Loading properties
    2012-11-05 17:57:10 [INFO] Default game type: SURVIVAL
    2012-11-05 17:57:10 [INFO] Generating keypair
    2012-11-05 17:57:11 [INFO] Starting Minecraft server on *:25565
    2012-11-05 17:57:11 [INFO] This server is running CraftBukkit version git-Bukkit-1.4.2-R0.1-6-g4a656a3-b2453jnks (MC: 1.4.2) (Implementing API version 1.4.2-R0.2-SNAPSHOT)
    2012-11-05 17:57:11 [INFO] [ChatPlugin] Loading ChatPlugin v1.3
    2012-11-05 17:57:11 [INFO] Preparing level "world"
    2012-11-05 17:57:11 [INFO] Preparing start region for level 0 (Seed: 3769289623228791077)
    2012-11-05 17:57:12 [INFO] ----- Bukkit Auto Updater -----
    2012-11-05 17:57:12 [INFO] It appears that you're running a Development Build, when you've specified in bukkit.yml that you prefer to run Recommended Builds.
    2012-11-05 17:57:12 [INFO] If you would like to be kept informed about new Development Build releases, it is recommended that you change 'preferred-channel' in your bukkit.yml to 'dev'.
    2012-11-05 17:57:12 [INFO] With that set, you will be told whenever a new version is available for download, so that you can always keep up to date and secure with the latest fixes.
    2012-11-05 17:57:12 [INFO] If you would like to disable this warning, simply set 'suggest-channels' to false in bukkit.yml.
    2012-11-05 17:57:12 [INFO] ----- ------------------- -----
    2012-11-05 17:57:12 [INFO] Preparing spawn area: 16%
    2012-11-05 17:57:13 [INFO] Preparing spawn area: 40%
    2012-11-05 17:57:14 [INFO] Preparing spawn area: 57%
    2012-11-05 17:57:15 [INFO] Preparing start region for level 1 (Seed: 3769289623228791077)
    2012-11-05 17:57:16 [INFO] Preparing spawn area: 0%
    2012-11-05 17:57:17 [INFO] Preparing spawn area: 16%
    2012-11-05 17:57:18 [INFO] Preparing spawn area: 32%
    2012-11-05 17:57:19 [INFO] Preparing spawn area: 48%
    2012-11-05 17:57:20 [INFO] Preparing spawn area: 61%
    2012-11-05 17:57:21 [INFO] Preparing spawn area: 77%
    2012-11-05 17:57:22 [INFO] Preparing spawn area: 93%
    2012-11-05 17:57:22 [INFO] Preparing start region for level 2 (Seed: 3769289623228791077)
    2012-11-05 17:57:23 [INFO] Preparing spawn area: 16%
    2012-11-05 17:57:24 [INFO] Preparing spawn area: 52%
    2012-11-05 17:57:25 [INFO] Preparing spawn area: 85%
    2012-11-05 17:57:25 [INFO] [ChatPlugin] Enabling ChatPlugin v1.3
    2012-11-05 17:57:25 [INFO] [ChatPlugin] ChatPlugin has loaded!
    2012-11-05 17:57:25 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2012-11-05 17:57:25 [INFO] Done (14.024s)! For help, type "help" or "?"
    2012-11-05 17:57:41 [INFO] linkrock4[/127.0.0.1:56423] logged in with entity id 1390 at ([world] 181.22695137888684, 67.0, 312.09647726987487)
    2012-11-05 17:57:44 [INFO] linkrock4 issued server command: /kick
    2012-11-05 17:57:50 [INFO] linkrock4 issued server command: /kick linkrock4
    2012-11-05 17:57:54 [INFO] linkrock4 issued server command: /kiss linkrock4
    2012-11-05 17:57:54 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'kiss' in plugin ChatPlugin v1.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:185)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:504)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:911)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:824)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:806)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:569)
        at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:215)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:486)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:818)
    Caused by: java.lang.IllegalArgumentException: Health must be between 0 and 20
        at org.bukkit.craftbukkit.entity.CraftLivingEntity.setHealth(CraftLivingEntity.java:59)
        at net.skydevs.src.ChatPlugin.onCommand(ChatPlugin.java:36)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    2012-11-05 17:57:59 [INFO] linkrock4 issued server command: /hug linkrock4
    2012-11-05 17:57:59 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'hug' in plugin ChatPlugin v1.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:185)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:504)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:911)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:824)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:806)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:569)
        at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:215)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:486)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:818)
    Caused by: java.lang.IllegalArgumentException: Health must be between 0 and 20
        at org.bukkit.craftbukkit.entity.CraftLivingEntity.setHealth(CraftLivingEntity.java:59)
        at net.skydevs.src.ChatPlugin.onCommand(ChatPlugin.java:76)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    And umm, what logger?
     
  8. ok so the problem is the health. before doing the "currentHealth++;" check if it is < 20
    remove these "getLogger().info("ChatPlugin has loaded!");"
     
  9. Im really new to coding and i dont know how to check if it is < 20
     
  10. if (currentHealth < 20)
    {
    currentHealth++;
    }
     
  11. linkrock4 add the same for the hug command, and the inverse for the kick:
    Code:java
    1. if(currentHealth > 0) // Warning, this will kill the player
    2. {
    3. currentHealth--;
    4. }
     
  12. Offline

    gamerzap

    If I were you, I'd also avoid calling the command "kick". It could cause problems, as most plugins use "kick" to kick a player off the server.
     
  13. IT WORKS! Thanks very much guys! :D

    --- -- SOLVED -- ---

    And yes, these are just temp commands :p it will most likely be /cp kick... ect... (cp=chat plugin)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page