Solved Looking data in an ArrayList is not working as expected

Discussion in 'Plugin Development' started by Heracles421, Dec 20, 2013.

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

    Heracles421

    I was trying to run a piece of code if the ArrayList contains the player that triggered an event, but it doesn't seem to work properly.
    I know that the event triggers properly, but when it checks if the player's name is inside the ArrayList it doesn't work.

    Code:java
    1. //Leaves the blue block trail behind the player
    2. @EventHandler
    3. public void setWoolEffectBlue(PlayerMoveEvent event) {
    4. Player player = event.getPlayer();
    5. Location loc = event.getPlayer().getLocation();
    6. World world = loc.getWorld();
    7. if (this.blue.contains(player.getName())) {
    8. Bukkit.broadcastMessage("I work!");
    9. loc.setY(loc.getY() - 1);
    10. Block block = world.getBlockAt(loc);
    11. block.setType(Material.DIAMOND_BLOCK);
    12. Location location = block.getLocation();
    13. if (!this.blocksChanged.contains(location)) {
    14. this.blocksChanged.add(location);
    15. }
    16. }
    17. }
    18.  
    19. //Leaves the red block trail behind the player
    20. @EventHandler
    21. public void setWoolEffectRed(PlayerMoveEvent event) {
    22. Player player = event.getPlayer();
    23. Location loc = event.getPlayer().getLocation();
    24. World world = loc.getWorld();
    25. if (this.red.contains(player.getName())) {
    26. Bukkit.broadcastMessage("I work!");
    27. loc.setY(loc.getY() - 1);
    28. Block block = world.getBlockAt(loc);
    29. block.setType(Material.REDSTONE_BLOCK);
    30. Location location = block.getLocation();
    31. if (!this.blocksChanged.contains(location)) {
    32. this.blocksChanged.add(location);
    33. }
    34. }
    35. }


    This is how I'm adding players to the ArrayList:
    Code:java
    1. if (this.blue.size() < average) {
    2. if (!this.blue.contains(player.getName()) && !this.red.contains(player.getName())) {
    3. this.blue.add(player.getName());
    4. player.sendMessage(ChatColor.BLUE + "You have been added to the " + ChatColor.AQUA + "blue" + ChatColor.BLUE + " team!");
    5. }
    6. } else if (this.red.size() < average) {
    7. if (!this.blue.contains(player.getName()) && !this.red.contains(player.getName())) {
    8. this.red.add(player.getName());
    9. player.sendMessage(ChatColor.BLUE + "You have been added to the " + ChatColor.RED + "red" + ChatColor.BLUE + " team!");
    10. }
    11. }


    And this is how I defined the ArrayList:
    Code:java
    1. public ArrayList<String> red = new ArrayList<String>();
    2. public ArrayList<String> blue = new ArrayList<String>();


    There's no error displaying in the console, which means that the code works fine, but it doesn't check the condition as it should.
    Not sure what's wrong, please help me, I've tried every single thing that has popped in my mind, but it doesn't work.

    ~ Heracles
     
  2. Offline

    xTrollxDudex

    Heracles421
    What's "not going to work"? Elaborate.
     
  3. Offline

    _Cookie_

    I'm pretty sure the array list should be:
    public ArrayList<Player> red = new ArrayList<Player>();
    And
    public ArrayList<Player> blue = new ArrayList<Player>();

    I might be wrong but it is just a stab at the problem :)
     
  4. Offline

    Heracles421

    xTrollxDudex the If statements at lines 7 and 25
    The problem is that whenever the PlayerMove event is triggered, even though the player's name is inside the array list, the code inside the if condition is not executed
     
  5. Offline

    Developing

  6. Offline

    Heracles421

    *Bump*
    I still don't know what's wrong. Maybe it's not the if statement, maybe it's the Player player = event.getPlayer().
    Should I use Player player = (Player) event.getPlayer()?
     
  7. Offline

    xTrollxDudex

    Heracles421
    Is the list in different instances of the class?
     
  8. Offline

    Heracles421

    xTrollxDudex Nope, it's defined in the class intself. Here is the code of the full class:
    Show Spoiler

    Code:java
    1. package me.ianespana.Tron;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.World;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.entity.PlayerDeathEvent;
    17. import org.bukkit.event.player.PlayerMoveEvent;
    18.  
    19. public class GameManager implements Listener {
    20.  
    21. int taskID;
    22. private int getTaskID() {
    23. return taskID;
    24. }
    25.  
    26. public Tron plugin;
    27. public GameManager(Tron tron) {
    28. this.plugin = tron;
    29. }
    30.  
    31. String tronHeader = ChatColor.WHITE + "[" + ChatColor.BLUE + "TRON" + ChatColor.WHITE + "]";
    32.  
    33. public boolean allowJoin = true;
    34.  
    35. public ArrayList<Location> blocksChanged = new ArrayList<Location>();
    36. public ArrayList<String> gamePlayerList = new ArrayList<String>();
    37. public ArrayList<String> red = new ArrayList<String>();
    38. public ArrayList<String> blue = new ArrayList<String>();
    39.  
    40. //Happens when the player issues the /tron join command
    41. public void join(CommandSender sender, Command cmd, String[] args) {
    42. Player player = (Player) sender;
    43. if (!this.allowJoin) {
    44. player.sendMessage(ChatColor.RED + "The game has already started!");
    45. } else {
    46. if (!this.gamePlayerList.contains(player.getName())) {
    47. this.gamePlayerList.add(player.getName());
    48. player.sendMessage(ChatColor.BLUE + "You have been added to the game!");
    49.  
    50. World lobbyWorld = plugin.getServer().getWorld(plugin.getConfig().getString("lobbySpawn.World"));
    51. double xLobby = plugin.getConfig().getDouble("lobbySpawn.X");
    52. double yLobby = plugin.getConfig().getDouble("lobbySpawn.Y");
    53. double zLobby = plugin.getConfig().getDouble("lobbySpawn.Z");
    54. float lobbyPitch = plugin.getConfig().getInt("lobbySpawn.Pitch");
    55. float lobbyYaw = plugin.getConfig().getInt("lobbySpawn.Yaw");
    56.  
    57. Location lobby = new Location(lobbyWorld, xLobby, yLobby, zLobby, lobbyPitch, lobbyYaw);
    58.  
    59. player.teleport(lobby);
    60. int average = (int) Math.ceil(1.0D * gamePlayerList.size() / 2.0D);
    61.  
    62. if (this.blue.size() < average) {
    63. if (!this.blue.contains(player.getName()) && !this.red.contains(player.getName())) {
    64. this.blue.add(player.getName());
    65. player.sendMessage(ChatColor.BLUE + "You have been added to the " + ChatColor.AQUA + "blue" + ChatColor.BLUE + " team!");
    66. }
    67. } else if (this.red.size() < average) {
    68. if (!this.blue.contains(player.getName()) && !this.red.contains(player.getName())) {
    69. this.red.add(player.getName());
    70. player.sendMessage(ChatColor.BLUE + "You have been added to the " + ChatColor.RED + "red" + ChatColor.BLUE + " team!");
    71. }
    72. }
    73. } else {
    74. player.sendMessage(ChatColor.RED + "You are already a part of Tron!");
    75. }
    76. }
    77. }
    78.  
    79. //Happens when the player issues the /tron leave command
    80. public void leave(CommandSender sender, Command cmd, String[] args) {
    81. Player player = (Player) sender;
    82. if (this.gamePlayerList.contains(player.getName())) {
    83. player.sendMessage(ChatColor.RED + "You have left the game!");
    84. this.gamePlayerList.remove(player.getName());
    85. this.blue.remove(player.getName());
    86. this.red.remove(player.getName());
    87. } else {
    88. player.sendMessage(ChatColor.RED + "You are not in a game of Tron!");
    89. }
    90. }
    91.  
    92. // Runs the pre-game timer, which teleports players inside the arena
    93. public void gameTimer() {
    94. this.allowJoin = true;
    95.  
    96. taskID = this.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable() {
    97. int taskID = -1;
    98.  
    99. @Override
    100. public void run() {
    101. if (taskID == -1){
    102. taskID = getTaskID();
    103. }
    104. if (gamePlayerList.size() >= 1){
    105. GameManager.this.plugin.getServer().getScheduler().cancelTask(taskID);
    106. Bukkit.broadcastMessage(GameManager.this.tronHeader + ChatColor.BLUE + " is now starting! Do /tron join to join the game!");
    107. GameManager.this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(GameManager.this.plugin, new Runnable() {
    108. public void run() {
    109. World worldBlue = GameManager.this.plugin.getServer().getWorld(GameManager.this.plugin.getConfig().getString("blueSpawn.World"));
    110. double xBlue = GameManager.this.plugin.getConfig().getDouble("blueSpawn.X");
    111. double yBlue = GameManager.this.plugin.getConfig().getDouble("blueSpawn.Y");
    112. double zBlue = GameManager.this.plugin.getConfig().getDouble("blueSpawn.Z");
    113. float bluePitch = GameManager.this.plugin.getConfig().getInt("blueSpawn.Pitch");
    114. float blueYaw = GameManager.this.plugin.getConfig().getInt("blueSpawn.Yaw");
    115.  
    116. World worldRed = GameManager.this.plugin.getServer().getWorld(GameManager.this.plugin.getConfig().getString("redSpawn.World"));
    117. double xRed = GameManager.this.plugin.getConfig().getDouble("redSpawn.X");
    118. double yRed = GameManager.this.plugin.getConfig().getDouble("redSpawn.Y");
    119. double zRed = GameManager.this.plugin.getConfig().getDouble("redSpawn.Z");
    120. float redPitch = GameManager.this.plugin.getConfig().getInt("redSpawn.Pitch");
    121. float redYaw = GameManager.this.plugin.getConfig().getInt("redSpawn.Yaw");
    122.  
    123. Location blueSpawn = new Location(worldBlue, xBlue, yBlue, zBlue, blueYaw, bluePitch);
    124. Location redSpawn = new Location(worldRed, xRed, yRed, zRed, redYaw, redPitch);
    125.  
    126. for (String player : GameManager.this.blue) {
    127. Bukkit.getPlayer(player).teleport(blueSpawn);
    128. }
    129. for (String player : GameManager.this.red) {
    130. Bukkit.getPlayer(player).teleport(redSpawn);
    131. }
    132. GameManager.this.allowJoin = false;
    133. }
    134. }, GameManager.this.plugin.getConfig().getLong("beginTime") * 20L);
    135. }
    136. }
    137. }, 0, 20);
    138. }
    139.  
    140. //Happens when the player issues the /tron setspawn command
    141. public void setSpawn(CommandSender sender, Command cmd, String[] args) {
    142. Player player = (Player) sender;
    143. Location loc = player.getLocation();
    144. if (args[1].equalsIgnoreCase("blue")) {
    145. plugin.getConfig().set("blueSpawn.World", loc.getWorld().getName());
    146. plugin.getConfig().set("blueSpawn.X", Double.valueOf(loc.getX()));
    147. plugin.getConfig().set("blueSpawn.Y", Double.valueOf(loc.getY()));
    148. plugin.getConfig().set("blueSpawn.Z", Double.valueOf(loc.getZ()));
    149. plugin.getConfig().set("blueSpawn.Yaw", Float.valueOf(loc.getYaw()));
    150. plugin.getConfig().set("blueSpawn.Pitch", Float.valueOf(loc.getPitch()));
    151. plugin.saveConfig();
    152. player.sendMessage(ChatColor.BLUE + "You have set the " + ChatColor.AQUA + "blue" + ChatColor.BLUE + " spawn!");
    153. } else if (args[1].equalsIgnoreCase("red")) {
    154. plugin.getConfig().set("redSpawn.World", loc.getWorld().getName());
    155. plugin.getConfig().set("redSpawn.X", Double.valueOf(loc.getX()));
    156. plugin.getConfig().set("redSpawn.Y", Double.valueOf(loc.getY()));
    157. plugin.getConfig().set("redSpawn.Z", Double.valueOf(loc.getZ()));
    158. plugin.getConfig().set("redSpawn.Yaw", Float.valueOf(loc.getYaw()));
    159. plugin.getConfig().set("redSpawn.Pitch", Float.valueOf(loc.getPitch()));
    160. plugin.saveConfig();
    161. player.sendMessage(ChatColor.BLUE + "You have set the " + ChatColor.RED + "red" + ChatColor.BLUE + " spawn!");
    162. } else if (args[1].equalsIgnoreCase("lobby")) {
    163. plugin.getConfig().set("lobbySpawn.World", loc.getWorld().getName());
    164. plugin.getConfig().set("lobbySpawn.X", Double.valueOf(loc.getX()));
    165. plugin.getConfig().set("lobbySpawn.Y", Double.valueOf(loc.getY()));
    166. plugin.getConfig().set("lobbySpawn.Z", Double.valueOf(loc.getZ()));
    167. plugin.getConfig().set("lobbySpawn.Yaw", Float.valueOf(loc.getYaw()));
    168. plugin.getConfig().set("lobbySpawn.Pitch", Float.valueOf(loc.getPitch()));
    169. plugin.saveConfig();
    170. player.sendMessage(ChatColor.BLUE + "You have set the " + ChatColor.YELLOW + "lobby" + ChatColor.BLUE + " spawn!");
    171. } else if (!args[1].equalsIgnoreCase("blue") || !args[1].equalsIgnoreCase("red") || !args[1].equalsIgnoreCase("lobby")) {
    172. player.sendMessage(ChatColor.BLUE + "Unknown argument, please say /tron help");
    173. } else {
    174. player.sendMessage(ChatColor.BLUE + "Unkown command, please say /tron help.");
    175. }
    176. }
    177.  
    178. //Leaves the blue block trail behind the player
    179. @EventHandler
    180. public void setWoolEffectBlue(PlayerMoveEvent event) {
    181. Player player = (Player) event.getPlayer();
    182. Location loc = player.getLocation();
    183. World world = loc.getWorld();
    184. if (this.blue.contains(player.getName())) {
    185. player.sendMessage("I work!");
    186. loc.setY(loc.getY() - 1);
    187. Block block = world.getBlockAt(loc);
    188. block.setType(Material.DIAMOND_BLOCK);
    189. Location location = block.getLocation();
    190. if (!this.blocksChanged.contains(location)) {
    191. this.blocksChanged.add(location);
    192. }
    193. } else {
    194. player.sendMessage("NOPE!");
    195. }
    196. }
    197.  
    198. //Leaves the red block trail behind the player
    199. @EventHandler
    200. public void setWoolEffectRed(PlayerMoveEvent event) {
    201. Player player = event.getPlayer();
    202. Location loc = event.getPlayer().getLocation();
    203. World world = loc.getWorld();
    204. if (this.red.contains(player.getName())) {
    205. Bukkit.broadcastMessage("I work!");
    206. loc.setY(loc.getY() - 1);
    207. Block block = world.getBlockAt(loc);
    208. block.setType(Material.REDSTONE_BLOCK);
    209. Location location = block.getLocation();
    210. if (!this.blocksChanged.contains(location)) {
    211. this.blocksChanged.add(location);
    212. }
    213. }
    214. }
    215.  
    216. //Checks if the player should die, and if so, it kills him
    217. @EventHandler
    218. public void setDeathEvent(PlayerMoveEvent event) {
    219. World lobbyWorld = plugin.getServer().getWorld(plugin.getConfig().getString("lobbySpawn.World"));
    220. double xLobby = plugin.getConfig().getDouble("lobbySpawn.X");
    221. double yLobby = plugin.getConfig().getDouble("lobbySpawn.Y");
    222. double zLobby = plugin.getConfig().getDouble("lobbySpawn.Z");
    223. float lobbyPitch = plugin.getConfig().getInt("lobbySpawn.Pitch");
    224. float lobbyYaw = plugin.getConfig().getInt("lobbySpawn.Yaw");
    225.  
    226. Location lobby = new Location(lobbyWorld, xLobby, yLobby, zLobby, lobbyPitch, lobbyYaw);
    227.  
    228. Player player = event.getPlayer();
    229. Location loc = player.getLocation();
    230. if (this.blue.contains(player.getName()) || this.red.contains(player.getName())) {
    231. loc.setY(loc.getY() - 1.0D);
    232. Block block = loc.getBlock();
    233. if (block.getType() == Material.DIAMOND_BLOCK || block.getType() == Material.REDSTONE_BLOCK) {
    234. player.teleport(lobby);
    235. player.setHealth(0.0D);
    236. loc.getWorld().createExplosion(loc, 0.0F);
    237. }
    238. }
    239. }
    240.  
    241. //Happens when the player dies
    242. @EventHandler
    243. public void onDeathEvent(PlayerDeathEvent event) {
    244. String player = event.getEntity().getPlayer().getName();
    245. World lobbyWorld = plugin.getServer().getWorld(plugin.getConfig().getString("lobbySpawn.World"));
    246. double xLobby = plugin.getConfig().getDouble("lobbySpawn.X");
    247. double yLobby = plugin.getConfig().getDouble("lobbySpawn.Y");
    248. double zLobby = plugin.getConfig().getDouble("lobbySpawn.Z");
    249. float LobbyPitch = plugin.getConfig().getInt("lobbySpawn.Pitch");
    250. float LobbyYaw = plugin.getConfig().getInt("lobbySpawn.Yaw");
    251.  
    252. Location lobby = new Location(lobbyWorld, xLobby, yLobby, zLobby, LobbyPitch, LobbyYaw);
    253.  
    254. if ((event.getEntity() instanceof Player)) {
    255. if (this.blue.contains(player)) {
    256. this.blue.remove(player);
    257. }
    258. if (this.red.contains(player)) {
    259. this.red.remove(player);
    260. }
    261. if (this.gamePlayerList.contains(player)) {
    262. this.gamePlayerList.remove(player);
    263. }
    264.  
    265. if (this.red.size() == 0) {
    266. for (Player online : Bukkit.getOnlinePlayers()) {
    267. if (gamePlayerList.contains(online.getName())){
    268. online.sendMessage(ChatColor.BLUE + "Blue has won this game of Tron!");
    269. online.teleport(lobby);
    270. }
    271. }
    272. for (Location loc : this.blocksChanged) {
    273. loc.getBlock().setType(Material.OBSIDIAN);
    274. }
    275. this.blue.clear();
    276. this.red.clear();
    277. this.gamePlayerList.clear();
    278. this.blocksChanged.clear();
    279. this.gameTimer();
    280. }
    281. if (this.blue.size() == 0) {
    282. for (Player online : Bukkit.getOnlinePlayers()) {
    283. if (gamePlayerList.contains(online.getName())){
    284. online.sendMessage(ChatColor.RED + "Red has won this game of Tron!");
    285. online.teleport(lobby);
    286. }
    287. }
    288. for (Location loc : this.blocksChanged) {
    289. loc.getBlock().setType(Material.OBSIDIAN);
    290. }
    291. this.blue.clear();
    292. this.red.clear();
    293. this.gamePlayerList.clear();
    294. this.blocksChanged.clear();
    295. this.gameTimer();
    296. }
    297. }
    298. }
    299. }



    The trouble is in the instance located at line 179.

    By the way, you can see that I have already used the same if statement before, so I think that the problem is actually the value assigned to the variable player inside the setWoolEffectBlue instance, rather than the if statement not working properly, although I am not sure what's the problem with it, because player.sendMessage() works fine.
     
  9. Offline

    xTrollxDudex

    Heracles421
    You're not getting what I meant by instance :p

    An instance is like a copy of the same class, but each field is independently controlled in each instance.

    But, I think your problem is that you have two handlers for the same event, instead, condense them into one event and use an else if statement to check if the player is in the red list.
     
  10. Offline

    Heracles421

    xTrollxDudex
    Nope, it didn't work. But after some debbuging I think that the problem is either: A- Assigning the event.getPlayer() to the player variable, or B- Getting the ArrayList to work properly inside the if statement.

    I did some debugging and this is my conclusion. The most probable (in my opinion) is the letter A, but I still don't know how to fix it, which is driving me mad...

    Aha!
    After some more debugging, I found that the actual problem is that the ArrayList is somehow empty (Or that's what my plugin thinks)!

    I got this console error while trying to print the string in the index 0 (First index) of the "blue" ArrayList:

    Code:
    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.rangeCheck(ArrayList.java:604) ~[?:1.7.0_25]
        at java.util.ArrayList.get(ArrayList.java:382) ~[?:1.7.0_25]
        at me.ianespana.Tron.GameManager.setWoolEffect(GameManager.java:184) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_25]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_25]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
        ... 13 more
    
    Now, this leaves me a new question: why the hell is it empty?

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

    xTigerRebornx

    The players move before they can get added to the ArrayList?
     
  12. Offline

    Heracles421

    No, they get added to the list when they use a command to join the minigame
     
  13. Offline

    xTrollxDudex

  14. Offline

    Heracles421

    xTrollxDudex
    Yea, all the commands are working perfectly fine.
    Weird... just added some debug messages to the join command (Prints the arrayList's size) and it tells me that the size is 1 (Only I have joined the game, so it only has one index used), but if I do the same with the setWoolEffect piece, it tells me the size is 0, even after using the join command
     
  15. Offline

    mrkirby153

    Heracles421

    Try accessing the values from a non-static (I think) context. Remove the "this"
     
  16. Offline

    Heracles421

    mrkirby153
    Nope, still broken....
    Maybe it's just the PlayerMoveEvent thing, that triggers every time someone moves, which doesn't leave enough time for my code to get the stuff inside the ArrayList? Idk, I'm just saying stuff that comes to my mind

    Meh, I found how to fix it... So weird.
    Instead of having the ArrayList inside the GameManager class, I moved them to the main class an set them as static

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

Share This Page