Bukkit Coding (SignUpdater) [HELP]

Discussion in 'Plugin Development' started by benzimmer123, Dec 17, 2013.

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

    benzimmer123

    Can someone please tell me whats wrong with this, or give me another method to update the sign. I have been at it for weeks but can't find a way to solve it! Errors are on line 46, 48, and 104.
    Thanks in advance:)

    CODE:
    Code:java
    1. package mindless.pvp.craft.Freeze;
    2.  
    3. import java.io.File;
    4. import java.util.ArrayList;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.World;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.block.Sign;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.Action;
    17. import org.bukkit.event.block.SignChangeEvent;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class Main extends JavaPlugin implements Listener {
    22.  
    23. File configFile = new File(this.getDataFolder() + "/config.yml");
    24. ArrayList<Player> minigames = new ArrayList<Player>();
    25. private Location bLocation;
    26.  
    27. public void onEnable() {
    28. getConfig().set("Players." + ".InEvent", 0);
    29. this.saveConfig();
    30. if (!configFile.exists()) {
    31. this.getLogger().info(
    32. "No configuration file found. Attempting to create one.");
    33. this.saveDefaultConfig();
    34. }
    35. getServer().getPluginManager().registerEvents(this, this);
    36. }
    37.  
    38. @EventHandler
    39. public void blockPlaced(SignChangeEvent event) {
    40. String[] a = event.getLines();
    41. if (a[1].equals(ChatColor.GREEN + "[Join]")) {
    42. bLocation = event.getBlock().getLocation();
    43. }
    44. }
    45.  
    46. @SuppressWarnings("deprecation")
    47. public void signUpdater(Location bLocation) {
    48. World w = bLocation.getWorld();
    49. Block b = w.getBlockAt(bLocation);
    50. if (b.getTypeId() == Material.SIGN_POST.getId()
    51. || b.getTypeId() == Material.WALL_SIGN.getId()) {
    52. Sign sign = (Sign) b;
    53. int players = getConfig().getInt("Players." + ".InEvent");
    54. sign.setLine(2, players + "/24");
    55. sign.update();
    56. }
    57. }
    58.  
    59. @EventHandler
    60. public void signChange(SignChangeEvent e) {
    61. int players = getConfig().getInt("Players." + ".InEvent");
    62. if (e.getLine(0).equalsIgnoreCase("[event]")) {
    63. if (players < 21) {
    64. e.setLine(0, ChatColor.GREEN + "[Join]");
    65. e.setLine(1, "1-0");
    66. e.setLine(2, players + "/24");
    67. e.setLine(3, "Events");
    68. } else if (players == 21 || players > 21 && players < 24) {
    69. e.setLine(0, ChatColor.DARK_PURPLE + "[Donator]");
    70. e.setLine(1, "1-0");
    71. e.setLine(2, players + "/24");
    72. e.setLine(3, "Events");
    73. } else if (players == 24) {
    74. e.setLine(0, ChatColor.RED + "[Full]");
    75. e.setLine(1, "1-0");
    76. e.setLine(2, players + "/24");
    77. e.setLine(3, "Events");
    78. }
    79. }
    80. }
    81.  
    82. @EventHandler
    83. public void onSign(PlayerInteractEvent e) {
    84. Player p = e.getPlayer();
    85. int players = getConfig().getInt("Players." + ".InEvent");
    86. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    87. if (e.getClickedBlock().getState() instanceof Sign) {
    88. final Sign s = (Sign) e.getClickedBlock().getState();
    89. if (s.getLine(0).equalsIgnoreCase("§a[Join]")) {
    90. if (minigames.contains(p)) {
    91. p.sendMessage(ChatColor.RED
    92. + "You are already in an event.");
    93. } else {
    94. minigames.add(p);
    95. players++;
    96. getConfig().set("Players." + ".InEvent", players);
    97. this.saveConfig();
    98. for (Player online : Bukkit.getOnlinePlayers()) {
    99. if (minigames.contains(online)) {
    100. online.sendMessage(ChatColor.GREEN
    101. + p.getName()
    102. + " just joined events, there is now "
    103. + players + "/24.");
    104. signUpdater(bLocation);
    105. } else {
    106. return;
    107. }
    108. }
    109. }
    110. } else if (s.getLine(0).equalsIgnoreCase("§5[Donator]")) {
    111. if (p.hasPermission("CommandCentral.joindonator")) {
    112. if (minigames.contains(p)) {
    113. p.sendMessage(ChatColor.RED
    114. + "You are already in an event.");
    115. } else {
    116. minigames.add(p);
    117. players++;
    118. getConfig().set("Players." + ".InEvent", players);
    119. this.saveConfig();
    120. signUpdater(bLocation);
    121. for (Player online : Bukkit.getOnlinePlayers()) {
    122. if (minigames.contains(online)) {
    123. online.sendMessage(ChatColor.GREEN
    124. + p.getName()
    125. + " just joined events, there is now "
    126. + players + "/24.");
    127. signUpdater(bLocation);
    128. } else {
    129. return;
    130. }
    131. }
    132. }
    133. } else {
    134. p.sendMessage(ChatColor.RED
    135. + "You don't have permission.");
    136. }
    137. } else if (s.getLine(0).equalsIgnoreCase("§c[Full]")) {
    138. p.sendMessage(ChatColor.RED + "This game is full.");
    139. }
    140. } else {
    141. return;
    142. }
    143. } else {
    144. return;
    145. }
    146. }
    147. }


    No one?:(

    BUMP

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

    Mitsugaru

    Simply put, bLocation is not initialized / set when the PlayerInteractEvent is fired and runs through your listener's method onSign. Sort of a race condition since its possible for the PlayerInteractEvent to fire before the SignChangeEvent.

    In other words, line 104, 120, or 127 may have the chance of calling your signUpdater with a null parameter. And since you don't check if the passed parameter is null in signUpdater, it throws an NPE.

    This is the best I can do with just a quick glance. I could be completely wrong. Also, next time, an actual stack trace would be more helpful than just the line numbers associated. Especially since 46 points to the annotation and has no bearing on the actual issue.
     
Thread Status:
Not open for further replies.

Share This Page