Skip to content

Commit

Permalink
Update Sponge impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Nov 29, 2024
1 parent e80cb4e commit baa72e3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion sponge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ java {

sponge {
injectRepositories(false)
apiVersion("12.0.0-SNAPSHOT")
apiVersion("13.0.0-SNAPSHOT")
plugin(rootProject.name.lowercase()) {
loader {
name(PluginLoaders.JAVA_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.math.RoundingMode;
import java.util.function.BooleanSupplier;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.profiling.ProfilerFiller;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Implements;
Expand All @@ -51,34 +52,61 @@
@Mixin(MinecraftServer.class)
@Implements({@Interface(iface = TickTimeService.class, prefix = "tabtps$")})
abstract class MinecraftServerMixin implements MinecraftServerAccess {
@Unique
private final TickTimes tickTimes5s = new TickTimes(100);
@Unique
private final TickTimes tickTimes10s = new TickTimes(200);
@Unique
private final TickTimes tickTimes60s = new TickTimes(1200);

private final RollingAverage tps5s = new RollingAverage(5);
@Unique
private final RollingAverage tps15s = new RollingAverage(15);
@Unique
private final RollingAverage tps1m = new RollingAverage(60);
@Unique
private final RollingAverage tps5m = new RollingAverage(60 * 5);
@Unique
private final RollingAverage tps15m = new RollingAverage(60 * 15);

@Unique
private long previousTime;
@Unique
private boolean tickingPaused;

@Shadow private int tickCount;
@Shadow @Final private long[] tickTimesNanos;

@Inject(method = "tickServer", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD)
public void injectTick(final BooleanSupplier var1, final CallbackInfo ci, final long tickStartTimeNanos, final long tickDurationNanos) {
@Inject(
method = "tickServer",
at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;info(Ljava/lang/String;Ljava/lang/Object;)V", ordinal = 0)
)
private void injectPause(final BooleanSupplier keepTicking, final CallbackInfo ci) {
this.tickingPaused = true;
}

@Inject(method = "tickServer", at = @At(value = "RETURN", ordinal = 1), locals = LocalCapture.CAPTURE_FAILHARD)
public void injectTick(
final BooleanSupplier keepTicking,
final CallbackInfo ci,
final long tickStartTimeNanos,
final ProfilerFiller profilerFiller,
final long tickDurationNanos
) {
this.tickTimes5s.add(this.tickCount, tickDurationNanos);
this.tickTimes10s.add(this.tickCount, tickDurationNanos);
this.tickTimes60s.add(this.tickCount, tickDurationNanos);

if (this.tickCount % RollingAverage.SAMPLE_INTERVAL == 0) {
if (this.previousTime == 0) {
this.previousTime = tickStartTimeNanos - RollingAverage.TICK_TIME;
if (this.previousTime == 0 || this.tickingPaused) {
this.previousTime = tickStartTimeNanos - tickDurationNanos;
if (this.tickingPaused) {
this.tickingPaused = false;
}
}
final long diff = tickStartTimeNanos - this.previousTime;
this.previousTime = tickStartTimeNanos;
final BigDecimal currentTps = RollingAverage.TPS_BASE.divide(new BigDecimal(diff), 30, RoundingMode.HALF_UP);
this.tps5s.add(currentTps, diff);
this.tps15s.add(currentTps, diff);
this.tps1m.add(currentTps, diff);
this.tps5m.add(currentTps, diff);
this.tps15m.add(currentTps, diff);
Expand All @@ -91,7 +119,7 @@ public void injectTick(final BooleanSupplier var1, final CallbackInfo ci, final

public double @NonNull [] tabtps$recentTps() {
final double[] tps = new double[4];
tps[0] = this.tps5s.average();
tps[0] = this.tps15s.average();
tps[1] = this.tps1m.average();
tps[2] = this.tps5m.average();
tps[3] = this.tps15m.average();
Expand Down

0 comments on commit baa72e3

Please sign in to comment.