Add new TickRateChangedEvent; fires when tickRate changes

This commit is contained in:
Axionize 2024-10-08 20:38:39 -04:00
parent 7cd4f2c15f
commit 34ec367b30
2 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,72 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Axionize <154778082+Axionize@users.noreply.github.com>
Date: Tue, 8 Oct 2024 20:38:01 -0400
Subject: [PATCH] Add new TickRateChangedEvent
diff --git a/src/main/java/org/bukkit/event/server/TickRateChangeEvent.java b/src/main/java/org/bukkit/event/server/TickRateChangeEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..97c13fe34ca28775c6757dc2aac28d8d7a375bdf
--- /dev/null
+++ b/src/main/java/org/bukkit/event/server/TickRateChangeEvent.java
@@ -0,0 +1,60 @@
+package org.bukkit.event.server;
+
+import org.bukkit.command.CommandSender;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Fired when the server's target tickrate changes
+ */
+public class TickRateChangeEvent extends ServerEvent implements Cancellable {
+
+ private float tickRate;
+ private final CommandSender commandSender;
+ private boolean cancelled;
+ private static HandlerList handlers = new HandlerList();
+
+ public TickRateChangeEvent(CommandSender commandSender, float tickRate) {
+ this.commandSender = commandSender;
+ this.tickRate = tickRate;
+ }
+
+ /**
+ * @return {@link org.bukkit.command.CommandSender} representing source of the /tick rate command
+ * Will return null if the tick rate was set by a plugin or setTickRate() was otherwise directly called
+ */
+ public CommandSender getCommandSender() {
+ return commandSender;
+ }
+
+ /**
+ * @return float representing new tick rate of the server between 1.0F and 10,000F
+ */
+ public float getTickRate() {
+ return tickRate;
+ }
+
+ /**
+ *
+ * @param tickRate float overriding the new tickrate being applied to the server
+ */
+ public void setTickRate(float tickRate) {
+ this.tickRate = tickRate;
+ }
+
+ @Override
+ public @NotNull HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ cancelled = cancel;
+ }
+}

View file

@ -0,0 +1,51 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Axionize <154778082+Axionize@users.noreply.github.com>
Date: Tue, 8 Oct 2024 20:32:34 -0400
Subject: [PATCH] Fire and handle canellable TickRateChangedEvent
diff --git a/src/main/java/net/minecraft/server/ServerTickRateManager.java b/src/main/java/net/minecraft/server/ServerTickRateManager.java
index 37dcf3dc3e50afd85912a7496c828576a38a4e9c..765c17532d13b136c51fe68a970aab36b8fb7f24 100644
--- a/src/main/java/net/minecraft/server/ServerTickRateManager.java
+++ b/src/main/java/net/minecraft/server/ServerTickRateManager.java
@@ -121,12 +121,22 @@ public class ServerTickRateManager extends TickRateManager {
this.sprintTimeSpend += System.nanoTime() - this.sprintTickStartTime;
}
+ // Paper start - Fire and handle cancellable TickRateChangeEvent
+ public void setTickRate(org.bukkit.command.CommandSender commandSender, float tickRate) {
+ org.bukkit.event.server.TickRateChangeEvent event = new org.bukkit.event.server.TickRateChangeEvent(commandSender, tickRate);
+ event.callEvent();
+ if (!event.isCancelled()) {
+ super.setTickRate(tickRate);
+ this.server.onTickRateChanged();
+ this.updateStateToClients();
+ }
+ }
+
@Override
public void setTickRate(float tickRate) {
- super.setTickRate(tickRate);
- this.server.onTickRateChanged();
- this.updateStateToClients();
+ setTickRate(null, tickRate);
}
+ // Paper end - Fire and handle cancellable TickRateChangeEvent
public void updateJoiningPlayer(ServerPlayer player) {
player.connection.send(ClientboundTickingStatePacket.from(this));
diff --git a/src/main/java/net/minecraft/server/commands/TickCommand.java b/src/main/java/net/minecraft/server/commands/TickCommand.java
index 5ce845a9cd84c355e2716dfcb0b62686c783c9f9..727e7df4231bc5797ab3e2046111b9483dab40fa 100644
--- a/src/main/java/net/minecraft/server/commands/TickCommand.java
+++ b/src/main/java/net/minecraft/server/commands/TickCommand.java
@@ -61,7 +61,9 @@ public class TickCommand {
private static int setTickingRate(CommandSourceStack source, float rate) {
ServerTickRateManager serverTickRateManager = source.getServer().tickRateManager();
- serverTickRateManager.setTickRate(rate);
+ // Paper start - Fire and handle cancellable TickRateChangeEvent
+ serverTickRateManager.setTickRate(source.getBukkitSender(), rate);
+ // Paper end - Fire and handle cancellable TickRateChangeEvent
String string = String.format("%.1f", rate);
source.sendSuccess(() -> Component.translatable("commands.tick.rate.success", string), true);
return (int)rate;