Skip to content

Commit

Permalink
Fixed a ConcurrentModificationException
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan923 committed Sep 17, 2020
1 parent 2489020 commit c3c785a
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions src/main/java/me/stefan923/superlms/game/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.scheduler.BukkitScheduler;

import java.io.IOException;
import java.util.Iterator;
import java.util.UUID;

public class GameManager implements MessageUtils, SerializationUtils, PlayerUtils {
Expand Down Expand Up @@ -170,12 +171,7 @@ public void endGame() {
.replace("%time%", convertTime(System.currentTimeMillis() - startTime, language))));

removePlayer(winner);
Bukkit.getScheduler().runTask(instance, () -> {
for (Player spectator : instance.getSpectators()) {
removeSpectator(spectator);
}
});

removeAllSpectators();

ConsoleCommandSender consoleCommandSender = Bukkit.getConsoleSender();

Expand All @@ -187,14 +183,8 @@ public void endGame() {
public void forceEndGame() {
status = GameStatus.IDLE;

Bukkit.getScheduler().runTask(instance, () -> {
for (Player player : instance.getPlayers()) {
removePlayer(player);
}
for (Player spectator : instance.getSpectators()) {
removeSpectator(spectator);
}
});
removeAllPlayers();
removeAllSpectators();

cancelCurrentTask();
}
Expand Down Expand Up @@ -292,6 +282,41 @@ public void removePlayer(Player player) {
}
}

public void removeAllPlayers() {
for (Iterator<Player> iterator = instance.getPlayers().iterator(); iterator.hasNext(); ) {
Player player = iterator.next();

UUID playerUUID = player.getUniqueId();
InventoryManager inventoryManager = instance.getInventoryManager();
FileConfiguration inventoryConfig = inventoryManager.getConfig();

PlayerInventory playerInventory = player.getInventory();
playerInventory.clear();
playerInventory.setHelmet(null);
playerInventory.setChestplate(null);
playerInventory.setLeggings(null);
playerInventory.setBoots(null);

try {
player.getInventory().setContents(itemStackArrayFromBase64(inventoryConfig.getString(playerUUID + ".inventory")));
player.getInventory().setArmorContents(itemStackArrayFromBase64(inventoryConfig.getString(playerUUID + ".armor")));
} catch (IOException e) {
e.printStackTrace();
}
player.updateInventory();
setTotalExperience(player, inventoryConfig.getInt(playerUUID + ".experience"));

player.teleport(deserializeLocation(settings.getString("Game.Locations.Spawn")));
inventoryConfig.set(playerUUID + ".inventory", null);
inventoryConfig.set(playerUUID + ".armor", null);
inventoryConfig.set(playerUUID + ".experience", null);
inventoryConfig.set(String.valueOf(playerUUID), null);
inventoryManager.save();

iterator.remove();
}
}

public void addSpectator(Player player) {
instance.getSpectators().add(player);

Expand All @@ -312,6 +337,16 @@ public void removeSpectator(Player player) {
Bukkit.getOnlinePlayers().forEach(targetPlayer -> targetPlayer.showPlayer(player));
}

public void removeAllSpectators() {
for (Iterator<Player> iterator = instance.getSpectators().iterator(); iterator.hasNext(); ) {
Player spectator = iterator.next();
spectator.teleport(deserializeLocation(settings.getString("Game.Locations.Spawn")));
Bukkit.getOnlinePlayers().forEach(targetPlayer -> targetPlayer.showPlayer(spectator));

iterator.remove();
}
}

public void broadcastInGame(String message) {
for (Player player : instance.getPlayers()) {
player.sendMessage(message);
Expand Down

0 comments on commit c3c785a

Please sign in to comment.