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

    Miffy

    Hi everyone !

    I'm making a plugin who teleport the player to a game when he hit a sign.
    To know how many players are in the game, I use an arraylist who store player (I'm okay with memory leaks, i will fix that later) and update every sign.
    This is impossible for me to create an arraylist for every signs because i don't know on many signs there are.

    This is possible to store every player in the same arraylist (instantiate ?) ?
    How to do this and how to call the right one ?

    Thanks.
     
  2. Offline

    soulofw0lf

    Map<String, List<String>> arenaSigns = new HashMap<>();
    store the player name in the list string, call it with a string to get a specific one
    arenaSigns.get(arena1); returns the list for arena one
     
  3. Offline

    Miffy

    Hum, some issue with this.

    How to put player name into the list ?
    I have try :
    Code:java
    1. gamepList.put("arena1",add(player));


    But this didn't work.

    Bump

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

    Taketheword

    I would approach this with a totally different method but using hashmaps is probably better.
     
  5. Offline

    Miffy

  6. Offline

    Taketheword

    Use a vector to store the players:
    Code:
    Vector<String> playerNames = new Vector<String>()
    playerNames.add(player.getName());
    
     
  7. Offline

    soulofw0lf

    gamepList.get("arena1").add(player.getName());
     
    Taketheword likes this.
  8. Offline

    Miffy

    Taketheword But this way, we have the same problem of arraylist.
    How to call a specific one with vector ?
     
  9. Offline

    Taketheword

    That is the ideal method ^^
     
  10. Offline

    Miffy

    soulofw0lf Thank you, all my problems are solved
     
  11. Offline

    soulofw0lf

    np sorry i didn't respond sooner, i got lost in coding :p

    please remember to mark this thread as solved!

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

    Taketheword

    I learned all about this while programming with sockets.
    If you're wanting to get a specific arena, then get the players of that arena it would look something like this:
    Code:
    Vector<Vector<String>> playerVectors = new Vector<Vector<String>>();
    Vector<String> arenas = new Vector<String>();
    //add an arena
    arenas.add("Arena1");
    playerVectors.add(new Vector<String>());
    //add a player to that arena
    playerVectors.elementAt(arenas.indexOf("Arena1").add(player.getName());
    But like I said, I would use soulofw0lf 's method.
     
  13. Offline

    Miffy

    It's done, no problem for the time of the answer, it's cool.
     
  14. Offline

    soulofw0lf

    that is the most overly complicated convoluted map system i have ever seen put into use... i LOVE it :p but seriously.. learn hashmaps!
     
  15. Offline

    Miffy

    Taketheword I keep this topic on my bookmark, you're way could be helpful someday.
     
  16. Offline

    soulofw0lf

    i wouldn't bother miffy, that's really just a bad version of a hashmap, it does the same thing, but is harder to setup / use.
     
    Taketheword likes this.
  17. Offline

    Taketheword

    I find it funny because I tend to use this method out of pure laziness because I rarely take the time to learn/review on hashmaps. But seriously that works! You should try it ;D

    He's right but for me I understand it so its not really harder to setup XD

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

    Miffy

    soulofw0lf Okay, just one last question specific to HashMap. This is impossible to have two hashmap with the same key ? I hope this is impossible.
     
  19. Offline

    Taketheword

  20. Offline

    Miffy

    Taketheword
    In the soulofw0lf exemple, the key is the string and the value is the list of string.
     
  21. Offline

    Taketheword

    It is possible to have 2 of the same keys. Because in reality the key/string list is just an index in that hashmap. I suggest checking if that hashmap has that key before adding another key.
     
  22. Offline

    soulofw0lf

    the map can have as many keys as you want in it and each returns it's own seperate values
     
  23. Offline

    Taketheword

    soulofw0lf
    he's asking if the map can have 2 of the same key. He needs to check that map to see if it already has a particular key before addinga new one.
     
  24. Offline

    Miffy

    Okay, thank you again.
     
  25. Offline

    soulofw0lf

    ohhh yeah sorry misunderstood
    if (mapName.containsKey("arena1")){
    mapName.get("arena1").add(player.getName());
    } else {
    List<string> playerNames = new ArrayList<>();
    playerNames.add(player.getName();
    mapName.put("arena1", playerNames);
    }

    you just have to pretend that all those syntax errors aren't in there :p

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

    Taketheword

    Code:
    if (mapName.containsKey("arena1")){
        mapName.get("arena1").add(player.getName());
    } else {
        List<String> playerNames = new ArrayList<>();
        playerNames.add(player.getName();
        mapName.put("arena1", playerNames);
    }
    
     
  27. Offline

    soulofw0lf

    playerNames.add(player.getName();
    missed one :p
    playerNames.add(player.getName());
     
  28. Offline

    Miffy

    soulofw0lf

    I got a NPE and I didn't know why.
    Code:java
    1. if(joinSigns.containsKey(sloc)) {
    2.  
    3.  
    4.  
    5. if(gameInt.get(sloc) == 2){
    6. int size = gamepList.size();
    7. if (size > 4) {
    8. player.sendMessage(ChatColor.RED + "This game is full");
    9. }
    10. if (size <= 3) {
    11.  
    12. gamepList.get(gname).add(player.getName()); // NPE at this line
    13. player.sendMessage(ChatColor.GREEN + "Waiting for other people");
    14. player.sendMessage(ChatColor.GREEN + "Choose your team (/team blue or /team red)");
    15. player.teleport(joinSigns.get(event.getClickedBlock().getLocation()));
    16. size = gamepList.size();
    17. SignUpdater(sloc, size);
    18. playerGame.put(player.getName(), sloc);
    19. log.info("lol3");
    20.  
    21. }


    Before, i have declared gamepList this way:
    Code:java
    1. gamepList.put("rush2", null);




    EDIT: I've solved this on my own, the problem was on the declaration.
     
  29. Offline

    soulofw0lf

    yeah just now got the message :) glad you solved it!
     
  30. Offline

    Miffy

    soulofw0lf

    Still got NPE..
    My whole LobbyManager class:
    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.  
    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 (rush or nail or tower)
    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);
    43. String pstring = gameType.get(psloc);
    44. if(gamepList.get(pstring).contains(player.getName())){ // NPE HERE
    45. gamepList.get(pstring).remove(player.getName());
    46. int size = gamepList.get(pstring).size();
    47. log.info("SIZE after disconnect = " + size);
    48. SignUpdater(playerGame.get(player), size);
    49. }
    50. if(lobbyplist.contains(player)){
    51. lobbyplist.remove(player);
    52. }
    53. }
    54.  
    55. public void TabListLobbyUpdate(){
    56. lobbyplist = Bukkit.getWorld("world").getPlayers();
    57. for (Player nplayer : lobbyplist) {
    58. for (Player player : nlobbyplist){
    59. nplayer.hidePlayer(player);
    60. }
    61.  
    62. }
    63. }
    64.  
    65.  
    66.  
    67. @EventHandler
    68. public void PlayerClickOnSign(PlayerInteractEvent event) {
    69. Player player = event.getPlayer();
    70. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
    71. Block b = event.getClickedBlock();
    72. if (b.getType().equals(Material.SIGN) || b.getType().equals(Material.SIGN_POST) ||
    73. b.getType().equals(Material.WALL_SIGN)) {
    74. nlobbyplist.add(player);
    75. TabListLobbyUpdate();
    76. log.info("What ?");
    77. Sign s = (Sign) b.getState();
    78. double xsign = 0;
    79. double zsign = 0;
    80. double ysign = 0;
    81. Location sloc = new Location(Bukkit.getWorld("World"), xsign, zsign, ysign);
    82. sloc = s.getLocation();
    83. String gname = gameType.get(sloc);
    84.  
    85.  
    86. if(joinSigns.containsKey(sloc)) {
    87.  
    88. if(gameInt.get(sloc) == 2){
    89. int size = gamepList.get(gname).size();
    90. if (size > 4) {
    91. player.sendMessage(ChatColor.RED + "This game is full");
    92. }
    93. if (size <= 3) {
    94. if (gamepList.containsKey(gname)){
    95. gamepList.get(gname).add(player.getName());
    96. } else {
    97. List<String> playerNames = new ArrayList<>();
    98. playerNames.add(player.getName());
    99. gamepList.put(gname, playerNames);
    100. }
    101.  
    102. player.sendMessage(ChatColor.GREEN + "Waiting for other people");
    103. player.sendMessage(ChatColor.GREEN + "Choose your team (/team blue or /team red)");
    104. player.teleport(joinSigns.get(event.getClickedBlock().getLocation()));
    105. size = gamepList.get(gname).size();
    106. SignUpdater(sloc, size);
    107. playerGame.put(player.getName(), sloc);
    108. log.info("lol3");
    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. }
    186. }
    187. }
    188.  
    189.  
    190.  
    191.  
    192.  
    193. public static void SignUpdater(Location bLocation, int size) {
    194. World w = bLocation.getWorld();
    195. Block b = w.getBlockAt(bLocation);
    196. if(b.getTypeId() == Material.SIGN_POST.getId() || b.getTypeId() == Material.WALL_SIGN.getId()) {
    197. Sign s = (Sign) b.getState();
    198. log.info("Size = " + size);
    199. if(size <= 3){
    200. s.setLine(2, size + " / 4");
    201. s.setLine(1, "#JOIN");
    202. s.update();
    203. }
    204. if(size == 4){
    205. s.setLine(2, size + " / 4");
    206. s.setLine(1, "#FULL");
    207. s.update();
    208. }
    209. }
    210. }
    211.  
    212.  
    213.  
    214.  
    215.  
    216. public static void CheckSignLocation(){
    217. int xmax = -359;
    218. int x = -371;
    219. int ymax = 13;
    220. int y = 11;
    221. int z = -720;
    222. World w = Bukkit.getWorld("world");
    223. List<String> playerNames = new ArrayList<>();
    224. while (y <= ymax){
    225.  
    226. for(x = -371; x <= xmax; x++){
    227.  
    228.  
    229. Block b = w.getBlockAt(x, y, z);
    230. if(b.getType().equals(Material.SIGN) || b.getType().equals(Material.SIGN_POST) ||
    231. b.getType().equals(Material.WALL_SIGN)) {
    232. Sign s = (Sign) b.getState();
    233. Location sloc = new Location(Bukkit.getWorld("world"), x, y, z);
    234. sloc = s.getLocation();
    235.  
    236. if(s.getLine(0).equals("[Rush 2vs2]")){
    237. Location loc = new Location(Bukkit.getWorld("Rush2"),-232, 51, -8.50);
    238. gamepList.put("rush2", playerNames);
    239. joinSigns.put(sloc, loc);
    240. gameInt.put(sloc, 2);
    241. gameType.put(sloc, "rush2");
    242. SignUpdater(sloc,0);
    243. log.info("Rush 2");
    244. }
    245. if(s.getLine(0).equals("[Rush 4vs4]")){
    246. Location loc = new Location(Bukkit.getWorld("Rush4"),-232, 51, -8.50);
    247. gamepList.put("rush4", playerNames);
    248. joinSigns.put(sloc, loc);
    249. gameInt.put(sloc, 4);
    250. gameType.put(sloc, "rush4");
    251. SignUpdater(sloc,0);
    252. log.info("Rush 4");
    253. }
    254.  
    255. }
    256.  
    257. }
    258. y++;
    259. }
    260. }
    261. }
    262.  


    And my LobbyManagerCmd class:

    Code:java
    1. public class LobbyManagerCmd implements CommandExecutor{
    2.  
    3. Logger log = Logger.getLogger("Minecraft");
    4.  
    5. public LobbyManagerCmd(SmashCraft plugin) {
    6.  
    7. }
    8.  
    9.  
    10.  
    11. @Override
    12. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    13. Player player = (Player) sender;
    14. Location psloc = LobbyManager.playerGame.get(player);
    15. String pstring = LobbyManager.gameType.get(psloc);
    16. if(cmd.getName().equalsIgnoreCase("lobby")&& args.length == 0) {
    17. player.teleport(LobbyManager.lobbyspawnloc);
    18. if(LobbyManager.gamepList.get(pstring).contains(player.getName())){ // NPE HERE
    19. LobbyManager.gamepList.get(pstring).remove(player.getName());
    20. int size = LobbyManager.gamepList.get(pstring).size();
    21. log.info("SIZE after disconnect = " + size);
    22. LobbyManager.SignUpdater(LobbyManager.playerGame.get(player), size);
    23.  
    24. }
    25. return true;
    26. }
    27. return false;
    28. }
    29.  
    30.  
    31. }
    32.  
     
Thread Status:
Not open for further replies.

Share This Page