Solved Error with 3 arg (a username) on a command

Discussion in 'Plugin Development' started by FrozenBattles, Sep 8, 2014.

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

    FrozenBattles

    Hey im trying to make a minecraft mini-game like the gmod game the hidden i did a test to make sure everything was working before i went on but i get an error in minecraft when i type a 3rd arg in my command can anyone help?

    Class 1:
    Code:java
    1. package com.frozenbattles.HiddenHunt;
    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 HiddenHunt extends JavaPlugin{
    10. public String[] players;
    11. int i = 0;
    12. int numOfPlayers = 0;
    13. static Player player;
    14.  
    15. public void onEnable(){
    16. getLogger().info("Hidden Hunt Enabled! :)");
    17. }
    18.  
    19. public void onDisable(){
    20. getLogger().info("Hidden Hunt Disenabled! :(");
    21. }
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    24.  
    25. if(sender instanceof Player){
    26. player = (Player) sender;
    27. if(cmd.getName().equalsIgnoreCase("hiddenhunt")){
    28. if(args.length == 0){
    29. player.sendMessage(ChatColor.GREEN + "Do /hiddenhunt add [playername] to add a player");
    30. player.sendMessage(ChatColor.GREEN + "Do /hiddenhunt remove [playername] to remove a player");
    31. player.sendMessage(ChatColor.GREEN + "Do /hiddenhunt start to start the round");
    32. player.sendMessage(ChatColor.GREEN + "Do /hiddenhunt end to end the round");
    33. player.sendMessage(ChatColor.GREEN + "Or do /hiddenhunt reset to reset the whole game and scores");
    34. } else if(args[0].equalsIgnoreCase("add")){
    35. if(args.length == 1){
    36. player.sendMessage(ChatColor.GREEN + "Do /hiddenhunt add [playername] to add a player");
    37. } else if(args.length == 2) {
    38. if(i < 12){
    39. players[i] = args[1];
    40. i = i + 1;
    41. numOfPlayers = numOfPlayers + 1;
    42. }
    43. }
    44. } else if(args[0].equalsIgnoreCase("remove")){
    45. if(args.length == 1){
    46. player.sendMessage(ChatColor.GREEN + "Do /hiddenhunt remove [playername] to remove a player");
    47. } else {
    48. player.sendMessage(ChatColor.DARK_GREEN + "Removed" + players[i] + "from the list of players.");
    49. players[i] = "";
    50. i--;
    51. numOfPlayers--;
    52. }
    53. } else if(args[0].equalsIgnoreCase("start")){
    54. if(args.length == 1){
    55. player.sendMessage(ChatColor.GREEN + "Do /hiddenhunt start to start the round");
    56. } else {
    57. com.frozenbattles.HiddenHunt.HiddenHuntStart.start(i, player);
    58. }
    59. }
    60. } else {
    61. sender.sendMessage("This command can not be used in the console");
    62. }
    63. }
    64. return false;
    65. }
    66. } //Please ignore that i thing below that is a forum glitch\\
    67. [/i][/i][/i]


    Class 2:
    Code:java
    1. package com.frozenbattles.HiddenHunt;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5.  
    6. public class HiddenHuntStart {
    7. public static void start(int i,Player user) {
    8. if (i < 2){
    9. user.sendMessage(ChatColor.GREEN + "There are not enough players.");
    10. user.sendMessage(ChatColor.GREEN + "Do /hiddenhunt add [playername] to add more players.");
    11. } else if(i < 7){
    12. user.sendMessage(ChatColor.GREEN + "There is one hidden.");
    13. } else if(i < 13){
    14. user.sendMessage(ChatColor.GREEN + "There is two hidden.");
    15. }
    16. }
    17. }
     
  2. Offline

    dchaosknight

    FrozenBattles
    1. Do you have a stack trace?
    2. At the beginning of your onCommand method, please check that the sender is actually a Player before casting. This'll save you some headache later on when you enter the command from the console and get a ClassCastException as a result.
     
  3. Offline

    teej107

    Please post stacktrace if there are errors. I also don't see in your code that you are accessing the 3rd argument. Also why are you storing a single static Player object?
     
  4. Offline

    FrozenBattles

    dchaosknight @teej107is this what you want and updated first post's code to check for a player. oh and i use the static player to transfer between classes.

    Code:
    [17:13:45 INFO]: WadetheStealth issued server command: /hiddenhunt add w
    [17:13:45 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'hiddenhunt' in plugin The_Hidden_Hunt_Plugin v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:957) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:818) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:258) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
    Caused by: java.lang.NullPointerException
        at com.frozenbattles.HiddenHunt.HiddenHunt.onCommand(HiddenHunt.java:38) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        ... 13 more
     
  5. Offline

    fireblast709

    FrozenBattles
    • You never initialized String[] players.
    • Why is there a static Player player field?
     
  6. Offline

    FrozenBattles

    fireblast709 oh yes i forgot to do that but where should i put it in onEnable() or the onCommand()?
    and i use the static player to use in class 2
     
  7. Offline

    teej107

    Is that the only reason for making player static? If so I recommend you read up on how to pass information through parameters as well as taking your time to learn Java first. It will be a lot easier to develop and code when you know the language you are developing in.
     
  8. Offline

    FrozenBattles

    teej107 thank you i accidentally put that there when i was going to do something else but decided to drop it
     
Thread Status:
Not open for further replies.

Share This Page