made all old clans ban clients get unloaded. there seems to have been problems with certain clients not unloading.

This commit is contained in:
NewGarbo 2016-01-10 21:15:50 +00:00
parent 9c3a62a308
commit 734351011a

View File

@ -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<String> 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<ClansBanClient> 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<String, ClansBanClient> entry : _clients.entrySet())
synchronized (_lock)
{
if (entry.getValue().equals(client))
String name = "";
for (Entry<String, ClansBanClient> 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<ClansBanClient> 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());
}