Solved Config Problem

Discussion in 'Plugin Development' started by PieMan456, Dec 10, 2013.

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

    PieMan456

    Hello Everyone,

    I have no idea why I am getting this one part of my code null.
    Error:
    Code:
    2013-12-10 14:38:03 [WARNING] Unexpected exception while parsing console command "ctoken take Pie_Man_456 2"
    org.bukkit.command.CommandException: Unhandled exception executing command 'ctoken' in plugin ThatCookieRankUp v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchServerCommand(CraftServer.java:512)
        at net.minecraft.server.v1_6_R3.DedicatedServer.as(DedicatedServer.java:263)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:228)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at me.pieman.tcru.ThatCookieRankUp.onCommand(ThatCookieRankUp.java:129)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 8 more
    Here is my code where it is not working:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("ctoken")){
    3. if(args.length <= 1){
    4. sender.sendMessage(ChatColor.RED + "Usage: /ctoken [give, take] (name) (amount)");
    5. return true;
    6. }
    7. if(args[0].equalsIgnoreCase("give")){
    8. if(sender.hasPermission("cookietoken.give")){
    9. Player target = Bukkit.getServer().getPlayer(args[1]);
    10. int am = Integer.parseInt(args[2]);
    11. int po = getConfig().getInt("players." + target.getName());
    12. getConfig().set(target.getName(), po + am);
    13. saveConfig();
    14. } else {
    15. sender.sendMessage(ChatColor.RED + "You do not have permission!");
    16. return true;
    17. }
    18. }
    19. if(args[0].equalsIgnoreCase("take")){
    20. if(sender.hasPermission("cookietoken.take")){
    21. Player target = Bukkit.getServer().getPlayer(args[1]);
    22. int am = Integer.parseInt(args[2]);
    23. int po = getConfig().getInt("players." + target.getName());
    24. getConfig().set(target.getName(), po - am);
    25. saveConfig();
    26. } else {
    27. sender.sendMessage(ChatColor.RED + "You do not have permission!");
    28. return true;
    29. }
    30. }
    31. }
    32. return true;
    33. }

    Line 129:
    Code:java
    1. int po = getConfig().getInt("players." + target.getName());
     
  2. Offline

    1Rogue Retired Staff

    Check that "target" isn't null. If you use Bukkit.getPlayer(argument) it will return null if they are not online, and will throw an NPE when you attempt to get the name.
     
  3. Offline

    PieMan456

    Ok thanks it works now except on problem. When I try to add or take away coins it just replaces 'players:' with the name they typed and the amount. Why is this happening?
    Whole class:
    Code:java
    1. package me.pieman.tcru;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class ThatCookieRankUp extends JavaPlugin implements Listener {
    14.  
    15. public void onEnable(){
    16. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    17. getConfig().options().copyDefaults(true);
    18. saveDefaultConfig();
    19.  
    20. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    21. public void run(){
    22. for(Player p : Bukkit.getServer().getOnlinePlayers()){
    23. if(getConfig().getInt("players." + p.getName()) == 1){
    24. String l = getConfig().getString("points.1");
    25. Bukkit.dispatchCommand(p, l);
    26. return;
    27. }
    28. if(getConfig().getInt("players." + p.getName()) == 2){
    29. String l = getConfig().getString("points.2");
    30. Bukkit.dispatchCommand(p, l);
    31. return;
    32. }
    33. if(getConfig().getInt("players." + p.getName()) == 3){
    34. String l = getConfig().getString("points.3");
    35. Bukkit.dispatchCommand(p, l);
    36. return;
    37. }
    38. if(getConfig().getInt("players." + p.getName()) == 4){
    39. String l = getConfig().getString("points.4");
    40. Bukkit.dispatchCommand(p, l);
    41. return;
    42. }
    43. if(getConfig().getInt("players." + p.getName()) == 5){
    44. String l = getConfig().getString("points.5");
    45. Bukkit.dispatchCommand(p, l);
    46. return;
    47. }
    48. if(getConfig().getInt("players." + p.getName()) == 6){
    49. String l = getConfig().getString("points.6");
    50. Bukkit.dispatchCommand(p, l);
    51. return;
    52. }
    53. if(getConfig().getInt("players." + p.getName()) == 7){
    54. String l = getConfig().getString("points.7");
    55. Bukkit.dispatchCommand(p, l);
    56. return;
    57. }
    58. if(getConfig().getInt("players." + p.getName()) == 8){
    59. String l = getConfig().getString("points.8");
    60. Bukkit.dispatchCommand(p, l);
    61. return;
    62. }
    63. if(getConfig().getInt("players." + p.getName()) == 9){
    64. String l = getConfig().getString("points.9");
    65. Bukkit.dispatchCommand(p, l);
    66. return;
    67. }
    68. if(getConfig().getInt("players." + p.getName()) == 10){
    69. String l = getConfig().getString("points.10");
    70. Bukkit.dispatchCommand(p, l);
    71. return;
    72. }
    73. if(getConfig().getInt("players." + p.getName()) == 11){
    74. String l = getConfig().getString("points.11");
    75. Bukkit.dispatchCommand(p, l);
    76. return;
    77. }
    78. if(getConfig().getInt("players." + p.getName()) == 12){
    79. String l = getConfig().getString("points.12");
    80. Bukkit.dispatchCommand(p, l);
    81. return;
    82. }
    83. if(getConfig().getInt("players." + p.getName()) == 13){
    84. String l = getConfig().getString("points.13");
    85. Bukkit.dispatchCommand(p, l);
    86. return;
    87. }
    88. if(getConfig().getInt("players." + p.getName()) == 14){
    89. String l = getConfig().getString("points.14");
    90. Bukkit.dispatchCommand(p, l);
    91. return;
    92. }
    93. if(getConfig().getInt("players." + p.getName()) == 15){
    94. String l = getConfig().getString("points.15");
    95. Bukkit.dispatchCommand(p, l);
    96. return;
    97. }
    98. }
    99. }
    100. }, 20*60, 20*60);
    101. }
    102.  
    103. public void onDisable(){
    104. saveDefaultConfig();
    105. }
    106.  
    107. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    108. if(cmd.getName().equalsIgnoreCase("ctoken")){
    109. if(args.length <= 1){
    110. sender.sendMessage(ChatColor.RED + "Usage: /ctoken [give, take] (name) (amount)");
    111. return true;
    112. }
    113. if(args[0].equalsIgnoreCase("give")){
    114. if(sender.hasPermission("cookietoken.give")){
    115. Player target = Bukkit.getServer().getPlayer(args[1]);
    116. if(target == null){
    117. sender.sendMessage(ChatColor.RED + "That player is not online!");
    118. return true;
    119. }
    120. int am = Integer.parseInt(args[2]);
    121. int po = getConfig().getInt("players." + target.getName());
    122. getConfig().set(target.getName(), po + am);
    123. saveConfig();
    124. } else {
    125. sender.sendMessage(ChatColor.RED + "You do not have permission!");
    126. return true;
    127. }
    128. }
    129. if(args[0].equalsIgnoreCase("take")){
    130. if(sender.hasPermission("cookietoken.take")){
    131. Player target = Bukkit.getServer().getPlayer(args[1]);
    132. if(target == null){
    133. sender.sendMessage(ChatColor.RED + "That player is not online!");
    134. return true;
    135. }
    136. int am = Integer.parseInt(args[2]);
    137. int po = getConfig().getInt("players." + target.getName());
    138. getConfig().set(target.getName(), po - am);
    139. saveConfig();
    140. } else {
    141. sender.sendMessage(ChatColor.RED + "You do not have permission!");
    142. return true;
    143. }
    144. }
    145. }
    146. return true;
    147. }
    148.  
    149. }
    150.  

    Config:
    Code:
    ###  Default Configuration
     
    points:
    ### Set here what command you want to execute when a player has 1 token.
      1:
    ### Set here what command you want to execute when a player has 2 tokens.
      2:
    ### Set here what command you want to execute when a player has 3 tokens.
      3:
    ### Set here what command you want to execute when a player has 4 tokens.
      4:
    ### Set here what command you want to execute when a player has 5 tokens.
      5:
    ### Set here what command you want to execute when a player has 6 tokens.
      6:
    ### Set here what command you want to execute when a player has 7 tokens.
      7:
    ### Set here what command you want to execute when a player has 8 tokens.
      8:
    ### Set here what command you want to execute when a player has 9 tokens.
      9:
    ### Set here what command you want to execute when a player has 10 tokens.
      10:
    ### Set here what command you want to execute when a player has 11 tokens.
      11:
    ### Set here what command you want to execute when a player has 12 tokens.
      12:
    ### Set here what command you want to execute when a player has 13 tokens.
      13:
    ### Set here what command you want to execute when a player has 14 tokens.
      14:
    ### Set here what command you want to execute when a player has 15 tokens.
      15:
     
    ### This is how many points each player has
    players:
    
     
  4. Offline

    AoH_Ruthless

    PieMan456
    Your code isn't checking if the player isn't null specifically. What I mean is that, lines 136-139 (example) will be run whether or not the target is null. You want an else statement nesting that.

    So this:
    Code:java
    1. if(target == null){
    2. sender.sendMessage(ChatColor.RED + "That player is not online!");
    3. return true;
    4. }
    5. int am = Integer.parseInt(args[2]);
    6. int po = getConfig().getInt("players." + target.getName());
    7. getConfig().set(target.getName(), po + am);
    8. saveConfig();


    Will become this:

    Code:java
    1. if(target == null){
    2. sender.sendMessage(ChatColor.RED + "That player is not online!");
    3. return true;
    4. } else {
    5. int am = Integer.parseInt(args[2]);
    6. int po = getConfig().getInt("players." + target.getName());
    7. getConfig().set(target.getName(), po + am);
    8. saveConfig();
    9. return true;
    10. }


    See the difference?
     
  5. Offline

    felixfritz

    Watch your "set" method in line 138. (there's "players." missing infront of the targetname)

    Also, have you considered replacing the code inside the runnable with a for-loop?
     
  6. Offline

    PieMan456

Thread Status:
Not open for further replies.

Share This Page