Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit 97a38ca

Browse files
committed
Improve CPU usage fallback and entity task scheduling
1 parent 45335ea commit 97a38ca

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

src/main/java/net/opmasterleo/combat/Combat.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ public class Combat extends JavaPlugin implements Listener {
117117
private final java.lang.management.OperatingSystemMXBean osBean = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
118118
private static final long CPU_CHECK_INTERVAL = 5000;
119119
private static final long ADJUSTMENT_INTERVAL = 10000;
120+
private volatile boolean cpuLoadAvailable = true;
121+
private static final long FALLBACK_CPU_CHECK_INTERVAL = 15000;
122+
private static final long FALLBACK_ADJUSTMENT_INTERVAL = 30000;
120123
private static final double HIGH_CPU_THRESHOLD = 0.75;
121124
private static final double LOW_CPU_THRESHOLD = 0.30;
122125

@@ -595,32 +598,38 @@ public void forceCombatCleanup(UUID playerUUID) {
595598

596599
private double getCpuUsage() {
597600
long now = System.currentTimeMillis();
598-
if (now - lastCpuCheck < CPU_CHECK_INTERVAL) {
601+
long effectiveInterval = cpuLoadAvailable ? CPU_CHECK_INTERVAL : FALLBACK_CPU_CHECK_INTERVAL;
602+
if (now - lastCpuCheck < effectiveInterval) {
599603
return currentCpuUsage;
600604
}
601-
605+
602606
lastCpuCheck = now;
603607
try {
604-
if (osBean instanceof com.sun.management.OperatingSystemMXBean sunBean) {
608+
if (cpuLoadAvailable && osBean instanceof com.sun.management.OperatingSystemMXBean sunBean) {
605609
double usage = sunBean.getProcessCpuLoad();
606610
if (usage >= 0.0) {
607611
currentCpuUsage = usage;
608612
return usage;
609613
}
614+
615+
cpuLoadAvailable = false;
610616
}
611617

612-
int currentActive = combatWorkerPool.getActiveCount();
618+
int currentActive = (combatWorkerPool != null) ? combatWorkerPool.getActiveCount() : 0;
613619
int availableProcessors = Runtime.getRuntime().availableProcessors();
614-
currentCpuUsage = Math.min(1.0, (double) currentActive / availableProcessors);
620+
currentCpuUsage = Math.min(1.0, (double) currentActive / Math.max(1, availableProcessors));
615621
return currentCpuUsage;
616-
} catch (Exception e) {
617-
return Math.min(0.5, (double) combatWorkerPool.getActiveCount() / maxWorkerPoolSize);
622+
} catch (Throwable t) {
623+
cpuLoadAvailable = false;
624+
int active = (combatWorkerPool != null) ? combatWorkerPool.getActiveCount() : 0;
625+
return Math.min(0.5, (double) active / Math.max(1, maxWorkerPoolSize));
618626
}
619627
}
620628

621629
private void adjustThreadPoolDynamically() {
622630
long now = System.currentTimeMillis();
623-
if (now - lastAdjustment < ADJUSTMENT_INTERVAL) {
631+
long effectiveAdjustment = cpuLoadAvailable ? ADJUSTMENT_INTERVAL : FALLBACK_ADJUSTMENT_INTERVAL;
632+
if (now - lastAdjustment < effectiveAdjustment) {
624633
return;
625634
}
626635

src/main/java/net/opmasterleo/combat/util/SchedulerUtil.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ private static Runnable wrapTask(Runnable task, Plugin plugin, String context) {
212212
return () -> {
213213
try {
214214
task.run();
215-
} catch (Exception e) {
216-
if (plugin.getLogger().isLoggable(java.util.logging.Level.SEVERE)) {
217-
plugin.getLogger().log(java.util.logging.Level.SEVERE, "Error in {0} task", new Object[]{context, e});
215+
} catch (Throwable t) {
216+
java.util.logging.Logger logger = plugin.getLogger();
217+
if (logger.isLoggable(java.util.logging.Level.SEVERE)) {
218+
logger.log(java.util.logging.Level.SEVERE, "Error in " + context + " task", t);
218219
}
219220
}
220221
};
@@ -469,16 +470,20 @@ public static void runEntityTask(Plugin plugin, Entity entity, Runnable task) {
469470
}
470471

471472
public static void runEntityTaskLater(Plugin plugin, Entity entity, Runnable task, long delay) {
472-
if (shouldSkip(plugin)) return;
473+
if (shouldSkip(plugin) || entity == null || !entity.isValid()) return;
473474

474475
Runnable wrapped = wrapTask(task, plugin, "entity");
475476

476477
if (IS_FOLIA || IS_CANVAS) {
477-
entity.getScheduler().runDelayed(plugin, t -> wrapped.run(), null, delay);
478+
try {
479+
entity.getScheduler().runDelayed(plugin, t -> wrapped.run(), null, delay);
480+
} catch (Throwable ignored) {
481+
runTaskLater(plugin, wrapped, delay);
482+
}
478483
} else if (PAPER_ENTITY_SCHEDULER) {
479484
try {
480485
entity.getScheduler().runDelayed(plugin, t -> wrapped.run(), null, delay);
481-
} catch (Exception e) {
486+
} catch (Throwable ignored) {
482487
runTaskLater(plugin, wrapped, delay);
483488
}
484489
} else if (IS_ARCLIGHT) {
@@ -493,7 +498,7 @@ public static void runEntityTaskTimer(Plugin plugin, Entity entity, Runnable tas
493498
}
494499

495500
public static void runEntityTaskTimer(Plugin plugin, Entity entity, Runnable task, long delay, long period, long minInterval) {
496-
if (shouldSkip(plugin)) return;
501+
if (shouldSkip(plugin) || entity == null || !entity.isValid()) return;
497502

498503
if (minInterval > 0 && shouldThrottleEntity(entity, minInterval)) {
499504
return;
@@ -503,11 +508,15 @@ public static void runEntityTaskTimer(Plugin plugin, Entity entity, Runnable tas
503508
Runnable wrapped = wrapTask(task, plugin, "entity");
504509

505510
if (IS_FOLIA || IS_CANVAS) {
506-
entity.getScheduler().runAtFixedRate(plugin, t -> wrapped.run(), null, delay, period);
511+
try {
512+
entity.getScheduler().runAtFixedRate(plugin, t -> wrapped.run(), null, delay, period);
513+
} catch (Throwable ignored) {
514+
runTaskTimer(plugin, wrapped, delay, period);
515+
}
507516
} else if (PAPER_ENTITY_SCHEDULER) {
508517
try {
509518
entity.getScheduler().runAtFixedRate(plugin, t -> wrapped.run(), null, delay, period);
510-
} catch (Exception e) {
519+
} catch (Throwable ignored) {
511520
runTaskTimer(plugin, wrapped, delay, period);
512521
}
513522
} else if (IS_ARCLIGHT) {

src/main/java/net/opmasterleo/combat/util/WorldGuardUtil.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,15 @@ public static void disablePacketEventsIntegration() {
150150
public void onPacketReceive(PacketReceiveEvent event) {
151151
try {
152152
if (!plugin.isEnabled() || !plugin.isPluginEnabled()) return;
153-
154153
if (!packetEventsEnabled || !barrierEnabled) return;
155154
var type = event.getPacketType();
156155

157156
if (type == Client.PLAYER_POSITION || type == Client.PLAYER_POSITION_AND_ROTATION) {
158157
Player player = (Player) event.getPlayer();
159158
if (!player.isOnline()) return;
159+
if (!plugin.isInCombat(player)) return;
160160
if (shouldBypass(player)) return;
161+
if (!plugin.isCombatEnabledInWorld(player)) return;
161162
Vector3d newPos;
162163
if (type == Client.PLAYER_POSITION) {
163164
WrapperPlayClientPlayerPosition wrapper = new WrapperPlayClientPlayerPosition(event);
@@ -364,22 +365,23 @@ private void resetBlockChange(Player player, Location loc) {
364365
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
365366
public void onPlayerMove(PlayerMoveEvent event) {
366367
if (!barrierEnabled) return;
367-
368368
Player player = event.getPlayer();
369369
if (!player.isOnline()) return;
370370
if (shouldBypass(player)) return;
371+
boolean inCombat = plugin.isInCombat(player);
372+
if (!inCombat) return;
371373

372374
Location from = event.getFrom();
373375
Location to = event.getTo();
374376
if (from.getBlockX() == to.getBlockX() && from.getBlockZ() == to.getBlockZ()) {
375377
return;
376378
}
377379

378-
if (plugin.isInCombat(player) && isNearSafezone(to)) {
380+
if (isNearSafezone(to)) {
379381
createVisualBarrier(player, to);
380382
}
381383

382-
if (plugin.isInCombat(player) && !shouldBypass(player)) {
384+
if (!shouldBypass(player)) {
383385
boolean fromSafe = isPvpDenied(from);
384386
boolean toSafe = isPvpDenied(to);
385387
if (!fromSafe && toSafe) {

0 commit comments

Comments
 (0)