Solved ConcurrentModificationException (Possibly relating to arraylists?).

Discussion in 'Plugin Development' started by SpongyBacon, Jul 29, 2014.

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

    SpongyBacon

    Hey,
    I'm currently making a plugin that when a player uses the command: /dropzone, they are added to an arraylist, and then from that, every 20 mins, if the list has 3 or more players (uuids) in the list, a block is placed at a set location randomly, which then does stuff (still in progress).

    My issue is that when the 20 mins comes around/the command /dropzone admin force-start is used and the arraylist has players in it, shoots out an error. This could be due to how the arraylist only contains one player, however it should still be able to loop through the list if there is only one player (right?).
    Note: I've set the first list to only need 1 player for debugging/testing.

    The stacktrace:
    Code:java
    1. [12:57:42] [Server thread/INFO]: SpongyBacon issued server command: /dropzone // Joining the arraylist
    2. [12:57:59] [Server thread/INFO]: SpongyBacon issued server command: /dropzone admin force-start // Forcing the block to spawn
    3. [12:57:59] [Server thread/WARN]: java.util.ConcurrentModificationException
    4. [12:57:59] [Server thread/WARN]: at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    5. [12:57:59] [Server thread/WARN]: at java.util.ArrayList$Itr.next(Unknown Source)
    6. [12:57:59] [Server thread/WARN]: at com.spongybacon.dropzone.Main.checkQueues(Main.java:92)
    7. [12:57:59] [Server thread/WARN]: at com.spongybacon.dropzone.Main.onCommand(Main.java:195)
    8. [12:57:59] [Server thread/WARN]: at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    9. [12:57:59] [Server thread/WARN]: at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
    10. [12:57:59] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701)
    11. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956)
    12. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817)
    13. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28)
    14. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47)
    15. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157)
    16. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134)
    17. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667)
    18. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260)
    19. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558)
    20. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469)
    21. [12:57:59] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    22. [12:57:59] [Server thread/INFO]: [Dropzone] Dropping package for Queue 1!
    23. [12:57:59] [Server thread/ERROR]: null
    24. org.bukkit.command.CommandException: Unhandled exception executing command 'dropzone' in plugin Dropzone v1.0
    25. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    26. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    27. at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    28. at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    29. at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    30. at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    31. at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    32. at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    33. at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    34. at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    35. at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    36. at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    37. at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    38. at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    39. Caused by: java.util.ConcurrentModificationException
    40. at java.util.ArrayList$Itr.checkForComodification(Unknown Source) ~[?:1.7.0_65]
    41. at java.util.ArrayList$Itr.next(Unknown Source) ~[?:1.7.0_65]
    42. at com.spongybacon.dropzone.Main.drop(Main.java:347) ~[?:?]
    43. at com.spongybacon.dropzone.Main.checkQueues(Main.java:101) ~[?:?]
    44. at com.spongybacon.dropzone.Main.onCommand(Main.java:195) ~[?:?]
    45. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    46. ... 13 more
    47.  


    My code: (Main class)
    Code:java
    1. package com.spongybacon.dropzone;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Random;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Location;
    10. import org.bukkit.Material;
    11. import org.bukkit.World;
    12. import org.bukkit.block.Block;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandExecutor;
    15. import org.bukkit.command.CommandSender;
    16. import org.bukkit.configuration.file.FileConfiguration;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.event.block.Action;
    21. import org.bukkit.event.inventory.InventoryCloseEvent;
    22. import org.bukkit.event.player.PlayerInteractEvent;
    23. import org.bukkit.inventory.Inventory;
    24. import org.bukkit.inventory.ItemStack;
    25. import org.bukkit.plugin.PluginManager;
    26. import org.bukkit.plugin.java.JavaPlugin;
    27.  
    28. public class Main extends JavaPlugin implements CommandExecutor, Listener {
    29.  
    30. /**
    31.   * Config layout: lastId: //This has to be the biggest number id: x: y: z:
    32.   * world: id: x: y: z: world:
    33.   */
    34.  
    35. public ArrayList<UUID> q1 = new ArrayList<UUID>();
    36. public ArrayList<UUID> q2 = new ArrayList<UUID>();
    37. public ArrayList<UUID> q3 = new ArrayList<UUID>();
    38. public ArrayList<UUID> q4 = new ArrayList<UUID>();
    39. public ArrayList<UUID> q5 = new ArrayList<UUID>();
    40.  
    41. public FileConfiguration config = getConfig();
    42.  
    43. public ArrayList<UUID> players = new ArrayList<UUID>();
    44. public ArrayList<UUID> players2 = new ArrayList<UUID>();
    45. public ArrayList<ItemStack> contents = new ArrayList<ItemStack>();
    46. public ArrayList<Location> locations = new ArrayList<Location>();
    47.  
    48. public Inventory inv = Bukkit.createInventory(null, 54, "Dropzone Package");
    49.  
    50. public void onEnable() {
    51. getLogger().info("Enabled!");
    52.  
    53. getCommand("dropzone").setExecutor(this);
    54.  
    55. PluginManager pm = getServer().getPluginManager();
    56. pm.registerEvents(this, this);
    57.  
    58. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    59. public void run() {
    60. checkQueues();
    61. }
    62. }, 24000, 24000); // 20 mins
    63. }
    64.  
    65. public void onDisable() {
    66. getLogger().info("Disabled!");
    67. }
    68.  
    69. public String prefix = ChatColor.RED + "[Dropzone]" + ChatColor.GRAY + " ";
    70. public String noPermission = prefix + "You do not have permission to do that!";
    71.  
    72. public void msg(Player p, String type, String message) {
    73. if (p == null) {
    74. getLogger().info("Could not find any players to message!");
    75. return;
    76. }
    77.  
    78. if (type.equalsIgnoreCase("nopermission")) {
    79. p.sendMessage(noPermission);
    80. } else if (type.equalsIgnoreCase("ok")) {
    81. p.sendMessage(prefix + message);
    82. } else if (type.equalsIgnoreCase("bad")) {
    83. p.sendMessage(prefix + ChatColor.DARK_RED + message);
    84. } else {
    85. return;
    86. }
    87. }
    88.  
    89. public void checkQueues() {
    90. if (q1.size() >= 1) {
    91. try {
    92. for (UUID id : q1) {
    93. players.add(id);
    94. q1.remove(id);
    95. }
    96. } catch (Exception e) {
    97. e.printStackTrace();
    98. }
    99.  
    100. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 1!");
    101. drop(players);
    102. } else if (q2.size() >= 3) {
    103. for (UUID id : q2) {
    104. players.add(id);
    105. q2.remove(id);
    106. }
    107.  
    108. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 2!");
    109. drop(players);
    110. } else if (q3.size() >= 3) {
    111. for (UUID id : q3) {
    112. players.add(id);
    113. q3.remove(id);
    114. }
    115.  
    116. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 3!");
    117. drop(players);
    118. } else if (q4.size() >= 3) {
    119. for (UUID id : q4) {
    120. players.add(id);
    121. q4.remove(id);
    122. }
    123.  
    124. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 4!");
    125. drop(players);
    126. } else if (q5.size() >= 3) {
    127. for (UUID id : q5) {
    128. players.add(id);
    129. q5.remove(id);
    130. }
    131.  
    132. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 5!");
    133. drop(players);
    134. }
    135. }
    136.  
    137. public boolean onCommand(CommandSender s, Command cmd, String label, String args[]) {
    138. if (!(s instanceof Player)) {
    139. s.sendMessage("Only players can use Dropzone!");
    140. return true;
    141. }
    142.  
    143. Player p = (Player) s;
    144. UUID id = p.getUniqueId();
    145.  
    146. if (cmd.getName().equalsIgnoreCase("dropzone")) {
    147. if (args.length == 0) {
    148. if (!(p.hasPermission("dropzone.drop")) || !(p.isOp())) {
    149. msg(p, "nopermission", "");
    150. return true;
    151. }
    152.  
    153. if (q1.size() == 3 == true && q2.size() == 3 && q3.size() == 3 && q4.size() == 3 && q5.size() == 3) {
    154. msg(p, "bad", "Sorry, but all the queues are full! Try again in a secound please.");
    155. return true;
    156. }
    157.  
    158. if (q1.size() >= 3) {
    159. if (q2.size() >= 3) {
    160. if (q3.size() >= 3) {
    161. if (q4.size() >= 3) {
    162. if (q5.size() >= 3) {
    163. msg(p, "bad", "Sorry, but all the queues are full! Try again in a second please.");
    164. return true;
    165. } else if (q5.size() >= 0) {
    166. msg(p, "ok", "Adding you to Queue 5!");
    167. q5.add(id);
    168. }
    169. } else if (q4.size() >= 0) {
    170. msg(p, "ok", "Adding you to Queue 4!");
    171. q4.add(id);
    172. }
    173. } else if (q3.size() >= 0) {
    174. msg(p, "ok", "Adding you to Queue 3!");
    175. q3.add(id);
    176. }
    177. } else if (q2.size() >= 0) {
    178. msg(p, "ok", "Adding you to Queue 2!");
    179. q2.add(id);
    180. }
    181. } else if (q1.size() >= 0) {
    182. msg(p, "ok", "Adding you to Queue 1!");
    183. q1.add(id);
    184. }
    185. } else if (args.length == 1) {
    186. if (args[0].equalsIgnoreCase("admin")) {
    187. msg(p, "ok", "Usage: /dropzone admin <setlocation / force-start> [id]");
    188. return true;
    189. } else {
    190. return false;
    191. }
    192. } else if (args.length == 2) {
    193. if (args[0].equalsIgnoreCase("admin")) {
    194. if (args[1].equalsIgnoreCase("force-start")) {
    195. checkQueues();
    196. msg(p, "ok", "Force-starting queues!");
    197. return true;
    198. }
    199. if (args[1].equalsIgnoreCase("clear-queues")) {
    200. q1.clear();
    201. q2.clear();
    202. q3.clear();
    203. q4.clear();
    204. q5.clear();
    205.  
    206. msg(p, "ok", "Cleared all queues!");
    207. return true;
    208.  
    209. } else {
    210. msg(p, "ok", "Usage: /dropzone admin <setlocation / force-start> [id]");
    211. return true;
    212. }
    213. } else {
    214. return false;
    215. }
    216. } else if (args.length == 3) {
    217. if (args[0].equalsIgnoreCase("admin")) {
    218. if (p.hasPermission("dropzone.admin") || p.isOp()) {
    219. if (args[1].equalsIgnoreCase("setlocation")) {
    220. if (Integer.parseInt(args[2]) > 0) {
    221. try {
    222. // if (config.get(args[2]) == null) {
    223. int x = p.getLocation().getBlockX();
    224. int y = p.getLocation().getBlockY();
    225. int z = p.getLocation().getBlockZ();
    226. String world = p.getLocation().getWorld().getName();
    227. p.sendMessage("Got locations");
    228.  
    229. config.set(args[2] + ".x", x);
    230. config.set(args[2] + ".y", y);
    231. config.set(args[2] + ".z", z);
    232. config.set(args[2] + ".world", world);
    233. p.sendMessage("Set locations");
    234.  
    235. saveConfig();
    236. p.sendMessage("Saved config");
    237.  
    238. msg(p, "good", "Set location for Dropzone ID: " + args[2] + "!");
    239. p.sendMessage("Sent message!");
    240. return true;
    241. // } else {
    242. // msg(p, "bad",
    243. // "Dropzone with that ID already exists! Modify it in the config!");
    244. // }
    245. } catch (Exception e) {
    246. e.printStackTrace();
    247. msg(p, "bad", e.getStackTrace().toString());
    248. }
    249. } else {
    250. msg(p, "bad", "ID must be greater than 0");
    251. }
    252. } else {
    253. return false;
    254. }
    255. } else {
    256. msg(p, "nopermission", "");
    257. return true;
    258. }
    259. }
    260. }
    261. }
    262.  
    263. return true;
    264.  
    265. }
    266.  
    267. public Location getLocation() {
    268. int lastId = config.getInt("lastId");
    269. if (config.get("lastId") == null) {
    270. config.set("lastId", 1);
    271. }
    272.  
    273. Random r = new Random();
    274. int ri = r.nextInt((lastId - 1) + 1) + 1;
    275.  
    276. int x = config.getInt(ri + ".x");
    277. int y = config.getInt(ri + ".y");
    278. int z = config.getInt(ri + ".z");
    279. String world = config.getString(ri + ".world");
    280.  
    281. World w = Bukkit.getWorld(world);
    282. Location loc = new Location(w, x, y, z);
    283.  
    284. return loc;
    285. }
    286.  
    287. public Location getPlayerLocation(Location loc) {
    288. int x = loc.getBlockX();
    289. int y = loc.getBlockY() + 2;
    290. int z = loc.getBlockZ();
    291. String world = loc.getWorld().getName();
    292.  
    293. World w = Bukkit.getWorld(world);
    294. Location pLoc = new Location(w, x, y, z);
    295.  
    296. return pLoc;
    297. }
    298.  
    299. @EventHandler
    300. public void onInteract(PlayerInteractEvent e) {
    301. final Player p = (Player) e.getPlayer();
    302. Block b = e.getClickedBlock();
    303.  
    304. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK) || !(e.getAction() == Action.LEFT_CLICK_BLOCK)) {
    305. return;
    306. }
    307.  
    308. if (!(b.getType() == Material.COBBLESTONE)) {
    309. return;
    310. }
    311.  
    312. Random r = new Random();
    313. int ri = r.nextInt();
    314. boolean rb = r.nextBoolean();
    315. int count = 0;
    316.  
    317. for (Material m : Material.values()) {
    318. if (rb == false) {
    319. return;
    320. }
    321.  
    322. if (count == 54) {
    323. return;
    324. }
    325.  
    326. ItemStack is = new ItemStack(m, ri);
    327. contents.add(is);
    328. count++;
    329. }
    330.  
    331. for (ItemStack is : contents) {
    332. inv.addItem(is);
    333. }
    334.  
    335. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    336. public void run() {
    337. p.openInventory(inv);
    338. }
    339. }, 1);
    340.  
    341. }
    342.  
    343. public void drop(ArrayList<UUID> id) {
    344. Location loc = getLocation();
    345. Location pLoc = getPlayerLocation(loc);
    346.  
    347. for(UUID uuid : id){
    348. players2.add(uuid);
    349. players.remove(uuid);
    350. }
    351.  
    352. // Create falling block
    353. loc.getBlock().setType(Material.COBBLESTONE);
    354. locations.add(loc);
    355.  
    356. for (UUID uuid : players2) {
    357. Player pl = (Player) Bukkit.getPlayer(uuid);
    358. msg(pl, "ok", "Teleporting you to the location! (" + pLoc.getBlockX() + ", " + pLoc.getBlockY() + ", " + pLoc.getBlockZ() + ")");
    359. pl.teleport(loc);
    360. players2.remove(pl);
    361. }
    362. }
    363.  
    364. @EventHandler
    365. public void onClose(InventoryCloseEvent e) {
    366. // Player p = (Player) e.getPlayer();
    367. if (e.getInventory() == inv) {
    368. for (Location l : locations) {
    369. l.getBlock().setType(Material.AIR);
    370. locations.remove(l);
    371. }
    372. } else {
    373. return;
    374. }
    375. }
    376. }
    377.  


    Line 92: com.spongybacon.dropzone.Main.checkQueues(Main.java:92)
    Code:java
    1. if (q1.size() >= 1) {
    2. try {
    3. for (UUID id : q1) { //THIS IS LINE 92
    4. players.add(id);
    5. q1.remove(id);
    6. }
    7. } catch (Exception e) {
    8. e.printStackTrace();
    9. }
    10.  
    11. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 1!");
    12. drop(players);
    13. }


    Line 195: com.spongybacon.dropzone.Main.onCommand(Main.java:195) & com.spongybacon.dropzone.Main.onCommand(Main.java:195)
    Code:java
    1. if (args[0].equalsIgnoreCase("admin")) {
    2. if (args[1].equalsIgnoreCase("force-start")) {
    3. checkQueues(); //THIS IS LINE 195
    4. msg(p, "ok", "Force-starting queues!");
    5. return true;
    6. }
    7. if (args[1].equalsIgnoreCase("clear-queues")) {
    8. q1.clear();
    9. q2.clear();
    10. q3.clear();
    11. q4.clear();
    12. q5.clear();
    13.  
    14. msg(p, "ok", "Cleared all queues!");
    15. return true;
    16.  
    17. } else {
    18. msg(p, "ok", "Usage: /dropzone admin <setlocation / force-start> [id]");
    19. return true;
    20. }
    21. }


    Line 347: com.spongybacon.dropzone.Main.drop(Main.java:347)
    Code:java
    1. public void drop(ArrayList<UUID> id) {
    2. Location loc = getLocation();
    3. Location pLoc = getPlayerLocation(loc);
    4.  
    5. for(UUID uuid : id){ //THIS IS LINE 347
    6. players2.add(uuid);
    7. players.remove(uuid);
    8. }
    9.  
    10. // Create falling block
    11. loc.getBlock().setType(Material.COBBLESTONE);
    12. locations.add(loc);
    13.  
    14. for (UUID uuid : players2) {
    15. Player pl = (Player) Bukkit.getPlayer(uuid);
    16. msg(pl, "ok", "Teleporting you to the location! (" + pLoc.getBlockX() + ", " + pLoc.getBlockY() + ", " + pLoc.getBlockZ() + ")");
    17. pl.teleport(loc);
    18. players2.remove(pl);
    19. }
    20. }


    Line 101: com.spongybacon.dropzone.Main.checkQueues(Main.java:101)
    Code:java
    1. if (q1.size() >= 1) {
    2. try {
    3. for (UUID id : q1) {
    4. players.add(id);
    5. q1.remove(id);
    6. }
    7. } catch (Exception e) {
    8. e.printStackTrace();
    9. }
    10.  
    11. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 1!");
    12. drop(players); //THIS IS LINE 101
    13. }


    Thanks, I really hope you can help.
    Oh and if you need any more info, just ask and i'll be happy to provide!
     
  2. Offline

    GeorgeeeHD

    SpongyBacon tl;dr

    did you try to remove an element from an ArrayList or any other collection while iterating over it? this causes a ConcurrentModificationException so remove that if you do.
     
    SpongyBacon likes this.
  3. Offline

    SpongyBacon

    GeorgeeeHD Tah, i'll take a look.

    GeorgeeeHD Update: Haven't found anything that suggests that- It seems to add a player to an arraylist before it removes it from another, so there is no problem there. I'll take another look!

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

    xize

    SpongyBacon

    from what ive seen is that you are reading but also on the same time changing the contents of that ArrayList which cause to throw this Exception, basicly due the fact the scheduler runs really fast per tick it even may go faster than the whole procedure your checkQueues does.

    however what you could try to fix it, is 1. slow down the scheduler a bit from 24000, 24000 to for example 24000, 30000

    because when the scheduler has waited 24000 ms, then it will run every 1L tick or lower because after the comma has the same tick rate.

    and instead of using for loops use Iterators it allows you to iterate a bit more safely even though it could still cause the same exception but really less frequently.
     
    SpongyBacon likes this.
  5. Offline

    SpongyBacon

    xize Thanks for your reply :). I've tried changing the scheduler so far, and it still bugged when I tried to force it, but I think me trying to force it to happen instead of stopping the scheduler/waiting for it is making it fuck up, so i'm going to wait like 20 mins and see if it works :D

    Edit: Didn't fix it. Now trying iterators.

    Ok, so I tried both of the things xize suggested, however they don't seem to have fixed it. When the scheduler runs, it shows this error:
    Code:java
    1. [17:58:43] [Server thread/WARN]: java.util.ConcurrentModificationException
    2. [17:58:43] [Server thread/WARN]: at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    3. [17:58:43] [Server thread/WARN]: at java.util.ArrayList$Itr.next(Unknown Source)
    4. [17:58:43] [Server thread/WARN]: at com.spongybacon.dropzone.Main.checkQueues(Main.java:96)
    5. [17:58:43] [Server thread/WARN]: at com.spongybacon.dropzone.Main$1.run(Main.java:61)
    6. [17:58:43] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53)
    7. [17:58:43] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    8. [17:58:43] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600)
    9. [17:58:43] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260)
    10. [17:58:43] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558)
    11. [17:58:43] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469)
    12. [17:58:43] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    13. [17:58:43] [Server thread/INFO]: [0;31;1m[Dropzone][0;37;22m [21mDropping package for Queue 1![m


    Line 96: com.spongybacon.dropzone.Main.checkQueues(Main.java:96)
    Code:java
    1. if (q1.size() >= 1) {
    2. try {
    3. Iterator<UUID> i = q1.iterator();
    4.  
    5. while(i.hasNext()){
    6. UUID id = i.next(); //THIS IS LINE 96
    7. players.add(id);
    8. q1.remove(id);
    9. }
    10. } catch (Exception e) {
    11. e.printStackTrace();
    12. }
    13.  
    14. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 1!");
    15. drop(players);
    16. }


    Line 61: com.spongybacon.dropzone.Main$1.run(Main.java:61)
    Code:java
    1. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. checkQueues(); //THIS IS LINE 61
    4. }
    5. }, 24000, 30000);


    Hope you can help!

    EDIT: Almost forgot, here's the updated code:
    Code:java
    1. package com.spongybacon.dropzone;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Iterator;
    5. import java.util.Random;
    6. import java.util.UUID;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Location;
    11. import org.bukkit.Material;
    12. import org.bukkit.World;
    13. import org.bukkit.block.Block;
    14. import org.bukkit.command.Command;
    15. import org.bukkit.command.CommandExecutor;
    16. import org.bukkit.command.CommandSender;
    17. import org.bukkit.configuration.file.FileConfiguration;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.event.EventHandler;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.block.Action;
    22. import org.bukkit.event.inventory.InventoryCloseEvent;
    23. import org.bukkit.event.player.PlayerInteractEvent;
    24. import org.bukkit.inventory.Inventory;
    25. import org.bukkit.inventory.ItemStack;
    26. import org.bukkit.plugin.PluginManager;
    27. import org.bukkit.plugin.java.JavaPlugin;
    28.  
    29. public class Main extends JavaPlugin implements CommandExecutor, Listener {
    30.  
    31. /**
    32.   * Config layout: lastId: //This has to be the biggest number id: x: y: z:
    33.   * world: id: x: y: z: world:
    34.   */
    35.  
    36. public ArrayList<UUID> q1 = new ArrayList<UUID>();
    37. public ArrayList<UUID> q2 = new ArrayList<UUID>();
    38. public ArrayList<UUID> q3 = new ArrayList<UUID>();
    39. public ArrayList<UUID> q4 = new ArrayList<UUID>();
    40. public ArrayList<UUID> q5 = new ArrayList<UUID>();
    41.  
    42. public FileConfiguration config = getConfig();
    43.  
    44. public ArrayList<UUID> players = new ArrayList<UUID>();
    45. public ArrayList<UUID> players2 = new ArrayList<UUID>();
    46. public ArrayList<ItemStack> contents = new ArrayList<ItemStack>();
    47. public ArrayList<Location> locations = new ArrayList<Location>();
    48.  
    49. public Inventory inv = Bukkit.createInventory(null, 54, "Dropzone Package");
    50.  
    51. public void onEnable() {
    52. getLogger().info("Enabled!");
    53.  
    54. getCommand("dropzone").setExecutor(this);
    55.  
    56. PluginManager pm = getServer().getPluginManager();
    57. pm.registerEvents(this, this);
    58.  
    59. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    60. public void run() {
    61. checkQueues();
    62. }
    63. }, 24000, 30000); // 20 mins
    64. }
    65.  
    66. public void onDisable() {
    67. getLogger().info("Disabled!");
    68. }
    69.  
    70. public String prefix = ChatColor.RED + "[Dropzone]" + ChatColor.GRAY + " ";
    71. public String noPermission = prefix + "You do not have permission to do that!";
    72.  
    73. public void msg(Player p, String type, String message) {
    74. if (p == null) {
    75. getLogger().info("Could not find any players to message!");
    76. return;
    77. }
    78.  
    79. if (type.equalsIgnoreCase("nopermission")) {
    80. p.sendMessage(noPermission);
    81. } else if (type.equalsIgnoreCase("ok")) {
    82. p.sendMessage(prefix + message);
    83. } else if (type.equalsIgnoreCase("bad")) {
    84. p.sendMessage(prefix + ChatColor.DARK_RED + message);
    85. } else {
    86. return;
    87. }
    88. }
    89.  
    90. public void checkQueues() {
    91. if (q1.size() >= 1) {
    92. try {
    93. Iterator<UUID> i = q1.iterator();
    94.  
    95. while(i.hasNext()){
    96. UUID id = i.next();
    97. players.add(id);
    98. q1.remove(id);
    99. }
    100. } catch (Exception e) {
    101. e.printStackTrace();
    102. }
    103.  
    104. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 1!");
    105. drop(players);
    106. } else if (q2.size() >= 3) {
    107. for (UUID id : q2) {
    108. players.add(id);
    109. q2.remove(id);
    110. }
    111.  
    112. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 2!");
    113. drop(players);
    114. } else if (q3.size() >= 3) {
    115. for (UUID id : q3) {
    116. players.add(id);
    117. q3.remove(id);
    118. }
    119.  
    120. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 3!");
    121. drop(players);
    122. } else if (q4.size() >= 3) {
    123. for (UUID id : q4) {
    124. players.add(id);
    125. q4.remove(id);
    126. }
    127.  
    128. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 4!");
    129. drop(players);
    130. } else if (q5.size() >= 3) {
    131. for (UUID id : q5) {
    132. players.add(id);
    133. q5.remove(id);
    134. }
    135.  
    136. Bukkit.broadcastMessage(prefix + ChatColor.BOLD + "" + "Dropping package for Queue 5!");
    137. drop(players);
    138. }
    139. }
    140.  
    141. public boolean onCommand(CommandSender s, Command cmd, String label, String args[]) {
    142. if (!(s instanceof Player)) {
    143. s.sendMessage("Only players can use Dropzone!");
    144. return true;
    145. }
    146.  
    147. Player p = (Player) s;
    148. UUID id = p.getUniqueId();
    149.  
    150. if (cmd.getName().equalsIgnoreCase("dropzone")) {
    151. if (args.length == 0) {
    152. if (!(p.hasPermission("dropzone.drop")) || !(p.isOp())) {
    153. msg(p, "nopermission", "");
    154. return true;
    155. }
    156.  
    157. if (q1.size() == 3 == true && q2.size() == 3 && q3.size() == 3 && q4.size() == 3 && q5.size() == 3) {
    158. msg(p, "bad", "Sorry, but all the queues are full! Try again in a secound please.");
    159. return true;
    160. }
    161.  
    162. if (q1.size() >= 3) {
    163. if (q2.size() >= 3) {
    164. if (q3.size() >= 3) {
    165. if (q4.size() >= 3) {
    166. if (q5.size() >= 3) {
    167. msg(p, "bad", "Sorry, but all the queues are full! Try again in a second please.");
    168. return true;
    169. } else if (q5.size() >= 0) {
    170. msg(p, "ok", "Adding you to Queue 5!");
    171. q5.add(id);
    172. }
    173. } else if (q4.size() >= 0) {
    174. msg(p, "ok", "Adding you to Queue 4!");
    175. q4.add(id);
    176. }
    177. } else if (q3.size() >= 0) {
    178. msg(p, "ok", "Adding you to Queue 3!");
    179. q3.add(id);
    180. }
    181. } else if (q2.size() >= 0) {
    182. msg(p, "ok", "Adding you to Queue 2!");
    183. q2.add(id);
    184. }
    185. } else if (q1.size() >= 0) {
    186. msg(p, "ok", "Adding you to Queue 1!");
    187. q1.add(id);
    188. }
    189. } else if (args.length == 1) {
    190. if (args[0].equalsIgnoreCase("admin")) {
    191. msg(p, "ok", "Usage: /dropzone admin <setlocation / force-start> [id]");
    192. return true;
    193. } else {
    194. return false;
    195. }
    196. } else if (args.length == 2) {
    197. if (args[0].equalsIgnoreCase("admin")) {
    198. if (args[1].equalsIgnoreCase("force-start")) {
    199. checkQueues();
    200. msg(p, "ok", "Force-starting queues!");
    201. return true;
    202. }
    203. if (args[1].equalsIgnoreCase("clear-queues")) {
    204. q1.clear();
    205. q2.clear();
    206. q3.clear();
    207. q4.clear();
    208. q5.clear();
    209.  
    210. msg(p, "ok", "Cleared all queues!");
    211. return true;
    212.  
    213. } else {
    214. msg(p, "ok", "Usage: /dropzone admin <setlocation / force-start> [id]");
    215. return true;
    216. }
    217. } else {
    218. return false;
    219. }
    220. } else if (args.length == 3) {
    221. if (args[0].equalsIgnoreCase("admin")) {
    222. if (p.hasPermission("dropzone.admin") || p.isOp()) {
    223. if (args[1].equalsIgnoreCase("setlocation")) {
    224. if (Integer.parseInt(args[2]) > 0) {
    225. try {
    226. // if (config.get(args[2]) == null) {
    227. int x = p.getLocation().getBlockX();
    228. int y = p.getLocation().getBlockY();
    229. int z = p.getLocation().getBlockZ();
    230. String world = p.getLocation().getWorld().getName();
    231. p.sendMessage("Got locations");
    232.  
    233. config.set(args[2] + ".x", x);
    234. config.set(args[2] + ".y", y);
    235. config.set(args[2] + ".z", z);
    236. config.set(args[2] + ".world", world);
    237. p.sendMessage("Set locations");
    238.  
    239. saveConfig();
    240. p.sendMessage("Saved config");
    241.  
    242. msg(p, "good", "Set location for Dropzone ID: " + args[2] + "!");
    243. p.sendMessage("Sent message!");
    244. return true;
    245. // } else {
    246. // msg(p, "bad",
    247. // "Dropzone with that ID already exists! Modify it in the config!");
    248. // }
    249. } catch (Exception e) {
    250. e.printStackTrace();
    251. msg(p, "bad", e.getStackTrace().toString());
    252. }
    253. } else {
    254. msg(p, "bad", "ID must be greater than 0");
    255. }
    256. } else {
    257. return false;
    258. }
    259. } else {
    260. msg(p, "nopermission", "");
    261. return true;
    262. }
    263. }
    264. }
    265. }
    266.  
    267. return true;
    268.  
    269. }
    270.  
    271. public Location getLocation() {
    272. int lastId = config.getInt("lastId");
    273. if (config.get("lastId") == null) {
    274. config.set("lastId", 1);
    275. }
    276.  
    277. Random r = new Random();
    278. int ri = r.nextInt((lastId - 1) + 1) + 1;
    279.  
    280. int x = config.getInt(ri + ".x");
    281. int y = config.getInt(ri + ".y");
    282. int z = config.getInt(ri + ".z");
    283. String world = config.getString(ri + ".world");
    284.  
    285. World w = Bukkit.getWorld(world);
    286. Location loc = new Location(w, x, y, z);
    287.  
    288. return loc;
    289. }
    290.  
    291. public Location getPlayerLocation(Location loc) {
    292. int x = loc.getBlockX();
    293. int y = loc.getBlockY() + 2;
    294. int z = loc.getBlockZ();
    295. String world = loc.getWorld().getName();
    296.  
    297. World w = Bukkit.getWorld(world);
    298. Location pLoc = new Location(w, x, y, z);
    299.  
    300. return pLoc;
    301. }
    302.  
    303. @EventHandler
    304. public void onInteract(PlayerInteractEvent e) {
    305. final Player p = (Player) e.getPlayer();
    306. Block b = e.getClickedBlock();
    307.  
    308. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK) || !(e.getAction() == Action.LEFT_CLICK_BLOCK)) {
    309. return;
    310. }
    311.  
    312. if (!(b.getType() == Material.COBBLESTONE)) {
    313. return;
    314. }
    315.  
    316. Random r = new Random();
    317. int ri = r.nextInt();
    318. boolean rb = r.nextBoolean();
    319. int count = 0;
    320.  
    321. for (Material m : Material.values()) {
    322. if (rb == false) {
    323. return;
    324. }
    325.  
    326. if (count == 54) {
    327. return;
    328. }
    329.  
    330. ItemStack is = new ItemStack(m, ri);
    331. contents.add(is);
    332. count++;
    333. }
    334.  
    335. for (ItemStack is : contents) {
    336. inv.addItem(is);
    337. }
    338.  
    339. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    340. public void run() {
    341. p.openInventory(inv);
    342. }
    343. }, 1);
    344.  
    345. }
    346.  
    347. public void drop(ArrayList<UUID> id) {
    348. Location loc = getLocation();
    349. Location pLoc = getPlayerLocation(loc);
    350.  
    351. Iterator<UUID> i = q1.iterator();
    352.  
    353. while(i.hasNext()){
    354. UUID uuid = i.next();
    355. players2.add(uuid);
    356. players.remove(uuid);
    357. }
    358.  
    359. // Create falling block
    360. loc.getBlock().setType(Material.COBBLESTONE);
    361. locations.add(loc);
    362.  
    363. for (UUID uuid : players2) {
    364. Player pl = (Player) Bukkit.getPlayer(uuid);
    365. msg(pl, "ok", "Teleporting you to the location! (" + pLoc.getBlockX() + ", " + pLoc.getBlockY() + ", " + pLoc.getBlockZ() + ")");
    366. pl.teleport(loc);
    367. players2.remove(pl);
    368. }
    369. }
    370.  
    371. @EventHandler
    372. public void onClose(InventoryCloseEvent e) {
    373. // Player p = (Player) e.getPlayer();
    374. if (e.getInventory() == inv) {
    375. for (Location l : locations) {
    376. l.getBlock().setType(Material.AIR);
    377. locations.remove(l);
    378. }
    379. } else {
    380. return;
    381. }
    382. }
    383. }
    384.  


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

    xTigerRebornx

    SpongyBacon You are removing an element while iterating over it. It throws a ConcurrentModificationException.
    Either use Iterator#remove() or remove it after iterating.
     
    SpongyBacon likes this.
  7. Offline

    SpongyBacon

    xTigerRebornx Finally fixed it, just popped a bunch of schedulers in to delay list remove by 1 tick (Probably very inefficient, but YOLO). /Solved
     
Thread Status:
Not open for further replies.

Share This Page