| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import java.util.Locale;
|
| 4 | import net.minecraft.network.chat.Component;
|
| 5 | import net.minecraft.network.protocol.game.ClientboundTickingStatePacket;
|
| 6 | import net.minecraft.network.protocol.game.ClientboundTickingStepPacket;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.util.TimeUtil;
|
| 9 | import net.minecraft.world.TickRateManager;
|
| 10 |
|
| 11 | public class ServerTickRateManager extends TickRateManager {
|
| 12 | private long remainingSprintTicks = 0L;
|
| 13 | private long sprintTickStartTime = 0L;
|
| 14 | private long sprintTimeSpend = 0L;
|
| 15 | private long scheduledCurrentSprintTicks = 0L;
|
| 16 | private boolean previousIsFrozen = false;
|
| 17 | private final MinecraftServer server;
|
| 18 |
|
| 19 | public ServerTickRateManager(final MinecraftServer server) {
|
| 20 | this.server = server;
|
| 21 | }
|
| 22 |
|
| 23 | public boolean isSprinting() {
|
| 24 | return this.scheduledCurrentSprintTicks > 0L;
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public void setFrozen(final boolean frozen) {
|
| 29 | super.setFrozen(frozen);
|
| 30 | this.updateStateToClients();
|
| 31 | }
|
| 32 |
|
| 33 | private void updateStateToClients() {
|
| 34 | this.server.getPlayerList().broadcastAll(ClientboundTickingStatePacket.from(this));
|
| 35 | }
|
| 36 |
|
| 37 | private void updateStepTicks() {
|
| 38 | this.server.getPlayerList().broadcastAll(ClientboundTickingStepPacket.from(this));
|
| 39 | }
|
| 40 |
|
| 41 | public boolean stepGameIfPaused(final int ticks) {
|
| 42 | if (!this.isFrozen()) {
|
| 43 | return false;
|
| 44 | }
|
| 45 |
|
| 46 | this.frozenTicksToRun = ticks;
|
| 47 | this.updateStepTicks();
|
| 48 | return true;
|
| 49 | }
|
| 50 |
|
| 51 | public boolean stopStepping() {
|
| 52 | if (this.frozenTicksToRun > 0) {
|
| 53 | this.frozenTicksToRun = 0;
|
| 54 | this.updateStepTicks();
|
| 55 | return true;
|
| 56 | } else {
|
| 57 | return false;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | public boolean stopSprinting() {
|
| 62 | if (this.remainingSprintTicks > 0L) {
|
| 63 | this.finishTickSprint();
|
| 64 | return true;
|
| 65 | } else {
|
| 66 | return false;
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | public boolean requestGameToSprint(final int time) {
|
| 71 | boolean interrupted = this.remainingSprintTicks > 0L;
|
| 72 | this.sprintTimeSpend = 0L;
|
| 73 | this.scheduledCurrentSprintTicks = time;
|
| 74 | this.remainingSprintTicks = time;
|
| 75 | this.previousIsFrozen = this.isFrozen();
|
| 76 | this.setFrozen(false);
|
| 77 | return interrupted;
|
| 78 | }
|
| 79 |
|
| 80 | private void finishTickSprint() {
|
| 81 | long completedTicks = this.scheduledCurrentSprintTicks - this.remainingSprintTicks;
|
| 82 | double millisecondsToComplete = Math.max(1.0, this.sprintTimeSpend) / TimeUtil.NANOSECONDS_PER_MILLISECOND;
|
| 83 | int ticksPerSecond = (int)(TimeUtil.MILLISECONDS_PER_SECOND * completedTicks / millisecondsToComplete);
|
| 84 | String millisecondsPerTick = String.format(Locale.ROOT, "%.2f", completedTicks == 0L ? this.millisecondsPerTick() : millisecondsToComplete / completedTicks);
|
| 85 | this.scheduledCurrentSprintTicks = 0L;
|
| 86 | this.sprintTimeSpend = 0L;
|
| 87 | this.server.createCommandSourceStack().sendSuccess(() -> Component.translatable("commands.tick.sprint.report", ticksPerSecond, millisecondsPerTick), true);
|
| 88 | this.remainingSprintTicks = 0L;
|
| 89 | this.setFrozen(this.previousIsFrozen);
|
| 90 | this.server.onTickRateChanged();
|
| 91 | }
|
| 92 |
|
| 93 | public boolean checkShouldSprintThisTick() {
|
| 94 | if (!this.runGameElements) {
|
| 95 | return false;
|
| 96 | } else if (this.remainingSprintTicks > 0L) {
|
| 97 | this.sprintTickStartTime = System.nanoTime();
|
| 98 | this.remainingSprintTicks--;
|
| 99 | return true;
|
| 100 | } else {
|
| 101 | this.finishTickSprint();
|
| 102 | return false;
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | public void endTickWork() {
|
| 107 | this.sprintTimeSpend = this.sprintTimeSpend + (System.nanoTime() - this.sprintTickStartTime);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public void setTickRate(final float rate) {
|
| 112 | super.setTickRate(rate);
|
| 113 | this.server.onTickRateChanged();
|
| 114 | this.updateStateToClients();
|
| 115 | }
|
| 116 |
|
| 117 | public void updateJoiningPlayer(final ServerPlayer player) {
|
| 118 | player.connection.send(ClientboundTickingStatePacket.from(this));
|
| 119 | player.connection.send(ClientboundTickingStepPacket.from(this));
|
| 120 | }
|
| 121 | }
|
| 122 |
|