Fix memory leak in StatsManager, CombatManager

This commit is contained in:
samczsun 2016-10-02 17:43:37 -04:00 committed by Shaun Bennett
parent 2b6d7a4d7a
commit 25d6a4239e
2 changed files with 47 additions and 61 deletions

View File

@ -5,6 +5,8 @@ import java.sql.SQLException;
import java.util.Iterator;
import java.util.UUID;
import mineplex.cache.player.PlayerInfo;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
@ -22,13 +24,13 @@ import mineplex.core.stats.event.StatChangeEvent;
public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{
private static Object _statSync = new Object();
private static final Object _statSync = new Object();
private StatsRepository _repository;
private NautHashMap<String, Integer> _stats = new NautHashMap<String, Integer>();
private NautHashMap<Player, NautHashMap<String, Long>> _statUploadQueue = new NautHashMap<Player, NautHashMap<String, Long>>();
private NautHashMap<Player, NautHashMap<String, Long>> _statUploadQueueOverRidable = new NautHashMap<Player, NautHashMap<String, Long>>();
private NautHashMap<UUID, NautHashMap<String, Long>> _statUploadQueue = new NautHashMap<>();
private NautHashMap<UUID, NautHashMap<String, Long>> _statUploadQueueOverRidable = new NautHashMap<>();
private Runnable _saveRunnable;
@ -122,13 +124,13 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{
synchronized (_statSync)
{
if (!_statUploadQueueOverRidable.containsKey(player))
_statUploadQueueOverRidable.put(player, new NautHashMap<String, Long>());
if (!_statUploadQueueOverRidable.containsKey(player.getUniqueId()))
_statUploadQueueOverRidable.put(player.getUniqueId(), new NautHashMap<String, Long>());
if (!_statUploadQueueOverRidable.get(player).containsKey(statName))
_statUploadQueueOverRidable.get(player).put(statName, 0L);
if (!_statUploadQueueOverRidable.get(player.getUniqueId()).containsKey(statName))
_statUploadQueueOverRidable.get(player.getUniqueId()).put(statName, 0L);
_statUploadQueueOverRidable.get(player).put(statName, _statUploadQueueOverRidable.get(player).get(statName) + value);
_statUploadQueueOverRidable.get(player.getUniqueId()).put(statName, _statUploadQueueOverRidable.get(player.getUniqueId()).get(statName) + value);
}
}
@ -143,24 +145,24 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
synchronized (_statSync)
{
for (Iterator<Player> statIterator = _statUploadQueueOverRidable.keySet().iterator(); statIterator.hasNext();)
for (Iterator<UUID> statIterator = _statUploadQueueOverRidable.keySet().iterator(); statIterator.hasNext();)
{
Player player = statIterator.next();
UUID player = statIterator.next();
if (player.isOnline())
if (Bukkit.getPlayer(player) != null)
continue;
try
{
int uploadKey = PlayerCache.getInstance().getPlayer(player.getUniqueId()).getAccountId();
PlayerInfo info = PlayerCache.getInstance().getPlayer(player);
uploadQueue.put(uploadKey, new NautHashMap<Integer, Long>());
uploadQueue.put(info.getAccountId(), new NautHashMap<>());
for (String statName : _statUploadQueueOverRidable.get(player).keySet())
{
int statId = _stats.get(statName);
uploadQueue.get(uploadKey).put(statId, _statUploadQueueOverRidable.get(player).get(statName));
System.out.println(player.getName() + " saving stat : " + statName + " overriding " + _statUploadQueueOverRidable.get(player).get(statName));
uploadQueue.get(info.getAccountId()).put(statId, _statUploadQueueOverRidable.get(player).get(statName));
System.out.println(info.getName() + " saving stat : " + statName + " overriding " + _statUploadQueueOverRidable.get(player).get(statName));
}
statIterator.remove();
@ -184,13 +186,13 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{
synchronized (_statSync)
{
if (!_statUploadQueue.containsKey(player))
_statUploadQueue.put(player, new NautHashMap<String, Long>());
if (!_statUploadQueue.containsKey(player.getUniqueId()))
_statUploadQueue.put(player.getUniqueId(), new NautHashMap<String, Long>());
if (!_statUploadQueue.get(player).containsKey(statName))
_statUploadQueue.get(player).put(statName, 0L);
if (!_statUploadQueue.get(player.getUniqueId()).containsKey(statName))
_statUploadQueue.get(player.getUniqueId()).put(statName, 0L);
_statUploadQueue.get(player).put(statName, _statUploadQueue.get(player).get(statName) + value);
_statUploadQueue.get(player.getUniqueId()).put(statName, _statUploadQueue.get(player.getUniqueId()).get(statName) + value);
}
}
@ -205,24 +207,24 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
synchronized (_statSync)
{
for (Iterator<Player> statIterator = _statUploadQueue.keySet().iterator(); statIterator.hasNext();)
for (Iterator<UUID> statIterator = _statUploadQueue.keySet().iterator(); statIterator.hasNext();)
{
Player player = statIterator.next();
if (player.isOnline())
UUID player = statIterator.next();
if (Bukkit.getPlayer(player) != null)
continue;
try
{
int uploadKey = PlayerCache.getInstance().getPlayer(player.getUniqueId()).getAccountId();
PlayerInfo info = PlayerCache.getInstance().getPlayer(player);
uploadQueue.put(uploadKey, new NautHashMap<Integer, Long>());
uploadQueue.put(info.getAccountId(), new NautHashMap<Integer, Long>());
for (String statName : _statUploadQueue.get(player).keySet())
{
int statId = _stats.get(statName);
uploadQueue.get(uploadKey).put(statId, _statUploadQueue.get(player).get(statName));
System.out.println(player.getName() + " saving stat : " + statName + " adding " + _statUploadQueue.get(player).get(statName));
uploadQueue.get(info.getAccountId()).put(statId, _statUploadQueue.get(player).get(statName));
System.out.println(info.getName() + " saving stat : " + statName + " adding " + _statUploadQueue.get(player).get(statName));
}
statIterator.remove();

View File

@ -6,6 +6,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.entity.Arrow;
@ -46,10 +47,10 @@ public class CombatManager extends MiniPlugin
DefaultWeaponName
}
private NautHashMap<Player, CombatLog> _active = new NautHashMap<Player, CombatLog>();
private NautHashMap<UUID, CombatLog> _active = new NautHashMap<>();
private Map<UUID, ClientCombat> _combatClients = new HashMap<>();
private HashSet<Player> _removeList = new HashSet<Player>();
private HashSet<UUID> _removeList = new HashSet<>();
protected long ExpireTime = 15000;
@ -63,7 +64,7 @@ public class CombatManager extends MiniPlugin
@EventHandler
public void UnloadDonor(ClientUnloadEvent event)
{
_combatClients.remove(event.GetName());
_combatClients.remove(event.getUniqueId());
}
public ClientCombat Get(UUID uuid)
@ -419,10 +420,10 @@ public class CombatManager extends MiniPlugin
{
event.setDeathMessage(null);
if (!_active.containsKey(event.getEntity()))
if (!_active.containsKey(event.getEntity().getUniqueId()))
return;
CombatLog log = _active.remove(event.getEntity());
CombatLog log = _active.remove(event.getEntity().getUniqueId());
log.SetDeathTime(System.currentTimeMillis());
// Save Death
@ -592,24 +593,24 @@ public class CombatManager extends MiniPlugin
public void Add(Player player)
{
_active.put(player, new CombatLog(player, 15000));
_active.put(player.getUniqueId(), new CombatLog(player, 15000));
}
@EventHandler(priority = EventPriority.HIGHEST)
public void Clear(ClearCombatEvent event)
{
_active.remove(event.GetPlayer());
_active.remove(event.GetPlayer().getUniqueId());
}
public CombatLog Get(Player player)
{
if (!_active.containsKey(player))
if (!_active.containsKey(player.getUniqueId()))
{
Add(player);
}
return _active.get(player);
return _active.get(player.getUniqueId());
}
public long GetExpireTime()
@ -623,44 +624,27 @@ public class CombatManager extends MiniPlugin
if (event.getType() == UpdateType.MIN_02)
{
// Remove already marked inactives if still offline
Iterator<Player> removeIterator = _removeList.iterator();
Iterator<UUID> removeIterator = _removeList.iterator();
while (removeIterator.hasNext())
{
Player player = removeIterator.next();
UUID uuid = removeIterator.next();
Player player = Bukkit.getPlayer(uuid);
if (!player.isOnline())
_active.remove(player);
if (player == null)
_active.remove(uuid);
removeIterator.remove();
}
// Mark inactives for cleanup next go around
for (Player player : _active.keySet())
for (UUID player : _active.keySet())
{
if (!player.isOnline())
if (Bukkit.getPlayer(player) == null)
_removeList.add(player);
}
}
}
public void DebugInfo(Player player)
{
StringBuilder nameBuilder = new StringBuilder();
for (Player combats : _active.keySet())
{
if (!combats.isOnline())
{
if (nameBuilder.length() != 0)
nameBuilder.append(", ");
nameBuilder.append(combats.getName());
}
}
player.sendMessage(F.main(getName(), nameBuilder.toString()));
}
public void setUseWeaponName(AttackReason var)
{