SPIGOT-7540, #1312: Add ServerTickManager API

By: Miles Holder <mwholder2005@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2023-12-17 09:30:00 +11:00
parent 3e5032caac
commit 46c67d7108
3 changed files with 154 additions and 0 deletions

View file

@ -0,0 +1,53 @@
--- a/net/minecraft/server/ServerTickRateManager.java
+++ b/net/minecraft/server/ServerTickRateManager.java
@@ -59,8 +59,14 @@
}
public boolean stopSprinting() {
+ // CraftBukkit start, add sendLog parameter
+ return stopSprinting(true);
+ }
+
+ public boolean stopSprinting(boolean sendLog) {
+ // CraftBukkit end
if (this.remainingSprintTicks > 0L) {
- this.finishTickSprint();
+ this.finishTickSprint(sendLog); // CraftBukkit - add sendLog parameter
return true;
} else {
return false;
@@ -78,7 +84,7 @@
return flag;
}
- private void finishTickSprint() {
+ private void finishTickSprint(boolean sendLog) { // CraftBukkit - add sendLog parameter
long i = this.scheduledCurrentSprintTicks - this.remainingSprintTicks;
double d0 = Math.max(1.0D, (double) this.sprintTimeSpend) / (double) TimeRange.NANOSECONDS_PER_MILLISECOND;
int j = (int) ((double) (TimeRange.MILLISECONDS_PER_SECOND * i) / d0);
@@ -86,9 +92,13 @@
this.scheduledCurrentSprintTicks = 0L;
this.sprintTimeSpend = 0L;
- this.server.createCommandSourceStack().sendSuccess(() -> {
- return IChatBaseComponent.translatable("commands.tick.sprint.report", j, s);
- }, true);
+ // CraftBukkit start - add sendLog parameter
+ if (sendLog) {
+ this.server.createCommandSourceStack().sendSuccess(() -> {
+ return IChatBaseComponent.translatable("commands.tick.sprint.report", j, s);
+ }, true);
+ }
+ // CraftBukkit end
this.remainingSprintTicks = 0L;
this.setFrozen(this.previousIsFrozen);
this.server.onTickRateChanged();
@@ -102,7 +112,7 @@
--this.remainingSprintTicks;
return true;
} else {
- this.finishTickSprint();
+ this.finishTickSprint(true); // CraftBukkit - add sendLog parameter
return false;
}
}

View file

@ -133,6 +133,7 @@ import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.Registry;
import org.bukkit.Server;
import org.bukkit.ServerTickManager;
import org.bukkit.StructureType;
import org.bukkit.UnsafeValues;
import org.bukkit.Warning.WarningState;
@ -298,6 +299,7 @@ public final class CraftServer implements Server {
public String minimumAPI;
public CraftScoreboardManager scoreboardManager;
public CraftDataPackManager dataPackManager;
private CraftServerTickManager serverTickManager;
public boolean playerCommandState;
private boolean printSaveWarning;
private CraftIconCache icon;
@ -324,6 +326,7 @@ public final class CraftServer implements Server {
this.serverVersion = CraftServer.class.getPackage().getImplementationVersion();
this.structureManager = new CraftStructureManager(console.getStructureManager());
this.dataPackManager = new CraftDataPackManager(this.getServer().getPackRepository());
this.serverTickManager = new CraftServerTickManager(console.tickRateManager());
Bukkit.setServer(this);
@ -714,6 +717,11 @@ public final class CraftServer implements Server {
return this.dataPackManager;
}
@Override
public ServerTickManager getServerTickManager() {
return this.serverTickManager;
}
@Override
public String getResourcePack() {
return this.getServer().getServerResourcePack().map(MinecraftServer.ServerResourcePackInfo::url).orElse("");

View file

@ -0,0 +1,93 @@
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import net.minecraft.server.ServerTickRateManager;
import org.bukkit.ServerTickManager;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.entity.Entity;
final class CraftServerTickManager implements ServerTickManager {
private final ServerTickRateManager manager;
CraftServerTickManager(ServerTickRateManager manager) {
this.manager = manager;
}
@Override
public boolean isRunningNormally() {
return manager.runsNormally();
}
@Override
public boolean isStepping() {
return manager.isSteppingForward();
}
@Override
public boolean isSprinting() {
return manager.isSprinting();
}
@Override
public boolean isFrozen() {
return manager.isFrozen();
}
@Override
public float getTickRate() {
return manager.tickrate();
}
@Override
public void setTickRate(final float tickRate) {
Preconditions.checkArgument(tickRate > 1 && tickRate < 10_000, "The given tick rate must not be less than one");
manager.setTickRate(tickRate);
}
@Override
public void setFrozen(final boolean frozen) {
if (frozen) {
if (manager.isSprinting()) {
manager.stopSprinting();
}
if (manager.isSteppingForward()) {
manager.stopStepping();
}
}
manager.setFrozen(frozen);
}
@Override
public boolean stepGameIfFrozen(final int ticks) {
return manager.stepGameIfPaused(ticks);
}
@Override
public boolean stopStepping() {
return manager.stopStepping();
}
@Override
public boolean requestGameToSprint(final int ticks) {
return manager.requestGameToSprint(ticks);
}
@Override
public boolean stopSprinting() {
return manager.stopSprinting();
}
@Override
public boolean isFrozen(final Entity entity) {
Preconditions.checkArgument(entity != null, "The given entity must not be null");
return manager.isEntityFrozen(((CraftEntity) entity).getHandle());
}
@Override
public int getFrozenTicksToRun() {
return manager.frozenTicksToRun();
}
}