Get several instance of an arraylist

Discussion in 'Plugin Development' started by Miffy, Jul 30, 2013.

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

    soulofw0lf

    i'm still looking it over, but before anything else don't store players.. store their names
    List<Player> nlobbyplist = new ArrayList<Player>();
    should be
    List<String> nlobbyplist = new ArrayList<String>();
    and you store by player.getName();

    ok you don't have a check for the first contains in both spots
    if(gamepList.get(pstring).contains(player.getName())){ // NPE HERE
    should be
    if (gameList.containsKey(pstring)){
    if(gamepList.get(pstring).contains(player.getName())){

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

    Miffy

    soulofw0lf
    Thanks you dude, this is solved.
    I know the issue about storing player, like I have explain in my first post, i will fix that later.

    soulofw0lf

    You certainly consider me as a java nooby, but still got an issue.

    I have made some test and I've find this bug.
    When a player join a game1 and a player2 join a game2 :
    Sign of game1 display 2 player (Should be 1)
    Sign of game2 display 1 player

    Can figured out what's wrong in my class..
    LobbyManager still the same with a few change

    Code:java
    1. public class LobbyManager implements Listener{
    2.  
    3.  
    4. public LobbyManager(SmashCraft plugin) {
    5. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    6. }
    7.  
    8.  
    9.  
    10. public static int size;
    11. static Logger log = Logger.getLogger("Minecraft");
    12. static Location lobbyspawnloc = new Location(Bukkit.getWorld("world"),-363, 4, -687);
    13.  
    14. public static Map<String, List<String>> gamepList = new HashMap<String, List<String>>();
    15.  
    16. static public Map<Location, Location> joinSigns = new HashMap<Location, Location>(); // HashMap of every Signs in the lobby, Keys are sign location, values are destination location
    17. static public Map<Location, Integer> gameInt = new HashMap<Location, Integer>(); // Type of game (2vs2 OR 4vs4)
    18. static public Map<Location, String> gameType = new HashMap<Location, String>(); // Type of game
    19. static public Map<String, Location> playerGame = new HashMap<String, Location>(); // Sign associate to a Player
    20.  
    21. SmashCraft sc;
    22.  
    23.  
    24. List<Player> nlobbyplist = new ArrayList<Player>(); // List of players who're not in the Lobby
    25. List<Player> lobbyplist = new ArrayList<Player>(); // List of players who're in the Lobby
    26.  
    27.  
    28.  
    29.  
    30. @EventHandler
    31. public void OnPlayerJoin(PlayerJoinEvent event){
    32. Player p = event.getPlayer();
    33. event.getPlayer().teleport(lobbyspawnloc);
    34. if (nlobbyplist.contains(p)){
    35. nlobbyplist.remove(p);
    36. }
    37. }
    38.  
    39. @EventHandler
    40. public void OnPlayerDisconnect(PlayerQuitEvent event){
    41. Player player = event.getPlayer();
    42. Location psloc = playerGame.get(player.getName());
    43. String game = gameType.get(psloc);
    44. if (gamepList.containsKey(game)){
    45. if(gamepList.get(game).contains(player.getName())){
    46. gamepList.get(game).remove(player.getName());
    47. int size = gamepList.get(game).size();
    48. log.info("SIZE after disconnect = " + size);
    49. SignUpdater(playerGame.get(player.getName()), size);
    50. }
    51. }
    52. if(lobbyplist.contains(player)){
    53. lobbyplist.remove(player);
    54. }
    55. }
    56.  
    57. public void TabListLobbyUpdate(){
    58. lobbyplist = Bukkit.getWorld("world").getPlayers();
    59. for (Player nplayer : lobbyplist) {
    60. for (Player player : nlobbyplist){
    61. nplayer.hidePlayer(player);
    62. }
    63.  
    64. }
    65. }
    66.  
    67.  
    68.  
    69. @EventHandler
    70. public void PlayerClickOnSign(PlayerInteractEvent event) {
    71. Player player = event.getPlayer();
    72. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
    73. Block b = event.getClickedBlock();
    74. if (b.getType().equals(Material.SIGN) || b.getType().equals(Material.SIGN_POST) ||
    75. b.getType().equals(Material.WALL_SIGN)) {
    76. nlobbyplist.add(player);
    77. TabListLobbyUpdate();
    78. Sign s = (Sign) b.getState();
    79. double xsign = 0;
    80. double zsign = 0;
    81. double ysign = 0;
    82. Location sloc = new Location(Bukkit.getWorld("World"), xsign, zsign, ysign);
    83. sloc = s.getLocation();
    84. String gname = gameType.get(sloc);
    85.  
    86.  
    87. if(joinSigns.containsKey(sloc)) {
    88.  
    89. if(gameInt.get(sloc) == 2){
    90. int size = gamepList.get(gname).size();
    91. if (size > 4) {
    92. player.sendMessage(ChatColor.RED + "This game is full");
    93. }
    94. if (size <= 3) {
    95. if (gamepList.containsKey(gname)){
    96. gamepList.get(gname).add(player.getName());
    97. } else {
    98. List<String> playerNames = new ArrayList<>();
    99. playerNames.add(player.getName());
    100. gamepList.put(gname, playerNames);
    101. }
    102.  
    103. player.sendMessage(ChatColor.GREEN + "Waiting for other people");
    104. player.sendMessage(ChatColor.GREEN + "Choose your team (/team blue or /team red)");
    105. player.teleport(joinSigns.get(event.getClickedBlock().getLocation()));
    106. size = gamepList.get(gname).size();
    107. SignUpdater(sloc, size);
    108. playerGame.put(player.getName(), sloc);
    109.  
    110. }
    111. if (size == 4) {
    112. if (gamepList.containsKey(gname)){
    113. gamepList.get(gname).add(player.getName());
    114. } else {
    115. List<String> playerNames = new ArrayList<>();
    116. playerNames.add(player.getName());
    117. gamepList.put(gname, playerNames);
    118. }
    119. player.teleport(joinSigns.get(event.getClickedBlock().getLocation()));
    120. size = gamepList.get(gname).size();
    121. SignUpdater(sloc, size);
    122. playerGame.put(player.getName(), sloc);
    123. for(String lplayer: gamepList.get(gname)){
    124. Player p = Bukkit.getServer().getPlayer(lplayer);
    125. p.sendMessage(ChatColor.DARK_GRAY + "Game starting in 20 seconds");
    126. p.sendMessage(ChatColor.DARK_GRAY + "Don't forget to choose your team (/team blue or /team red)");
    127.  
    128. }
    129.  
    130. }
    131.  
    132.  
    133.  
    134. }
    135.  
    136.  
    137. if(gameInt.get(sloc) == 4){
    138. int size = gamepList.get(gname).size();
    139. if (size > 8) {
    140. player.sendMessage(ChatColor.RED + "This game is full");
    141. }
    142. if (size <= 7) {
    143. if (gamepList.containsKey(gname)){
    144. gamepList.get(gname).add(player.getName());
    145. } else {
    146. List<String> playerNames = new ArrayList<>();
    147. playerNames.add(player.getName());
    148. gamepList.put(gname, playerNames);
    149. }
    150. player.sendMessage(ChatColor.GREEN + "Waiting for other people");
    151. player.sendMessage(ChatColor.GREEN + "Choose your team (/team blue or /team red)");
    152. player.teleport(joinSigns.get(event.getClickedBlock().getLocation()));
    153. size = gamepList.get(gname).size();
    154. SignUpdater(sloc, size);
    155. playerGame.put(player.getName(), sloc);
    156. }
    157. if (size == 8) {
    158. if (gamepList.containsKey(gname)){
    159. gamepList.get(gname).add(player.getName());
    160. } else {
    161. List<String> playerNames = new ArrayList<>();
    162. playerNames.add(player.getName());
    163. gamepList.put(gname, playerNames);
    164. }
    165. player.teleport(joinSigns.get(event.getClickedBlock().getLocation()));
    166. size = gamepList.get(gname).size();
    167. SignUpdater(sloc, size);
    168. playerGame.put(player.getName(), sloc);
    169. for(String lplayer: gamepList.get(gname)){
    170. Player p = Bukkit.getServer().getPlayer(lplayer);
    171. p.sendMessage(ChatColor.GREEN + "[Rush 2vs2] " + ChatColor.DARK_GRAY + "Game starting in 20 seconds");
    172. p.sendMessage(ChatColor.GREEN + "[Rush 2vs2] " + ChatColor.DARK_GRAY + "Don't forget to choose your team (/team blue or /team red)");
    173. }
    174. }
    175. }
    176. }
    177. }
    178. }
    179. }
    180.  
    181.  
    182.  
    183.  
    184.  
    185. public static void SignUpdater(Location bLocation,Integer size) {
    186. World w = bLocation.getWorld();
    187. Block b = w.getBlockAt(bLocation);
    188. if(b.getTypeId() == Material.SIGN_POST.getId() || b.getTypeId() == Material.WALL_SIGN.getId()) {
    189. if(joinSigns.containsKey(bLocation)){
    190. if(gameInt.get(bLocation) == 2){
    191. Sign s = (Sign) b.getState();
    192. log.info("Size = " + size);
    193. if(size <= 3){
    194. s.setLine(2, size + " / 4");
    195. s.setLine(1, "#JOIN");
    196. s.update();
    197. }
    198. if(size == 4){
    199. s.setLine(2, size + " / 4");
    200. s.setLine(1, "#FULL");
    201. s.update();
    202. }
    203. }
    204. if(gameInt.get(bLocation) == 4){
    205. Sign s = (Sign) b.getState();
    206. log.info("Size = " + size);
    207. if(size <= 7){
    208. s.setLine(2, size + " / 8");
    209. s.setLine(1, "#JOIN");
    210. s.update();
    211. }
    212. if(size == 8){
    213. s.setLine(2, size + " / 8");
    214. s.setLine(1, "#FULL");
    215. s.update();
    216. }
    217. }
    218. }
    219. }
    220. }
    221.  
    222.  
    223.  
    224.  
    225.  
    226. public static void CheckSignLocation(){
    227. int xmax = -359;
    228. int x = -371;
    229. int ymax = 13;
    230. int y = 11;
    231. int z = -720;
    232. World w = Bukkit.getWorld("world");
    233. List<String> playerNames = new ArrayList<>();
    234. while (y <= ymax){
    235.  
    236. for(x = -371; x <= xmax; x++){
    237.  
    238.  
    239. Block b = w.getBlockAt(x, y, z);
    240. if(b.getType().equals(Material.SIGN) || b.getType().equals(Material.SIGN_POST) ||
    241. b.getType().equals(Material.WALL_SIGN)) {
    242. Sign s = (Sign) b.getState();
    243. Location sloc = new Location(Bukkit.getWorld("world"), x, y, z);
    244. sloc = s.getLocation();
    245.  
    246. if(s.getLine(0).equals("[Rush 2vs2]")){
    247. Location loc = new Location(Bukkit.getWorld("Rush2"),-232, 51, -8.50);
    248. gamepList.put("rush2", playerNames);
    249. joinSigns.put(sloc, loc);
    250. gameInt.put(sloc, 2);
    251. gameType.put(sloc, "rush2");
    252. SignUpdater(sloc,0);
    253. log.info("Rush 2");
    254. }
    255. if(s.getLine(0).equals("[Rush 4vs4]")){
    256. Location loc = new Location(Bukkit.getWorld("Rush4"),-232, 51, -8.50);
    257. gamepList.put("rush4", playerNames);
    258. joinSigns.put(sloc, loc);
    259. gameInt.put(sloc, 4);
    260. gameType.put(sloc, "rush4");
    261. SignUpdater(sloc,0);
    262. log.info("Rush 4");
    263. }
    264. /* if(s.getLine(0).equals("[Nail 2vs2]")){
    265. joinSigns.put(sloc, Bukkit.getWorld("Nail2"));
    266. log.info("Nail 2");
    267. }
    268. if(s.getLine(0).equals("[Nail 4vs4]")){
    269. joinSigns.put(sloc, Bukkit.getWorld("Nail4"));
    270. log.info("Nail 4");
    271. }
    272. if(s.getLine(0).equals("[Tower 2vs2]")){
    273. joinSigns.put(sloc, Bukkit.getWorld("Tower2"));
    274. log.info("Tower 2");
    275. }
    276. if(s.getLine(0).equals("[Tower 4vs4]")){
    277. joinSigns.put(sloc, Bukkit.getWorld("Tower4"));
    278. log.info("Tower 4");
    279. } */
    280. }
    281.  
    282. }
    283. y++;
    284. }
    285. }
    286.  
    287. }
    288.  


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

Share This Page