From 46c67d71082a94504f8913b5db3ccf18d098e87d Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sun, 17 Dec 2023 09:30:00 +1100 Subject: [PATCH] SPIGOT-7540, #1312: Add ServerTickManager API By: Miles Holder --- .../server/ServerTickRateManager.patch | 53 +++++++++++ .../org/bukkit/craftbukkit/CraftServer.java | 8 ++ .../craftbukkit/CraftServerTickManager.java | 93 +++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 paper-server/nms-patches/net/minecraft/server/ServerTickRateManager.patch create mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/CraftServerTickManager.java diff --git a/paper-server/nms-patches/net/minecraft/server/ServerTickRateManager.patch b/paper-server/nms-patches/net/minecraft/server/ServerTickRateManager.patch new file mode 100644 index 0000000000..ef54ff385b --- /dev/null +++ b/paper-server/nms-patches/net/minecraft/server/ServerTickRateManager.patch @@ -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; + } + } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 238441fcea..0863da1f80 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -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(""); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServerTickManager.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServerTickManager.java new file mode 100644 index 0000000000..9651be64ad --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServerTickManager.java @@ -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(); + } +}