Npe on setspawn of arena

Discussion in 'Plugin Development' started by xXMaTTHDXx, Feb 19, 2014.

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

    xXMaTTHDXx

    Well I am making a sg plugin kinda like the nexus, I was testing everything, it is hooked into world edit, anyway I created my arena and then when I tried to setspawns I got a npe:

    Code:
    java.lang.NullPointerException
    [20:43:00 WARN]:        at me.xXMaTTHDXx.SSG.Game.isBlockInArena(Game.java:602)
    [20:43:00 WARN]:        at me.xXMaTTHDXx.SSG.GameManager.getBlockGameId(GameManager.java:77)
    [20:43:00 WARN]:        at me.xXMaTTHDXx.SSG.cmds.SetSpawn.onCommand(SetSpawn.java:38)
    [20:43:00 WARN]:        at me.xXMaTTHDXx.SSG.CommandHandler.onCommand(CommandHandler.java:153)
    [20:43:00 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [20:43:00 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175)
    [20:43:00 WARN]:        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:952)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457)
    [20:43:00 WARN]:        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617)
    Here are the classes:

    Code:java
    1. package me.xXMaTTHDXx.SSG.cmds;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import me.xXMaTTHDXx.SSG.Game;
    6. import me.xXMaTTHDXx.SSG.GameManager;
    7. import me.xXMaTTHDXx.SSG.MessageManager;
    8. import me.xXMaTTHDXx.SSG.MessageManager.MessageType;
    9. import me.xXMaTTHDXx.SSG.SettingsManager;
    10.  
    11. import org.bukkit.Location;
    12. import org.bukkit.entity.Player;
    13.  
    14. public class SetSpawn implements SubCommand{
    15.  
    16.  
    17. HashMap<Integer, Integer>next = new HashMap<Integer,Integer>();
    18.  
    19. public SetSpawn() {
    20.  
    21. }
    22.  
    23. public void loadNextSpawn(){
    24. for(Game g: GameManager.getInstance().getGames().toArray(new Game[0])){ //Avoid Concurrency problems
    25. next.put(g.getID(), SettingsManager.getInstance().getSpawnCount(g.getID())+1);
    26. }
    27. }
    28.  
    29. public boolean onCommand(Player player, String[] args) {
    30. if (!player.hasPermission(permission()) && !player.isOp()) {
    31. MessageManager.getInstance().msg(player, MessageType.BAD, "You do not have permission to you this command!");
    32. return true;
    33. }
    34.  
    35. loadNextSpawn();
    36. Location l = player.getLocation();
    37. int game = GameManager.getInstance().getBlockGameId(l);
    38. System.out.println(game+" "+next.size());
    39. if(game == -1){
    40. MessageManager.getInstance().msg(player, MessageType.BAD, "Not in an arena!");
    41.  
    42. if(GameManager.getInstance().getGame(game) == null){
    43. System.out.println("DEBUG: Game is null!!!!");
    44. }
    45.  
    46. return true;
    47. }
    48. int i = 0;
    49. if(args[0].equalsIgnoreCase("next")){
    50. i = next.get(game);
    51. next.put(game, next.get(game)+1);
    52. }
    53. else{
    54. try{
    55. i = Integer.parseInt(args[0]);
    56. if(i>next.get(game)+1 || i<1){
    57. MessageManager.getInstance().msg(player, MessageType.BAD, "Not between" + "num-" + next.get(game));
    58. return true;
    59. }
    60. if(i == next.get(game)){
    61. next.put(game, next.get(game)+1);
    62. }
    63. }catch(Exception e){
    64. MessageManager.getInstance().msg(player, MessageType.BAD, "Incorrect input!");
    65. return false;
    66. }
    67. }
    68. if(i == -1){
    69. MessageManager.getInstance().msg(player, MessageType.BAD, "Not inside region!");
    70. return true;
    71. }
    72. SettingsManager.getInstance().setSpawn(l, i, game);
    73. MessageManager.getInstance().msg(player, MessageType.INFO, "info: " + "num-" + i + ", arena-" + game);
    74. return true;
    75. }
    76.  
    77. @Override
    78. public String help(Player p) {
    79. return "/ssg setspawn next - " + "Sets a spawn for the arena you are located in";
    80. }
    81.  
    82. @Override
    83. public String permission() {
    84. return "ssg.admin.setarenaspawns";
    85. }
    86. }
    87.  


    GameManager:

    Code:java
    1. package me.xXMaTTHDXx.SSG;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.HashSet;
    6.  
    7. import me.xXMaTTHDXx.SSG.Game.GameState;
    8. import me.xXMaTTHDXx.SSG.MessageManager.MessageType;
    9. import me.xXMaTTHDXx.SSG.stats.StatsManager;
    10.  
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.Location;
    13. import org.bukkit.block.Block;
    14. import org.bukkit.configuration.file.FileConfiguration;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.plugin.Plugin;
    17.  
    18. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    19. import com.sk89q.worldedit.bukkit.selections.Selection;
    20.  
    21. public class GameManager {
    22.  
    23. private static GameManager instance = new GameManager();
    24. private ArrayList<Game> games = new ArrayList<Game>();
    25. public static HashMap<Integer, HashSet <Block>> openedChest = new HashMap<Integer, HashSet <Block>>();
    26. private SSGMain p;
    27.  
    28.  
    29. private GameManager(){
    30.  
    31. }
    32.  
    33. public static GameManager getInstance(){
    34. return instance;
    35. }
    36.  
    37. public void setup(SSGMain plugin){
    38. p = plugin;
    39. loadGames();
    40. for (Game g: getGames()) {
    41. openedChest.put(g.getID(), new HashSet < Block > ());
    42. }
    43. }
    44. public Plugin getPlugin(){
    45. return p;
    46. }
    47.  
    48. public void reloadGames(){
    49. loadGames();
    50. }
    51.  
    52. public void loadGames(){
    53. FileConfiguration fc = SettingsManager.getInstance().getArenas();
    54.  
    55. games.clear();
    56. int no = fc.getInt("ssg.arenano", 0);
    57. int loaded = 0;
    58. int a = 1;
    59.  
    60. while(loaded < no){
    61. if(fc.isSet("ssg.arenas." + a + ".x1")){
    62. if(fc.getBoolean("ssg.arenas." + a + "enabled")){
    63. loaded++;
    64. games.add(new Game(a));
    65. //StatsManager here.
    66.  
    67. }
    68. }
    69. a++;
    70. }
    71. LobbyManager.getInstance().clearAllSigns();
    72. }
    73.  
    74. public int getBlockGameId(Location loc){
    75. for(Game g : games){
    76. if(g.isBlockInArena(loc)){
    77. return g.getID();
    78. }
    79. }
    80. return -1;
    81.  
    82. }
    83.  
    84. public WorldEditPlugin getWorldEdit() {
    85. return p.getWorldEdit();
    86. }
    87.  
    88. public int getPlayerGameID(Player p){
    89. for(Game g : games){
    90. if(g.isPlayerActive(p)){
    91. return g.getID();
    92. }
    93. }
    94. return -1;
    95. }
    96.  
    97. public boolean isSpectator(Player player) {
    98. for (Game g: games) {
    99. if (g.isSpectating(player)) {
    100. return true;
    101. }
    102. }
    103. return false;
    104. }
    105.  
    106. public void removeFromOtherQueues(Player p, int id) {
    107. for (Game g: getGames()) {
    108. if (g.isInQueue(p) && g.getID() != id) {
    109. g.removeFromQueue(p);
    110. MessageManager.getInstance().msg(p, MessageType.INFO, "Removed from all other queues!");
    111. }
    112. }
    113. }
    114.  
    115. public void disableGame(int id) {
    116. getGame(id).disable();
    117. }
    118.  
    119. public void enableGame(int id) {
    120. getGame(id).enableArena();
    121. }
    122.  
    123. public ArrayList < Game > getGames() {
    124. return games;
    125. }
    126.  
    127. public Game getGame(int a) {
    128. //int t = gamemap.get(a);
    129. for (Game g: games) {
    130. if (g.getID() == a) {
    131. return g;
    132. }
    133. }
    134. return null;
    135. }
    136.  
    137. public boolean isPlayerActive(Player player) {
    138. for (Game g: games) {
    139. if (g.isPlayerActive(player)) {
    140. return true;
    141. }
    142. }
    143. return false;
    144. }
    145.  
    146. public boolean isPlayerInactive(Player player) {
    147. for (Game g: games) {
    148. if (g.isPlayerActive(player)) {
    149. return true;
    150. }
    151. }
    152. return false;
    153. }
    154.  
    155. public int getGameCount(){
    156. return games.size();
    157. }
    158.  
    159. public int getPlayerSpectateId(Player p) {
    160. for (Game g: games) {
    161. if (g.isSpectating(p)) {
    162. return g.getID();
    163. }
    164. }
    165. return -1;
    166. }
    167. public void removePlayer(Player p, boolean b) {
    168. getGame(getPlayerGameID(p)).removePlayer(p, b);
    169. }
    170.  
    171. public void removeSpectator(Player p) {
    172. getGame(getPlayerSpectateId(p)).removeSpectator(p);
    173. }
    174. public GameState getGameMode(int a) {
    175. for (Game g: games) {
    176. if (g.getID() == a) {
    177. return g.getGameState();
    178. }
    179. }
    180. return null;
    181. }
    182.  
    183. public void startGame(int a) {
    184. getGame(a).countdown(10);
    185. }
    186. public void addPlayer(Player p, int g) {
    187. Game game = getGame(g);
    188. if (game == null) {
    189. MessageManager.getInstance().msg(p, MessageType.BAD, "No arena exists with that ID!");
    190. return;
    191. }
    192. getGame(g).addPlayer(p);
    193. }
    194.  
    195. public void autoAddPlayer(Player pl) {
    196. ArrayList < Game > qg = new ArrayList < Game > (5);
    197. for (Game g: games) {
    198. if (g.getGameState() == Game.GameState.WAITING) qg.add(g);
    199. }
    200. //TODO: fancy auto balance algorithm
    201. if (qg.size() == 0) {
    202. pl.sendMessage(ChatColor.RED + "No games to join");
    203. MessageManager.getInstance().msg(pl, MessageType.BAD, "No games to join!");
    204. return;
    205. }
    206. qg.get(0).addPlayer(pl);
    207. }
    208.  
    209. public void createArenaFromSelection(Player pl) {
    210. FileConfiguration c = SettingsManager.getInstance().getArenas();
    211. //SettingsManager s = SettingsManager.getInstance();
    212.  
    213. WorldEditPlugin we = p.getWorldEdit();
    214. Selection sel = we.getSelection(pl);
    215. if (sel == null) {
    216. MessageManager.getInstance().msg(pl, MessageType.BAD, "You must make a WorldEdit Selection first!");
    217. return;
    218. }
    219. Location max = sel.getMaximumPoint();
    220. Location min = sel.getMinimumPoint();
    221.  
    222. /* if(max.getWorld()!=SettingsManager.getGameWorld() || min.getWorld()!=SettingsManager.getGameWorld()){
    223.   pl.sendMessage(ChatColor.RED+"Wrong World!");
    224.   return;
    225.   }*/
    226.  
    227. int no = c.getInt("ssg.arenano") + 1;
    228. c.set("ssg.arenano", no);
    229. if (games.size() == 0) {
    230. no = 1;
    231. } else no = games.get(games.size() - 1).getID() + 1;
    232. SettingsManager.getInstance().getSpawns().set(("spawns." + no), null);
    233. c.set("ssg.arenas." + no + ".world", max.getWorld().getName());
    234. c.set("ssg.arenas." + no + ".x1", max.getBlockX());
    235. c.set("ssg.arenas." + no + ".y1", max.getBlockY());
    236. c.set("ssg.arenas." + no + ".z1", max.getBlockZ());
    237. c.set("ssg.arenas." + no + ".x2", min.getBlockX());
    238. c.set("ssg.arenas." + no + ".y2", min.getBlockY());
    239. c.set("ssg.arenas." + no + ".z2", min.getBlockZ());
    240. c.set("ssg.arenas." + no + ".enabled", true);
    241.  
    242. SettingsManager.getInstance().saveArenas();
    243. hotAddArena(no);
    244. pl.sendMessage(ChatColor.GREEN + "Arena ID " + no + " Succesfully added");
    245. }
    246.  
    247. private void hotAddArena(int no) {
    248. Game game = new Game(no);
    249. games.add(game);
    250. StatsManager.getInstance().addArena(no);
    251. }
    252.  
    253. public void hotRemoveArena(int no) {
    254. for (Game g: games.toArray(new Game[0])) {
    255. if (g.getID() == no) {
    256. games.remove(getGame(no));
    257. }
    258. }
    259. }
    260.  
    261. public void gameEndCallBack(int id) {
    262. getGame(id).setRBStatus("clearing chest");
    263. openedChest.put(id, new HashSet < Block > ());
    264. }
    265.  
    266. public String getStringList(int gid){
    267. Game g = getGame(gid);
    268. StringBuilder sb = new StringBuilder();
    269. Player[][]players = g.getPlayers();
    270.  
    271. sb.append(ChatColor.GREEN+"<---------------------[ Alive: "+players[0].length+" ]--------------------->\n"+ChatColor.GREEN+" ");
    272. for(Player p: players[0]){
    273. sb.append(p.getName()+",");
    274. }
    275. sb.append("\n\n");
    276. sb.append(ChatColor.RED+ "<---------------------[ Dead: "+players[1].length+" ]---------------------->\n"+ChatColor.GREEN+" ");
    277. for(Player p: players[1]){
    278. sb.append(p.getName()+",");
    279. }
    280. sb.append("\n\n");
    281.  
    282. return sb.toString();
    283. }
    284. }
    285.  
     
  2. Offline

    Gater12

  3. Offline

    xXMaTTHDXx

    Gater12

    public boolean isBlockInArena(Location l){
    return arena.containsBlock(l);
     
Thread Status:
Not open for further replies.

Share This Page