Fix memory leak

This commit is contained in:
Esophose 2018-10-17 02:00:11 -06:00
parent 88dcf75ace
commit 79b18b7e88
3 changed files with 30 additions and 11 deletions

View file

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.scheduler.BukkitRunnable;
@ -58,7 +59,7 @@ public class DataManager {
*/
public static PPlayer getPPlayer(UUID playerUUID) {
for (PPlayer pp : ParticleManager.particlePlayers)
if (pp.getUniqueId() == playerUUID) return pp;
if (pp.getUniqueId().equals(playerUUID)) return pp;
return null;
}
@ -152,17 +153,32 @@ public class DataManager {
fixedParticles.add(new FixedParticleEffect(playerUUID, fixedEffectId, worldName, xPos, yPos, zPos, particle));
}
}
// If there aren't any groups then this is a brand new PPlayer and we need to save a new active group for them
boolean activeGroupExists = false;
for (ParticleGroup group : groups) {
if (group.getName().equals(ParticleGroup.DEFAULT_NAME)) {
activeGroupExists = true;
break;
}
}
if (groups.size() == 0) { // If there aren't any groups then this is a brand new PPlayer and we need to save a new active group for them
if (!activeGroupExists) {
ParticleGroup activeGroup = new ParticleGroup(ParticleGroup.DEFAULT_NAME, new ArrayList<ParticlePair>());
saveParticleGroup(playerUUID, activeGroup);
groups.add(activeGroup);
}
PPlayer loadedPPlayer = new PPlayer(playerUUID, groups, fixedParticles);
ParticleManager.particlePlayers.add(loadedPPlayer);
sync(() -> callback.execute(loadedPPlayer));
final PPlayer loadedPPlayer = new PPlayer(playerUUID, groups, fixedParticles);
sync(() -> {
Bukkit.broadcastMessage("About to add PPlayer");
if (getPPlayer(playerUUID) == null) { // Make sure the PPlayer still isn't added, since this is async it's possible it got ran twice
ParticleManager.particlePlayers.add(loadedPPlayer);
Bukkit.broadcastMessage("Added PPlayer: " + ParticleManager.particlePlayers.size());
callback.execute(loadedPPlayer);
}
});
});
});
}

View file

@ -57,7 +57,7 @@ public class ParticleManager extends BukkitRunnable implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent e) {
PPlayer pplayer = DataManager.getPPlayer(e.getPlayer().getUniqueId());
if (pplayer != null && pplayer.getFixedEffectIds().isEmpty()) particlePlayers.remove(pplayer);
if (pplayer != null && pplayer.getFixedEffectIds().isEmpty()) particlePlayers.remove(pplayer); // Unload the PPlayer if they don't have any fixed effects
}
/**

View file

@ -98,7 +98,7 @@ public class PPlayer {
for (ParticleGroup group : this.particleGroups)
if (group.getName().equals(ParticleGroup.DEFAULT_NAME))
return group;
return null; // This should never return null, there will always be at least one ParticleGroup
throw new IllegalStateException("Active particle group does not exist for player with UUID: " + this.getUniqueId());
}
/**
@ -177,9 +177,12 @@ public class PPlayer {
* @param id The id of the fixed effect to remove
*/
public void removeFixedEffect(int id) {
for (int i = this.fixedParticles.size() - 1; i >= 0; i--)
if (this.fixedParticles.get(i).getId() == id)
this.fixedParticles.remove(i);
for (FixedParticleEffect fixedEffect : this.fixedParticles) {
if (fixedEffect.getId() == id) {
this.fixedParticles.remove(fixedEffect);
break;
}
}
}
/**