Optimization and memory leak fix

This commit is contained in:
filoghost 2022-01-31 23:41:28 +01:00
parent 2789a24295
commit b89e6264bb
11 changed files with 51 additions and 36 deletions

View File

@ -129,16 +129,16 @@ public class HolographicDisplays extends FCommonsPlugin {
internalHologramEditor = new InternalHologramEditor(internalHologramManager, configManager);
new HologramCommandManager(this, internalHologramEditor).register(this);
// Listeners
registerListener(new PlayerListener(nmsManager, lineTrackerManager, lineClickListener));
registerListener(new ChunkListener(this, internalHologramManager, apiHologramManager, v2HologramManager));
UpdateNotificationListener updateNotificationListener = new UpdateNotificationListener();
registerListener(updateNotificationListener);
// Tasks
TickingTask tickingTask = new TickingTask(tickClock, placeholderTracker, lineTrackerManager, lineClickListener);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, tickingTask, 0, 1);
updateNotificationListener.runAsyncUpdateCheck(this);
// Listeners
PlayerListener playerListener = new PlayerListener(nmsManager, lineClickListener, tickingTask);
registerListener(playerListener);
registerListener(new ChunkListener(this, internalHologramManager, apiHologramManager, v2HologramManager));
UpdateNotificationListener updateNotificationListener = new UpdateNotificationListener();
registerListener(updateNotificationListener);
// Enable the APIs
HolographicDisplaysAPIProvider.setImplementation(
@ -157,6 +157,9 @@ public class HolographicDisplays extends FCommonsPlugin {
errorCollector.logToConsole();
Bukkit.getScheduler().runTaskLater(this, errorCollector::logSummaryToConsole, 10L);
}
// Run the update checker
updateNotificationListener.runAsyncUpdateCheck(this);
}
@SuppressWarnings("deprecation")

View File

@ -9,6 +9,7 @@ import me.filoghost.holographicdisplays.nms.common.NMSManager;
import me.filoghost.holographicdisplays.nms.common.entity.ItemNMSPacketEntity;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseItemHologramLine;
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.MustBeInvokedByOverriders;

View File

@ -6,6 +6,7 @@
package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLine;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.MustBeInvokedByOverriders;

View File

@ -10,6 +10,7 @@ import me.filoghost.holographicdisplays.plugin.hologram.base.BaseItemHologramLin
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextHologramLine;
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import org.bukkit.entity.Player;
import java.util.Collection;

View File

@ -6,6 +6,7 @@
package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.common.PositionCoordinates;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import org.bukkit.Location;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
@ -35,8 +36,8 @@ abstract class PositionBasedLineTracker<T extends Viewer> extends LineTracker<T>
}
@Override
protected final boolean shouldTrackPlayer(CachedPlayer cachedPlayer) {
Location playerLocation = cachedPlayer.getLocation();
protected final boolean shouldTrackPlayer(CachedPlayer player) {
Location playerLocation = player.getLocation();
if (playerLocation.getWorld() != getLine().getWorldIfLoaded()) {
return false;
}
@ -46,7 +47,7 @@ abstract class PositionBasedLineTracker<T extends Viewer> extends LineTracker<T>
return diffX <= (double) ENTITY_VIEW_RANGE
&& diffZ <= (double) ENTITY_VIEW_RANGE
&& getLine().isVisibleTo(cachedPlayer.getBukkitPlayer());
&& getLine().isVisibleTo(player.getBukkitPlayer());
}
@MustBeInvokedByOverriders

View File

@ -11,6 +11,7 @@ import me.filoghost.holographicdisplays.nms.common.entity.TextNMSPacketEntity;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextHologramLine;
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import java.util.Objects;

View File

@ -6,6 +6,7 @@
package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.nms.common.IndividualTextPacketGroup;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;

View File

@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.nms.common.IndividualTextPacketGroup;
import me.filoghost.holographicdisplays.nms.common.PacketGroup;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import org.bukkit.Location;
import org.bukkit.entity.Player;

View File

@ -6,7 +6,7 @@
package me.filoghost.holographicdisplays.plugin.listener;
import me.filoghost.holographicdisplays.nms.common.NMSManager;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager;
import me.filoghost.holographicdisplays.plugin.tick.TickingTask;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -16,25 +16,27 @@ import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerListener implements Listener {
private final NMSManager nmsManager;
private final LineTrackerManager lineTrackerManager;
private final LineClickListener lineClickListener;
private final TickingTask tickingTask;
public PlayerListener(NMSManager nmsManager, LineTrackerManager lineTrackerManager, LineClickListener lineClickListener) {
public PlayerListener(NMSManager nmsManager, LineClickListener lineClickListener, TickingTask tickingTask) {
this.nmsManager = nmsManager;
this.lineTrackerManager = lineTrackerManager;
this.lineClickListener = lineClickListener;
this.tickingTask = tickingTask;
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
tickingTask.onPlayerJoin(player);
nmsManager.injectPacketListener(player, lineClickListener);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
lineTrackerManager.onPlayerQuit(player);
tickingTask.onPlayerQuit(player);
nmsManager.uninjectPacketListener(player);
}

View File

@ -3,9 +3,8 @@
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.plugin.hologram.tracking;
package me.filoghost.holographicdisplays.plugin.tick;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -28,6 +27,7 @@ public class CachedPlayer {
}
public Location getLocation() {
// Avoid creating a new object on each invocation
long currentTick = tickClock.getCurrentTick();
if (locationUpdateTick != currentTick) {
location = player.getLocation();

View File

@ -6,16 +6,14 @@
package me.filoghost.holographicdisplays.plugin.tick;
import me.filoghost.fcommons.logging.Log;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.CachedPlayer;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager;
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Collection;
import java.util.WeakHashMap;
import java.util.Iterator;
import java.util.List;
public class TickingTask implements Runnable {
@ -23,7 +21,7 @@ public class TickingTask implements Runnable {
private final ActivePlaceholderTracker placeholderTracker;
private final LineTrackerManager lineTrackerManager;
private final LineClickListener lineClickListener;
private final WeakHashMap<Player, CachedPlayer> cachedPlayersMap;
private final List<CachedPlayer> onlinePlayers;
private long lastErrorLogTick;
@ -36,7 +34,24 @@ public class TickingTask implements Runnable {
this.placeholderTracker = placeholderTracker;
this.lineTrackerManager = lineTrackerManager;
this.lineClickListener = lineClickListener;
this.cachedPlayersMap = new WeakHashMap<>();
this.onlinePlayers = new ArrayList<>();
}
public void onPlayerJoin(Player player) {
onlinePlayers.add(new CachedPlayer(player, tickClock));
}
public void onPlayerQuit(Player player) {
Iterator<CachedPlayer> iterator = onlinePlayers.iterator();
while (iterator.hasNext()) {
if (iterator.next().getBukkitPlayer() == player) {
iterator.remove();
break;
}
}
lineTrackerManager.onPlayerQuit(player);
}
@Override
@ -47,7 +62,7 @@ public class TickingTask implements Runnable {
placeholderTracker.clearOutdatedEntries();
try {
lineTrackerManager.update(getOnlineCachedPlayers());
lineTrackerManager.update(onlinePlayers);
} catch (Throwable t) {
// Catch all types of Throwable because we're using NMS code
if (tickClock.getCurrentTick() - lastErrorLogTick >= 20) {
@ -63,16 +78,4 @@ public class TickingTask implements Runnable {
lineClickListener.processQueuedClickEvents();
}
private Collection<CachedPlayer> getOnlineCachedPlayers() {
Collection<? extends Player> bukkitPlayers = Bukkit.getOnlinePlayers();
Collection<CachedPlayer> cachedPlayers = new ArrayList<>(bukkitPlayers.size());
for (Player bukkitPlayer : bukkitPlayers) {
CachedPlayer cachedPlayer = cachedPlayersMap.computeIfAbsent(bukkitPlayer, key -> new CachedPlayer(key, tickClock));
cachedPlayers.add(cachedPlayer);
}
return cachedPlayers;
}
}