From 79b18b7e88c0644a1e5dc8953b0dcd2c43e7ca19 Mon Sep 17 00:00:00 2001 From: Esophose Date: Wed, 17 Oct 2018 02:00:11 -0600 Subject: [PATCH] Fix memory leak --- .../playerparticles/manager/DataManager.java | 28 +++++++++++++++---- .../manager/ParticleManager.java | 2 +- .../playerparticles/particles/PPlayer.java | 11 +++++--- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/com/esophose/playerparticles/manager/DataManager.java b/src/com/esophose/playerparticles/manager/DataManager.java index d627b3b..1964e67 100644 --- a/src/com/esophose/playerparticles/manager/DataManager.java +++ b/src/com/esophose/playerparticles/manager/DataManager.java @@ -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()); 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); + } + }); }); }); } diff --git a/src/com/esophose/playerparticles/manager/ParticleManager.java b/src/com/esophose/playerparticles/manager/ParticleManager.java index f3b13ac..ef257cc 100644 --- a/src/com/esophose/playerparticles/manager/ParticleManager.java +++ b/src/com/esophose/playerparticles/manager/ParticleManager.java @@ -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 } /** diff --git a/src/com/esophose/playerparticles/particles/PPlayer.java b/src/com/esophose/playerparticles/particles/PPlayer.java index 8c8efce..56f323b 100644 --- a/src/com/esophose/playerparticles/particles/PPlayer.java +++ b/src/com/esophose/playerparticles/particles/PPlayer.java @@ -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; + } + } } /**