Skip to content

Commit

Permalink
Performance improvement by caching PPlayer Player reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Esophose committed Feb 14, 2020
1 parent 1c834c7 commit c2146c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ public void onPlayerJoin(PlayerJoinEvent e) {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent e) {
PPlayer pplayer = this.playerParticles.getManager(DataManager.class).getPPlayer(e.getPlayer().getUniqueId());
if (pplayer != null && pplayer.getFixedEffectIds().isEmpty())
this.particlePlayers.remove(pplayer.getUniqueId()); // Unload the PPlayer if they don't have any fixed effects
if (pplayer != null) {
pplayer.clearCachedPlayer();
if (pplayer.getFixedEffectIds().isEmpty())
this.particlePlayers.remove(pplayer.getUniqueId()); // Unload the PPlayer if they don't have any fixed effects
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class PPlayer {
*/
private final UUID playerUUID;

/**
* Cached instance of the player
*/
private Player cachedPlayer;

/**
* A map of ParticleGroups this player has, the active particle group has an id of null
*/
Expand Down Expand Up @@ -74,7 +79,16 @@ public UUID getUniqueId() {
* @return the underlying CommandSender who executed the command
*/
public Player getPlayer() {
return Bukkit.getPlayer(this.playerUUID);
if (this.cachedPlayer == null)
this.cachedPlayer = Bukkit.getPlayer(this.playerUUID);
return this.cachedPlayer;
}

/**
* Clears the cached player to prevent the reference from lingering
*/
public void clearCachedPlayer() {
this.cachedPlayer = null;
}

/**
Expand Down

0 comments on commit c2146c1

Please sign in to comment.