Skip to content

Commit

Permalink
Optimize tnt render by ignoring afk players (#1310)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 authored Apr 23, 2024
1 parent f84e57b commit 4714177
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 15 deletions.
11 changes: 10 additions & 1 deletion core/src/main/java/tc/oc/pgm/PGMPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import tc.oc.pgm.util.chunk.NullChunkGenerator;
import tc.oc.pgm.util.compatability.SportPaperListener;
import tc.oc.pgm.util.concurrent.BukkitExecutorService;
import tc.oc.pgm.util.listener.AfkTracker;
import tc.oc.pgm.util.listener.ItemTransferListener;
import tc.oc.pgm.util.listener.PlayerBlockListener;
import tc.oc.pgm.util.listener.PlayerMoveListener;
Expand All @@ -97,6 +98,7 @@ public class PGMPlugin extends JavaPlugin implements PGM, Listener {
private ScheduledExecutorService executorService;
private ScheduledExecutorService asyncExecutorService;
private InventoryManager inventoryManager;
private AfkTracker afkTracker;

public PGMPlugin() {
super();
Expand Down Expand Up @@ -220,7 +222,7 @@ public void onEnable() {
Integration.setVanishIntegration(new SimpleVanishIntegration(matchManager, executorService));

inventoryManager = new InventoryManager(this);
inventoryManager.init();
afkTracker = new AfkTracker(this);

if (config.showTabList()) {
matchTabManager = new MatchTabManager(this);
Expand Down Expand Up @@ -339,6 +341,11 @@ public InventoryManager getInventoryManager() {
return inventoryManager;
}

@Override
public AfkTracker getAfkTracker() {
return afkTracker;
}

private void registerCommands() {
try {
new PGMCommandGraph(this);
Expand All @@ -363,6 +370,8 @@ private void registerListeners() {
registerEvents(new TNTMinecartPlacementListener());
new BlockTransformListener(this).registerEvents();
registerEvents(matchManager);
inventoryManager.init();
registerEvents(afkTracker);
if (matchTabManager != null) registerEvents(matchTabManager);
registerEvents(nameDecorationRegistry);
registerEvents(new PGMListener(this, matchManager));
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/PGM.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tc.oc.pgm.api.match.MatchManager;
import tc.oc.pgm.namedecorations.NameDecorationRegistry;
import tc.oc.pgm.tablist.MatchTabManager;
import tc.oc.pgm.util.listener.AfkTracker;

/** PvP Game Manager (aka. PGM), the global {@link Plugin} to manage PvP games. */
public interface PGM extends Plugin {
Expand Down Expand Up @@ -40,6 +41,8 @@ public interface PGM extends Plugin {

InventoryManager getInventoryManager();

AfkTracker getAfkTracker();

AtomicReference<PGM> GLOBAL = new AtomicReference<>(null);

static PGM set(PGM pgm) {
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/party/Party.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tc.oc.pgm.api.party;

import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -158,6 +159,20 @@ default Collection<? extends Filterable<? extends PlayerQuery>> getFilterableChi
return this.getPlayers();
}

@Override
@SuppressWarnings("unchecked")
default <R extends Filterable<?>> Collection<? extends R> getFilterableDescendants(
Class<R> type) {
Collection<R> result = new ArrayList<>();
if (type.isAssignableFrom(Party.class)) {
result.add((R) this);
}
if (type.isAssignableFrom(MatchPlayer.class)) {
result.addAll((Collection<? extends R>) getPlayers());
}
return result;
}

@Override
default Party getParty() {
return this;
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/player/MatchPlayer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tc.oc.pgm.api.player;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
import org.bukkit.GameMode;
Expand All @@ -20,6 +22,7 @@
import tc.oc.pgm.util.attribute.Attribute;
import tc.oc.pgm.util.attribute.AttributeInstance;
import tc.oc.pgm.util.bukkit.ViaUtils;
import tc.oc.pgm.util.listener.AfkTracker;
import tc.oc.pgm.util.named.Named;

/**
Expand Down Expand Up @@ -144,6 +147,33 @@ default boolean isLegacy() {
return getProtocolVersion() <= ViaUtils.VERSION_1_7;
}

/**
* Get when the {@link MatchPlayer} was last active in the game
*
* @return the last time player was not afk
*/
default Instant getLastActive() {
return getActivity().getLastActive();
}

/**
* Get whether the {@link MatchPlayer} is actively moving, or afk
*
* @param duration How much time until the player is considered inactive
* @return true if the player moved within {@param duration}, false if the player has been afk
* that long
*/
default boolean isActive(Duration duration) {
return getActivity().isActive(duration);
}

/**
* Get the AFK activity tracker for the {@link MatchPlayer}
*
* @return the afk activity tracker
*/
AfkTracker.Activity getActivity();

/**
* Get whether the {@link MatchPlayer} can interact with things in the {@link Match}.
*
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/tc/oc/pgm/goals/GoalMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -59,11 +60,11 @@ private GoalMatchModule(Match match) {
}

public Collection<Goal> getGoals() {
return goals;
return Collections.unmodifiableCollection(goals);
}

public Collection<Goal> getGoals(Competitor competitor) {
return goalsByCompetitor.get(competitor);
return Collections.unmodifiableCollection(goalsByCompetitor.get(competitor));
}

public Collection<Competitor> getCompetitors(Goal goal) {
Expand Down
13 changes: 5 additions & 8 deletions core/src/main/java/tc/oc/pgm/match/MatchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.World;
Expand Down Expand Up @@ -407,7 +406,7 @@ public void setMaxPlayers(int players) {

@Override
public Collection<MatchPlayer> getPlayers() {
return ImmutableList.copyOf(players.values());
return Collections.unmodifiableCollection(players.values());
}

@Override
Expand Down Expand Up @@ -937,17 +936,15 @@ public Collection<? extends Filterable<? extends MatchQuery>> getFilterableChild
@Override
@SuppressWarnings("unchecked")
public <R extends Filterable<?>> Collection<? extends R> getFilterableDescendants(Class<R> type) {
final Collection<R> result = new LinkedList<>();
Collection<R> result = new ArrayList<>();
if (type.isAssignableFrom(Match.class)) {
result.add((R) this);
}
if (Party.class.isAssignableFrom(type)) {
result.addAll(
(List<R>)
this.getParties().stream().filter(type::isInstance).collect(Collectors.toList()));
if (type.isAssignableFrom(Party.class)) {
result.addAll((Collection<? extends R>) getParties());
}
if (type.isAssignableFrom(MatchPlayer.class)) {
result.addAll((List<R>) this.getPlayers());
result.addAll((Collection<? extends R>) getPlayers());
}
return result;
}
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/java/tc/oc/pgm/match/MatchPlayerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import tc.oc.pgm.util.attribute.AttributeMap;
import tc.oc.pgm.util.attribute.AttributeModifier;
import tc.oc.pgm.util.bukkit.ViaUtils;
import tc.oc.pgm.util.listener.AfkTracker;
import tc.oc.pgm.util.named.NameStyle;
import tc.oc.pgm.util.nms.NMSHacks;

Expand All @@ -79,6 +80,7 @@ public class MatchPlayerImpl implements MatchPlayer, Comparable<MatchPlayer> {
private final AtomicBoolean protocolReady;
private final AtomicInteger protocolVersion;
private final AttributeMap attributeMap;
private final AfkTracker.Activity activity;

public MatchPlayerImpl(Match match, Player player) {
this.logger =
Expand All @@ -95,6 +97,7 @@ public MatchPlayerImpl(Match match, Player player) {
this.protocolReady = new AtomicBoolean(ViaUtils.isReady(player));
this.protocolVersion = new AtomicInteger(ViaUtils.getProtocolVersion(player));
this.attributeMap = NMSHacks.buildAttributeMap(player);
this.activity = PGM.get().getAfkTracker().getActivity(player);
}

@Override
Expand Down Expand Up @@ -190,6 +193,11 @@ public boolean isFrozen() {
return frozen.get();
}

@Override
public AfkTracker.Activity getActivity() {
return activity;
}

@Override
public boolean canInteract() {
return isAlive() && !isFrozen();
Expand Down Expand Up @@ -471,6 +479,15 @@ public Collection<? extends Filterable<? extends PlayerQuery>> getFilterableChil
return Collections.emptyList();
}

@Override
@SuppressWarnings("unchecked")
public <R extends Filterable<?>> Collection<? extends R> getFilterableDescendants(Class<R> type) {
if (type.isAssignableFrom(getClass())) {
return Collections.singleton((R) this);
}
return Collections.emptyList();
}

@Override
public int compareTo(MatchPlayer o) {
final int diff = this.id.compareTo(o.getId());
Expand Down
18 changes: 14 additions & 4 deletions core/src/main/java/tc/oc/pgm/tntrender/TNTRenderMatchModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tc.oc.pgm.tntrender;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand All @@ -10,6 +11,7 @@
import org.bukkit.Material;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
Expand All @@ -18,11 +20,12 @@
import tc.oc.pgm.api.match.MatchScope;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.events.ListenerScope;
import tc.oc.pgm.util.event.block.BlockDispenseEntityEvent;
import tc.oc.pgm.util.nms.NMSHacks;

@ListenerScope(value = MatchScope.LOADED)
public class TNTRenderMatchModule implements MatchModule, Listener {

private static final Duration AFK_TIME = Duration.ofSeconds(30);
private static final double MAX_DISTANCE = Math.pow(64d, 2);

private final Match match;
Expand All @@ -41,13 +44,19 @@ public void enable() {
() -> entities.removeIf(PrimedTnt::update), 0, 50, TimeUnit.MILLISECONDS);
}

@EventHandler
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onTntSpawn(ExplosionPrimeEvent event) {
if (event.getEntity() instanceof TNTPrimed)
entities.add(new PrimedTnt((TNTPrimed) event.getEntity()));
}

@EventHandler
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDispense(BlockDispenseEntityEvent event) {
if (event.getEntity() instanceof TNTPrimed)
entities.add(new PrimedTnt((TNTPrimed) event.getEntity()));
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onTntExplode(EntityExplodeEvent event) {
if (!(event.getEntity() instanceof TNTPrimed)) return;
Location explosion = event.getLocation();
Expand Down Expand Up @@ -90,7 +99,8 @@ public boolean update() {
}

private void updatePlayer(MatchPlayer player) {
if (currentLocation.distanceSquared(player.getBukkit().getLocation()) >= MAX_DISTANCE) {
if (currentLocation.distanceSquared(player.getLocation()) >= MAX_DISTANCE
&& player.isActive(AFK_TIME)) {
if (viewers.add(player)) {
NMSHacks.sendBlockChange(currentLocation, player.getBukkit(), Material.TNT);
} else if (moved) {
Expand Down
Loading

0 comments on commit 4714177

Please sign in to comment.