Not sure whats wrong...

Discussion in 'Plugin Development' started by cheer60823, Jul 6, 2013.

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

    cheer60823

    I've been creating a plugin, and I've had a lot of help so far, so I would like to thank you awesome people! But... theres always more problems.

    Code:java
    1. package me.Cheer60823.TheCowPlugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Chicken;
    7. import org.bukkit.entity.Cow;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.entity.Squid;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.potion.PotionEffect;
    12. import org.bukkit.potion.PotionEffectType;
    13.  
    14.  
    15.  
    16. public class TheCowPlugin extends JavaPlugin
    17. {
    18. public void onEnable() {
    19. System.out.println("TCP Enabled");
    20. }
    21.  
    22. public void onDisable() {
    23. System.out.println("TCP Disabled");
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27. if(cmd.getLabel().equalsIgnoreCase("Cow")) {
    28. if(sender instanceof Player) {
    29. Player player = (Player) sender;
    30. player.getWorld().spawn(player.getLocation(), Cow.class); Not sure!
    31. player.sendMessage("A cow has been spawened at your postition.");
    32. }
    33. else {
    34. sender.sendMessage("Only players can use this command!");
    35. }
    36. }
    37. if(cmd.getLabel().equalsIgnoreCase("Chicken")) {
    38. if(sender instanceof Player) {
    39. Player player = (Player) sender;
    40. player.getWorld().spawn(player.getLocation(), Chicken.class);
    41. player.sendMessage(ChatColor.DARK_RED + "HOW DARE YOU?");
    42. player.setHealth(1);
    43. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 400, 3));
    44. } else {
    45. sender.sendMessage(ChatColor.DARK_RED + "Only players can use this command!");
    46. }
    47. }
    48. if(cmd.getLabel().equalsIgnoreCase("Rocket")) {
    49. if(sender instanceof Player) {
    50. Player player = (Player) sender;
    51. player.getWorld().spawn(player.getLocation(), Squid.class);
    52. player.sendMessage(ChatColor.DARK_GREEN + "A wild Rocket has appeared...");
    53. } else {
    54. sender.sendMessage(ChatColor.DARK_RED + "Only players can use this command!");
    55. }
    56. }
    57. if (cmd.getLabel().equalsIgnoreCase("Heal")) {
    58. if (sender instanceof Player) {
    59. }
    60. Player toHeal = getServer().getPlayer(args[0]);
    61. toHeal.setHealth(20);
    62. toHeal.setFoodLevel(20);
    63. toHeal.setLevel(100);
    64. toHeal.setFireTicks(0);
    65. toHeal.sendRawMessage(ChatColor.GREEN + "You've been " + ChatColor.GOLD + "Healed" + ChatColor.GREEN + "!");
    66. } else {
    67. sender.sendMessage(ChatColor.DARK_RED + "Only players can use this command!");
    68. }
    69. if(cmd.getLabel().equalsIgnoreCase("Feed")) {
    70. if(sender instanceof Player) {
    71. Player player = (Player) sender;
    72. player.sendMessage(ChatColor.DARK_GREEN + "You are no longer hungery!");
    73. player.setFoodLevel(20);
    74. } else {
    75. sender.sendMessage(ChatColor.DARK_RED + "Only players can use this command!");
    76. }
    77. }
    78. return true;
    79. }
    80. }
    81.  
    82.  
    83.  



    First of all, when I type any command it always replies "Only players can use this command!" I only want that to happen if it's typed in the console.
    Next, when I type /heal it just says "An internal error has occurred while attempting to preform this command". Please help me!
    ~Cheer
     
  2. You forgot to add return true; it should be like This:
    Code:java
    1. If (//stuff)
    2. {
    3. //dostuff
    4. return true;
    5. } else {
    6. //do other stuff
    7. return true;
    8. }
     
  3. Offline

    Rocoty

    About the first issue:
    Something caught my eye on lines 58 and 59, where you are closing the if-block right after opening it. If you remove the bracket at 59, it should fix your first problem, and maybe even your second problem. But to know more about the second problem we'd need a stacktrace (which usually accompanies the internal error message, it should be in the console)
     
  4. Offline

    cheer60823

    That helps the re-logging but the /heal command is still acting weird

    Here you go Rocoty
    17:28:40 [INFO] cheer60823 issued server command: /heal
    17:28:40 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'heal' in plugin TheCowPlugin v2.5
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
    at org.bukkit.craftbukkit.v1_6_R1.CraftServer.dispatchCommand(CraftServer.java:523)
    at net.minecraft.server.v1_6_R1.PlayerConnection.handleCommand(PlayerConnection.java:983)
    at net.minecraft.server.v1_6_R1.PlayerConnection.chat(PlayerConnection.java:901)
    at net.minecraft.server.v1_6_R1.PlayerConnection.a(PlayerConnection.java:858)
    at net.minecraft.server.v1_6_R1.Packet3Chat.handle(SourceFile:49)
    at net.minecraft.server.v1_6_R1.NetworkManager.b(NetworkManager.java:293)
    at net.minecraft.server.v1_6_R1.PlayerConnection.d(PlayerConnection.java:118)
    at net.minecraft.server.v1_6_R1.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R1.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_6_R1.MinecraftServer.t(MinecraftServer.java:590)
    at net.minecraft.server.v1_6_R1.DedicatedServer.t(DedicatedServer.java:226)
    at net.minecraft.server.v1_6_R1.MinecraftServer.s(MinecraftServer.java:486)
    at net.minecraft.server.v1_6_R1.MinecraftServer.run(MinecraftServer.java:419)
    at net.minecraft.server.v1_6_R1.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at me.Cheer60823.TheCowPlugin.TheCowPlugin.onCommand(TheCowPlugin.java:64)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    SnipsRevival

    cheer60823 you need to check if(args.lengh == 1) first.
     
  6. Offline

    Rocoty

    I assume that happens when you type /heal with no arguments supplied. It happens because you are trying to access args[0], which doesn't exist if you don't pass any arguments. A length check on args should do this trick

    Tip: If the length is 0, you could assignt he toHeal variable to the sender player
     
  7. Offline

    Rprrr

    cheer60823
    Code:
            if (cmd.getLabel().equalsIgnoreCase("heal")) {
     
                if (!(sender instanceof Player)) {
                    //The sender is no player.               
                    sender.sendMessage("You must be a player.");
                    //We can now return a value - this will stop the code here.
                    return true;
                }
               
                //Since only player senders get to this point, we can now safely cast player to the sender.
                Player p = (Player) sender;
               
                p.setHealth(p.getMaxHealth());
                //... (your other healing code should go here).
               
                p.sendMessage("You've been healed!");
               
                //And, then, return a value.
                return true;
               
            }
     
  8. Offline

    cheer60823

    It works when I type /heal ch
    so can it be made so if I type /heal it heals me, if I type /heal ro it heals Rocoty
    or if I type /heal che mel it says "Too many arguments"?
    This is what I have:
    Code:java
    1. if (cmd.getLabel().equalsIgnoreCase("Heal")) {
    2. if (sender instanceof Player) {
    3. Player toHeal = getServer().getPlayer(args[0]);
    4. toHeal.setHealth(20);
    5. toHeal.setFoodLevel(20);
    6. toHeal.setLevel(100);
    7. toHeal.setFireTicks(0);
    8. toHeal.sendRawMessage(ChatColor.GREEN + "You've been " + ChatColor.GOLD + "Healed" + ChatColor.GREEN + "!");
    9. return true;
    10. } else {
    11. sender.sendMessage(ChatColor.DARK_RED + "Only players can use this command!");
    12. return true;
    13. }
    14. }
     
  9. Offline

    Rprrr

    cheer60823
    Use 'if (args.length == x)'-statements in order to achieve that.
     
  10. Offline

    Rocoty

    pseudo-code:
    Code:
    is args' length less than one?
        if yes: toHeal equals the player who executed the command      //(Player) sender
    if no, is args' length greater than one?
        if yes: too many arguments
    if no: toHeal equals a player with name starting with args[0]
    
    EDIT: Fixed a silly mistake
     
  11. Offline

    cheer60823

    Can you please code that for me? Rocoty

    Would it look like this?

    Code:java
    1. if (cmd.getLabel().equalsIgnoreCase("Heal")) {
    2. if (sender instanceof Player) {
    3. if (args.length < 1) {
    4. Player toHeal = getServer().getPlayer(args[0]);
    5. toHeal.setHealth(20);
    6. toHeal.setFoodLevel(20);
    7. toHeal.setLevel(100);
    8. toHeal.setFireTicks(0);
    9. toHeal.sendRawMessage(ChatColor.GREEN + "You've been " + ChatColor.GOLD + "Healed" + ChatColor.GREEN + "!");
    10. } else {
    11. if (args.length > 1) {
    12. sender.sendMessage(ChatColor.DARK_RED + "Too many arguments!");
    13. return true;
    14. } else {
    15. Player toHeal = getServer().getPlayer(args[0]);
    16. toHeal.setHealth(20);
    17. toHeal.setFoodLevel(20);
    18. toHeal.setLevel(100);
    19. toHeal.setFireTicks(0);
    20. return true;
    21. }
    22. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  12. Offline

    SnipsRevival

    cheer60823 you have a check if(args.length < 1), but then you are trying to use args[0] inside of it. That is not going to work.
     
  13. Offline

    cheer60823

    So should it be
    Code:java
    1. if (cmd.getLabel().equalsIgnoreCase("Heal")) {
    2. if (sender instanceof Player) {
    3. if (args.length < 1) {
    4. Player toHeal = getServer().getPlayer();
    5. toHeal.setHealth(20);
    6. toHeal.setFoodLevel(20);
    7. toHeal.setLevel(100);
    8. toHeal.setFireTicks(0);
    9. toHeal.sendRawMessage(ChatColor.GREEN + "You've been " + ChatColor.GOLD + "Healed" + ChatColor.GREEN + "!");
    10. } else {
    11. if (args.length > 1) {
    12. sender.sendMessage(ChatColor.DARK_RED + "Too many arguments!");
    13. return true;
    14. } else {
    15. Player toHeal = getServer().getPlayer(args[0]);
    16. toHeal.setHealth(20);
    17. toHeal.setFoodLevel(20);
    18. toHeal.setLevel(100);
    19. toHeal.setFireTicks(0);
    20. return true;
    21. }
    22. }



    @SnipsRevival
     
  14. Offline

    SnipsRevival

    cheer60823 Almost. Line 4 doesn't make sense to me because the method getPlayer() requires a String parameter. I assume you want to heal the sender, so cast the sender to a Player to give yourself the player you want to heal.
     
Thread Status:
Not open for further replies.

Share This Page