From 734351011a406ae0dc1e1ca850b876ab28e18fab Mon Sep 17 00:00:00 2001 From: NewGarbo Date: Sun, 10 Jan 2016 21:15:50 +0000 Subject: [PATCH] made all old clans ban clients get unloaded. there seems to have been problems with certain clients not unloading. --- .../game/clans/clans/ban/ClansBanManager.java | 65 +++++++++++++++---- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ban/ClansBanManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ban/ClansBanManager.java index 1883da837..bf826cce3 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ban/ClansBanManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ban/ClansBanManager.java @@ -1,12 +1,11 @@ package mineplex.game.clans.clans.ban; -import java.net.URL; -import java.net.URLConnection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -15,6 +14,8 @@ import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.java.JavaPlugin; +import com.google.common.collect.Lists; + import mineplex.core.MiniPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.common.util.C; @@ -24,6 +25,8 @@ import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilTime; import mineplex.core.common.util.UtilTime.TimeUnit; import mineplex.core.donation.DonationManager; +import mineplex.core.updater.UpdateType; +import mineplex.core.updater.event.UpdateEvent; import mineplex.game.clans.clans.ban.commands.ClansBanCommand; import mineplex.game.clans.clans.ban.commands.ClansBanListCommand; import mineplex.game.clans.clans.ban.ui.ClansBanListShop; @@ -38,6 +41,8 @@ public class ClansBanManager extends MiniPlugin private ClansBanShop _shop; private ClansBanListShop _shop2; + private Object _lock = new Object(); + public ClansBanManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager) { super("Blacklist", plugin); @@ -60,6 +65,24 @@ public class ClansBanManager extends MiniPlugin addCommand(new ClansBanListCommand(this)); } + @EventHandler + public void clearOldClients(UpdateEvent event) + { + if (!event.getType().equals(UpdateType.MIN_01)) + { + return; + } + + synchronized (_lock) + { + List purge = Lists.newArrayList(); + + _clients.keySet().stream().filter(name -> Bukkit.getPlayer(name) == null).forEach(purge::add); + + purge.forEach(this::UnloadClient); + } + } + public void ban(ClansBanClient client, String name, long time, String reason, Callback callback) { _repository.ban(client.AccountId, time, reason, time == -1); @@ -135,32 +158,46 @@ public class ClansBanManager extends MiniPlugin public void UnloadClient(ClansBanClient client) { - String name = ""; - - for (Entry entry : _clients.entrySet()) + synchronized (_lock) { - if (entry.getValue().equals(client)) + String name = ""; + + for (Entry entry : _clients.entrySet()) { - name = entry.getKey(); - break; + if (entry.getValue().equals(client)) + { + name = entry.getKey(); + break; + } } + + _clients.remove(name); + } + } + + public void UnloadClient(String name) + { + synchronized (_lock) + { + _clients.remove(name); } - - _clients.remove(name); } public void LoadClient(final String name, Callback callback) { GetRepository().loadBans(name, client -> { - _clients.put(name, client); - System.out.println("> CLIENTS: " + _clients); - if (callback != null) callback.run(client); + synchronized (_lock) + { + _clients.put(name, client); + System.out.println("> CLIENTS: " + _clients); + if (callback != null) callback.run(client); + } }); } public ClansBanClient Get(String name) { - synchronized (this) + synchronized (_lock) { return _clients.get(name.toLowerCase()); }