Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle tablist based on tps & match cycle #934

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions core/src/main/java/tc/oc/pgm/tablist/MatchTabManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tc.oc.pgm.api.event.NameDecorationChangeEvent;
import tc.oc.pgm.api.map.Contributor;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.match.event.MatchLoadEvent;
import tc.oc.pgm.api.match.event.MatchResizeEvent;
import tc.oc.pgm.api.match.event.MatchUnloadEvent;
import tc.oc.pgm.api.party.event.PartyRenameEvent;
Expand All @@ -39,8 +40,14 @@

public class MatchTabManager extends TabManager implements Listener {

// Min and max delay to trigger an update after tablist is invalidated
private static final int MIN_DELAY = 100, MAX_DELAY = 2000, TIME_RATIO = 40;
// Min and max delay for an update after tab-list is invalidated
private static final int MIN_DELAY = 100, MAX_DELAY = 5000;
// How many MS must be waited for each MS spent rendering
private static final int TIME_RATIO = 40;
// How many MS must be waited for each TPS under 20 (last minute average)
private static final int TPS_RATIO = 1000;
// On match load, throttle tab-list until another match unloads, or this many MS pass.
private static final int MATCH_LOAD_TIMEOUT = 15_000;

private final Map<Team, TeamTabEntry> teamEntries;
private final Map<Match, MapTabEntry> mapEntries;
Expand All @@ -50,7 +57,7 @@ public class MatchTabManager extends TabManager implements Listener {

private Future<?> pingUpdateTask;
private Future<?> renderTask;
private long lastUpdate = 0, renderTime = 0;
private long lastUpdate = 0, renderTime = 0, loadingUntil = 0;

public MatchTabManager(Plugin plugin) {
this(
Expand Down Expand Up @@ -144,7 +151,13 @@ protected void invalidate() {
renderTime = lastUpdate - start;
};

long nextUpdate = lastUpdate - System.currentTimeMillis() + (renderTime * TIME_RATIO);
long now = System.currentTimeMillis();

long nextUpdate =
(lastUpdate - now)
+ (renderTime * TIME_RATIO)
+ (long) Math.max(0, (20 - Bukkit.getServer().spigot().getTPS()[0]) * TPS_RATIO)
+ (loadingUntil > now ? MAX_DELAY : 0);
nextUpdate = Math.min(Math.max(MIN_DELAY, nextUpdate), MAX_DELAY);
this.renderTask = PGM.get().getExecutor().schedule(render, nextUpdate, TimeUnit.MILLISECONDS);
}
Expand Down Expand Up @@ -203,7 +216,7 @@ protected void invalidate(MatchPlayer player) {
public void onJoin(PlayerJoinEvent event) {
if (ViaUtils.isReady(event.getPlayer())) tryEnable(event.getPlayer());
else {
// Player connection hasn't been setup yet, try next tick
// Player connection hasn't been set up yet, try next tick
PGM.get()
.getExecutor()
.schedule(() -> tryEnable(event.getPlayer()), 50, TimeUnit.MILLISECONDS);
Expand All @@ -223,8 +236,14 @@ private void tryEnable(Player player) {
if (view != null) view.enable(this);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMatchLoad(MatchLoadEvent event) {
loadingUntil = System.currentTimeMillis() + MATCH_LOAD_TIMEOUT;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMatchUnload(MatchUnloadEvent event) {
loadingUntil = 0;
TeamMatchModule tmm = event.getMatch().getModule(TeamMatchModule.class);
if (tmm != null) {
for (Team team : tmm.getTeams()) {
Expand Down
12 changes: 6 additions & 6 deletions util/src/main/java/tc/oc/pgm/util/bukkit/ViaUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tc.oc.pgm.util.bukkit;

import com.viaversion.viaversion.api.Via;
import org.bukkit.entity.Player;
import us.myles.ViaVersion.api.Via;

public class ViaUtils {
/** Minecraft 1.7.6 &ndash; 1.7.10 */
Expand All @@ -11,15 +11,15 @@ public class ViaUtils {
/** Minecraft 1.9 &ndash; 1.9.1-pre1 */
public static final int VERSION_1_9 = 107;

private static final boolean ENABLED;
private static final boolean ENABLED = isViaLoaded();

static {
boolean viaLoaded = false;
private static boolean isViaLoaded() {
try {
viaLoaded = Class.forName("us.myles.ViaVersion.api.Via") != null;
Class.forName("com.viaversion.viaversion.api.Via");
return true;
} catch (ClassNotFoundException ignored) {
return false;
}
ENABLED = viaLoaded;
}

public static boolean enabled() {
Expand Down
2 changes: 1 addition & 1 deletion util/src/main/java/tc/oc/pgm/util/tablist/TabView.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void enable(TabManager manager) {
if (this.manager != null) disable();
this.manager = manager;

if (ViaUtils.getProtocolVersion(viewer) < ViaUtils.VERSION_1_8)
if (ViaUtils.getProtocolVersion(viewer) <= ViaUtils.VERSION_1_7)
this.display = new TabDisplay(viewer, WIDTH);

this.setup();
Expand Down