Solved Why does this code work but on player death, it works but still makes the stacktrace? :/

Discussion in 'Plugin Development' started by MayoDwarf, Nov 17, 2013.

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

    MayoDwarf

    Code:java
    1. public void mAdd(Player player) {
    2. for(Player players : Bukkit.getOnlinePlayers()) {
    3. if(p1p.get(player.getName()).equalsIgnoreCase(players.getName()) && !(p2p.containsKey(player.getName()))) {
    4. String player1 = p1p.get(player.getName());
    5. Double credits = p1.get(player1)/2;
    6. player.sendMessage(ChatColor.YELLOW + "" + player1 + ChatColor.AQUA + " has recieved " + ChatColor.YELLOW + "" + credits + ChatColor.AQUA + " credits for slaying you completely." );
    7. if(player1.equalsIgnoreCase(players.getName())) {
    8. players.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + credits + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p1.get(players.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    9. p1p.remove(player.getName());
    10. p1.remove(players.getName());
    11. p1.remove(player.getName());
    12. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    13. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    14. if(!file.contains("Money")) {
    15. file.addDefault("Money", "0");
    16. }
    17. Double amount = file.getDouble("Money") + credits;
    18. file.set("Money", amount);
    19. try {
    20. file.save(Money);
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }
    24. }
    25. }
    26. else if(p2p.get(player.getName()).equalsIgnoreCase(players.getName()) && !p3p.containsKey(player.getName())) {
    27. String player1 = p1p.get(player.getName());
    28. String player2 = p2p.get(player.getName());
    29. Double creds1 = p1.get(player1)/2;
    30. Double creds2 = p2.get(player2)/2;
    31. Double sum = creds1 + creds2;
    32. player.sendMessage(ChatColor.YELLOW + "" + sum + ChatColor.AQUA + " credits have been split between 2 players for being the cause of your death.");
    33. for(Player players1 : Bukkit.getOnlinePlayers()) {
    34. if(p1p.get(player.getName()).equalsIgnoreCase(players1.getName())) {
    35. players1.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds1 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p1.get(players1.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    36. p1p.remove(player.getName());
    37. p1.remove(players1.getName());
    38. p1.remove(player.getName());
    39. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    40. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    41. if(!file.contains("Money")) {
    42. file.addDefault("Money", "0");
    43. }
    44. Double amount = file.getDouble("Money") + creds1;
    45. file.set("Money", amount);
    46. try {
    47. file.save(Money);
    48. } catch (IOException e) {
    49. e.printStackTrace();
    50. }
    51. }
    52. }
    53. for(Player players2 : Bukkit.getOnlinePlayers()) {
    54. if(p2p.get(player.getName()).equalsIgnoreCase(players2.getName())) {
    55. players2.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds2 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p2.get(players2.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    56. p2p.remove(player.getName());
    57. p2.remove(players2.getName());
    58. p2.remove(player.getName());
    59. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    60. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    61. if(!file.contains("Money")) {
    62. file.addDefault("Money", "0");
    63. }
    64. Double amount = file.getDouble("Money") + creds2;
    65. file.set("Money", amount);
    66. try {
    67. file.save(Money);
    68. } catch (IOException e) {
    69. e.printStackTrace();
    70. }
    71. }
    72. }
    73. }
    74. else if(p3p.get(player.getName()).equalsIgnoreCase(players.getName()) && !p4p.containsKey(player.getName())) {
    75. String player1 = p1p.get(player.getName());
    76. String player2 = p2p.get(player.getName());
    77. String player3 = p3p.get(player.getName());
    78. double creds1 = p1.get(player1)/2;
    79. double creds2 = p2.get(player2)/2;
    80. double creds3 = p3.get(player3)/2;
    81. double sum = creds1 + creds2 + creds3;
    82. player.sendMessage(ChatColor.YELLOW + "" + sum + ChatColor.AQUA + " credits have been split between 3 players for being the cause of your death.");
    83. for(Player players1 : Bukkit.getOnlinePlayers()) {
    84. if(p1p.get(player.getName()).equalsIgnoreCase(players1.getName())) {
    85. players1.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds1 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p1.get(players1.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    86. p1p.remove(player.getName());
    87. p1.remove(players1.getName());
    88. p1.remove(player.getName());
    89. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    90. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    91. if(!file.contains("Money")) {
    92. file.addDefault("Money", "0");
    93. }
    94. Double amount = file.getDouble("Money") + creds1;
    95. file.set("Money", amount);
    96. try {
    97. file.save(Money);
    98. } catch (IOException e) {
    99. e.printStackTrace();
    100. }
    101. }
    102. }
    103. for(Player players2 : Bukkit.getOnlinePlayers()) {
    104. if(p2p.get(player.getName()).equalsIgnoreCase(players2.getName())) {
    105. players2.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds2 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p2.get(players2.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    106. p2p.remove(player.getName());
    107. p2.remove(players2.getName());
    108. p2.remove(player.getName());
    109. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    110. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    111. if(!file.contains("Money")) {
    112. file.addDefault("Money", "0");
    113. }
    114. Double amount = file.getDouble("Money") + creds2;
    115. file.set("Money", amount);
    116. try {
    117. file.save(Money);
    118. } catch (IOException e) {
    119. e.printStackTrace();
    120. }
    121. }
    122. }
    123. for(Player players3 : Bukkit.getOnlinePlayers()) {
    124. if(p3p.get(player.getName()).equalsIgnoreCase(players3.getName())) {
    125. players3.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds3 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p3.get(players3.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    126. p3p.remove(player.getName());
    127. p3.remove(players3.getName());
    128. p3.remove(player.getName());
    129. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    130. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    131. Double amount = file.getDouble("Money") + creds3;
    132. if(!file.contains("Money")) {
    133. file.addDefault("Money", "0");
    134. }
    135. file.set("Money", amount);
    136. try {
    137. file.save(Money);
    138. } catch (IOException e) {
    139. e.printStackTrace();
    140. }
    141. }
    142. }
    143. }
    144. else if(p4p.get(player.getName()).equalsIgnoreCase(players.getName())) {
    145. String player1 = p1p.get(player.getName());
    146. String player2 = p2p.get(player.getName());
    147. String player3 = p3p.get(player.getName());
    148. String player4 = p4p.get(player.getName());
    149. Double creds1 = p1.get(player1)/2;
    150. Double creds2 = p2.get(player2)/2;
    151. Double creds3 = p3.get(player3)/2;
    152. Double creds4 = p4.get(player4)/2;
    153. Double sum = creds1 + creds2 + creds3 + creds4;
    154. player.sendMessage(ChatColor.YELLOW + "" + sum + ChatColor.AQUA + " credits have been split between 4 players for being the cause of your death.");
    155. for(Player players1 : Bukkit.getOnlinePlayers()) {
    156. if(p1p.get(player.getName()).equalsIgnoreCase(players1.getName())) {
    157. players1.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds1 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p1.get(players1.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    158. p1p.remove(player.getName());
    159. p1.remove(players1.getName());
    160. p1.remove(player.getName());
    161. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    162. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    163. if(!file.contains("Money")) {
    164. file.addDefault("Money", "0");
    165. }
    166. Double amount = file.getDouble("Money") + creds1;
    167. file.set("Money", amount);
    168. try {
    169. file.save(Money);
    170. } catch (IOException e) {
    171. e.printStackTrace();
    172. }
    173. }
    174. }
    175. for(Player players2 : Bukkit.getOnlinePlayers()) {
    176. if(p2p.get(player.getName()).equalsIgnoreCase(players2.getName())) {
    177. players2.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds2 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p2.get(players2.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    178. p2p.remove(player.getName());
    179. p2.remove(players2.getName());
    180. p2.remove(player.getName());
    181. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    182. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    183. if(!file.contains("Money")) {
    184. file.addDefault("Money", "0");
    185. }
    186. Double amount = file.getDouble("Money") + creds2;
    187. file.set("Money", amount);
    188. try {
    189. file.save(Money);
    190. } catch (IOException e) {
    191. e.printStackTrace();
    192. }
    193. }
    194. }
    195. for(Player players3 : Bukkit.getOnlinePlayers()) {
    196. if(p3p.get(player.getName()).equalsIgnoreCase(players3.getName())) {
    197. players3.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds3 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p3.get(players3.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    198. p3p.remove(player.getName());
    199. p3.remove(players3.getName());
    200. p3.remove(player.getName());
    201. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    202. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    203. Double amount = file.getDouble("Money") + creds3;
    204. if(!file.contains("Money")) {
    205. file.addDefault("Money", "0");
    206. }
    207. file.set("Money", amount);
    208. try {
    209. file.save(Money);
    210. } catch (IOException e) {
    211. e.printStackTrace();
    212. }
    213. }
    214. }
    215. for(Player players4 : Bukkit.getOnlinePlayers()) {
    216. if(p4p.get(player.getName()).equalsIgnoreCase(players4.getName())) {
    217. players4.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds4 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p4.get(players4.getName())/2 + ChatColor.AQUA + " hearts of damage to " + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
    218. p4p.remove(player.getName());
    219. p4.remove(players4.getName());
    220. p4.remove(player.getName());
    221. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");
    222. FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
    223. Double amount = file.getDouble("Money") + creds3;
    224. if(!file.contains("Money")) {
    225. file.addDefault("Money", "0");
    226. }
    227. file.set("Money", amount);
    228. try {
    229. file.save(Money);
    230. } catch (IOException e) {
    231. e.printStackTrace();
    232. }
    233. }
    234. }
    235. }
    236. }
    237. }
     
  2. Offline

    RealDope

    1. What is the code doing
    2. What is the stacktrace
     
  3. Offline

    MayoDwarf

    RealDope It is giving the last 4 people to hit the player, credits. This is the stack trace:
    Code:java
    1. 17:05:30 [SEVERE] Could not pass event PlayerDeathEvent to jCredits v1.0
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    4. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    5. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    6. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    7. at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:344)
    8. at net.minecraft.server.v1_6_R3.EntityPlayer.die(EntityPlayer.java:312)
    9. at org.bukkit.craftbukkit.v1_6_R3.entity.CraftLivingEntity.setHealth(CraftLivingEntity.java:79)
    10. at org.bukkit.command.defaults.KillCommand.execute(KillCommand.java:33)
    11. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    12. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
    13. at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:968)
    14. at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:886)
    15. at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:837)
    16. at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    17. at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    18. at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    19. at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    20. at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    21. at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    22. at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    23. at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    24. at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    25. at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    26. Caused by: java.lang.NullPointerException
    27. at jCredits.Main.mAdd(Main.java:129)
    28. at jCredits.Damagelogger.onPlayerDeath(Damagelogger.java:67)
    29. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    30. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    31. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    32. at java.lang.reflect.Method.invoke(Method.java:597)
    33. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    34. ... 22 more


    RealDope Hello?

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

    Briggybros

    MayoDwarf Can you post the full class or point out which line in the mAdd method is line 129?
     
  5. Offline

    MayoDwarf

    Line 129 is
    Code:java
    1. if(p1p.get(player.getName()).equalsIgnoreCase(players.getName()) && !(p2p.containsKey(player.getName()))) {
    2.  
     
  6. Offline

    RealDope

    What is line 129
    Derp you just posted it

    Print all the variables on that line to console, see which is null.
     
  7. Offline

    thepaperboy99

    The error is coming from line 129, and after looking at it, you have:

    Code:java
    1. File Money = new File("plugins"+File.separator+"jCredits"+File.separator+"players"+File.separator+File.separator+"economy"+File.separator+players.getName().toLowerCase()+".yml");


    If you look close enough, you repeated the File.separator twice.
     
  8. Offline

    Briggybros

    thepaperboy99 But that's not the actual line 129, he only posted the method, not the class
     
  9. Offline

    MayoDwarf

    That's not line 129 though. EDIT: thepaperboy99
     
  10. Offline

    thepaperboy99


    Oh, then can you post the whole class? I thought it was that one line lol.
     
  11. Offline

    MayoDwarf

    This is line 129:
    Code:java
    1. if(p1p.get(player.getName()).equalsIgnoreCase(players.getName()) && !p2p.containsKey(player.getName())) {


    thepaperboy99

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

    thepaperboy99



    Maybe check if p1p.get(..) is trying to get something that is null?

    So maybe like if(p1p.get(player.getName)) != null) ?
     
  13. Offline

    MayoDwarf

    It can't be because the code runs... :eek:
    EDIT: thepaperboy99
     
  14. Offline

    thepaperboy99


    ??

    Can't you do

    if(p1p.get(player.getName()) == null){
    // Its null here
    return;
    }
     
  15. Offline

    MayoDwarf

    The thing is, why does the code run but it still says Null? If it's running it's obviously not null right? thepaperboy99
     
  16. Offline

    the_merciless

    Then the method must be called twice?
     
  17. Offline

    thepaperboy99


    No, it can still run even when its null, that's why you have to check if its not to avoid errors
     
  18. Offline

    the_merciless

    No it will stop

    MayoDwarf show us your method which calls this method
     
  19. Offline

    MayoDwarf

    the_merciless thepaperboy99
    When changed to:
    Code:java
    1. if(p1p.get(player.getName()).equalsIgnoreCase(players.getName()) && p2p.get(player.getName()) !=null) {
    It goes on to the else if and that is null because p2p does not contain anyone... -.- Method that calls this method:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent evt) {
    3. Player player = evt.getEntity();
    4. mAdd(player);
    5. }
    6. }
     
  20. Offline

    thepaperboy99


    Oh sorry, I read that wrong lol.
     
  21. Offline

    MayoDwarf

    You also missed this:
    thepaperboy99
     
  22. Offline

    the_merciless

    p1p.get(player.getName()) is giving you the error, add a check

    Code:
    for (Player players : Bukkit.getOnlinePlayers()) {
                if (p1p.contains(player.getName()){
                    if (p1p.get(player.getName()).equalsIgnoreCase(players.getName()) && !(p2p.containsKey(player.getName()))) {
     
  23. Offline

    MayoDwarf

    You are a lifesaver! Thank you! I just had to check if it did contain it! It wasn't ever turning null, it just wasn't efficient. Thanks bro :)
     
  24. Offline

    the_merciless

    it was returning null as your looping through all players. You should add a break in there too to stop the loop from continuing after you finished with it.

    Code:
        public void mAdd(Player player) {
           
            for (Player players : Bukkit.getOnlinePlayers()) {
               
                if (p1p.contains(player.getName())){
                   
                    if (p1p.get(player.getName()).equalsIgnoreCase(players.getName()) && !(p2p.containsKey(player.getName()))) {
                        String player1 = p1p.get(player.getName());
                        Double credits = p1.get(player1) / 2;
                        player.sendMessage(ChatColor.YELLOW + "" + player1 + ChatColor.AQUA + " has recieved " + ChatColor.YELLOW + "" + credits + ChatColor.AQUA + " credits for slaying you completely.");
                        if (player1.equalsIgnoreCase(players.getName())) {
                            players.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + credits + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p1.get(players.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                    + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                            p1p.remove(player.getName());
                            p1.remove(players.getName());
                            p1.remove(player.getName());
                            File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                            FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                            if (!file.contains("Money")) {
                                file.addDefault("Money", "0");
                            }
                            Double amount = file.getDouble("Money") + credits;
                            file.set("Money", amount);
                            try {
                                file.save(Money);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }break;
                   
                }else if (p2p.contains(player.getName())){
                   
                    if (p2p.get(player.getName()).equalsIgnoreCase(players.getName()) && !p3p.containsKey(player.getName())) {
                        String player1 = p1p.get(player.getName());
                        String player2 = p2p.get(player.getName());
                        Double creds1 = p1.get(player1) / 2;
                        Double creds2 = p2.get(player2) / 2;
                        Double sum = creds1 + creds2;
                        player.sendMessage(ChatColor.YELLOW + "" + sum + ChatColor.AQUA + " credits have been split between 2 players for being the cause of your death.");
                        for (Player players1 : Bukkit.getOnlinePlayers()) {
                            if (p1p.get(player.getName()).equalsIgnoreCase(players1.getName())) {
                                players1.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds1 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p1.get(players1.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p1p.remove(player.getName());
                                p1.remove(players1.getName());
                                p1.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                Double amount = file.getDouble("Money") + creds1;
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        for (Player players2 : Bukkit.getOnlinePlayers()) {
                            if (p2p.get(player.getName()).equalsIgnoreCase(players2.getName())) {
                                players2.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds2 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p2.get(players2.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p2p.remove(player.getName());
                                p2.remove(players2.getName());
                                p2.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                Double amount = file.getDouble("Money") + creds2;
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }break;               
                   
                }else if (p3p.contains(player.getName())){
                   
                    if (p3p.get(player.getName()).equalsIgnoreCase(players.getName()) && !p4p.containsKey(player.getName())) {
                        String player1 = p1p.get(player.getName());
                        String player2 = p2p.get(player.getName());
                        String player3 = p3p.get(player.getName());
                        double creds1 = p1.get(player1) / 2;
                        double creds2 = p2.get(player2) / 2;
                        double creds3 = p3.get(player3) / 2;
                        double sum = creds1 + creds2 + creds3;
                        player.sendMessage(ChatColor.YELLOW + "" + sum + ChatColor.AQUA + " credits have been split between 3 players for being the cause of your death.");
                        for (Player players1 : Bukkit.getOnlinePlayers()) {
                            if (p1p.get(player.getName()).equalsIgnoreCase(players1.getName())) {
                                players1.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds1 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p1.get(players1.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p1p.remove(player.getName());
                                p1.remove(players1.getName());
                                p1.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                Double amount = file.getDouble("Money") + creds1;
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        for (Player players2 : Bukkit.getOnlinePlayers()) {
                            if (p2p.get(player.getName()).equalsIgnoreCase(players2.getName())) {
                                players2.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds2 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p2.get(players2.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p2p.remove(player.getName());
                                p2.remove(players2.getName());
                                p2.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                Double amount = file.getDouble("Money") + creds2;
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        for (Player players3 : Bukkit.getOnlinePlayers()) {
                            if (p3p.get(player.getName()).equalsIgnoreCase(players3.getName())) {
                                players3.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds3 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p3.get(players3.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p3p.remove(player.getName());
                                p3.remove(players3.getName());
                                p3.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                Double amount = file.getDouble("Money") + creds3;
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }break;
                   
                } else if (p4p.contains(player.getName())){
                   
                    if (p4p.get(player.getName()).equalsIgnoreCase(players.getName())) {
                        String player1 = p1p.get(player.getName());
                        String player2 = p2p.get(player.getName());
                        String player3 = p3p.get(player.getName());
                        String player4 = p4p.get(player.getName());
                        Double creds1 = p1.get(player1) / 2;
                        Double creds2 = p2.get(player2) / 2;
                        Double creds3 = p3.get(player3) / 2;
                        Double creds4 = p4.get(player4) / 2;
                        Double sum = creds1 + creds2 + creds3 + creds4;
                        player.sendMessage(ChatColor.YELLOW + "" + sum + ChatColor.AQUA + " credits have been split between 4 players for being the cause of your death.");
                        for (Player players1 : Bukkit.getOnlinePlayers()) {
                            if (p1p.get(player.getName()).equalsIgnoreCase(players1.getName())) {
                                players1.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds1 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p1.get(players1.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p1p.remove(player.getName());
                                p1.remove(players1.getName());
                                p1.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                Double amount = file.getDouble("Money") + creds1;
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        for (Player players2 : Bukkit.getOnlinePlayers()) {
                            if (p2p.get(player.getName()).equalsIgnoreCase(players2.getName())) {
                                players2.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds2 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p2.get(players2.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p2p.remove(player.getName());
                                p2.remove(players2.getName());
                                p2.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                Double amount = file.getDouble("Money") + creds2;
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        for (Player players3 : Bukkit.getOnlinePlayers()) {
                            if (p3p.get(player.getName()).equalsIgnoreCase(players3.getName())) {
                                players3.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds3 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p3.get(players3.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p3p.remove(player.getName());
                                p3.remove(players3.getName());
                                p3.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                Double amount = file.getDouble("Money") + creds3;
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        for (Player players4 : Bukkit.getOnlinePlayers()) {
                            if (p4p.get(player.getName()).equalsIgnoreCase(players4.getName())) {
                                players4.sendMessage(ChatColor.AQUA + "You have recieved " + ChatColor.YELLOW + "" + creds4 + ChatColor.AQUA + " for doing " + ChatColor.YELLOW + "" + p4.get(players4.getName()) / 2 + ChatColor.AQUA + " hearts of damage to "
                                        + ChatColor.YELLOW + "" + player.getName() + ChatColor.AQUA + ".");
                                p4p.remove(player.getName());
                                p4.remove(players4.getName());
                                p4.remove(player.getName());
                                File Money = new File("plugins" + File.separator + "jCredits" + File.separator + "players" + File.separator + File.separator + "economy" + File.separator + players.getName().toLowerCase() + ".yml");
                                FileConfiguration file = YamlConfiguration.loadConfiguration(Money);
                                Double amount = file.getDouble("Money") + creds3;
                                if (!file.contains("Money")) {
                                    file.addDefault("Money", "0");
                                }
                                file.set("Money", amount);
                                try {
                                    file.save(Money);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }               
                }       
            }
        }
    You should just be able to copy and paste that ^^

    Someguyfromcrowd != null

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 4, 2018

  25. It's perfectly acceptable for methods to return null (unless they're void methods). For example, getServer().getPlayer("abc") will return null if there's no player named "abc" on the server.

    As others have said, it's important to do null-checking before using anything. You must be careful, though- trying to invoke any method on a null object immediately throws a null pointer exception. For example:

    Player player = getServer().getPlayer("abc");
    if (!player.equals(null))
    [...]

    This is bad! Equals() is a method defined for anything inheriting from Object (everything in Java). However, null can't do anything of the sort. Instead, you have to use this:

    Player player = getServer().getPlayer("abc");
    if (!player == null)
    [...]

    The == operator is safe to use in this case.


    Buh, that's right. I thought something looked wonky about that expression.

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

Share This Page