PaperMC/patches/api/0167-Server-Tick-Events.patch

115 lines
3.6 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 27 Mar 2019 21:58:55 -0400
Subject: [PATCH] Server Tick Events
Fires event at start and end of a server tick
diff --git a/src/main/java/com/destroystokyo/paper/event/server/ServerTickEndEvent.java b/src/main/java/com/destroystokyo/paper/event/server/ServerTickEndEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..15a177ce555ed329988d97a9f0b5d9c71aea18fb
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/ServerTickEndEvent.java
@@ -0,0 +1,61 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper.event.server;
+
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+/**
+ * Called when the server has finished ticking the main loop
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class ServerTickEndEvent extends Event {
+
2024-02-01 10:15:57 +01:00
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2021-06-11 14:02:28 +02:00
+ private final int tickNumber;
+ private final double tickDuration;
+ private final long timeEnd;
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
+ public ServerTickEndEvent(final int tickNumber, final double tickDuration, final long timeRemaining) {
2021-06-11 14:02:28 +02:00
+ this.tickNumber = tickNumber;
+ this.tickDuration = tickDuration;
+ this.timeEnd = System.nanoTime() + timeRemaining;
+ }
+
+ /**
+ * @return What tick this was since start (first tick = 1)
+ */
+ public int getTickNumber() {
2024-02-01 10:15:57 +01:00
+ return this.tickNumber;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * @return Time in milliseconds of how long this tick took
+ */
+ public double getTickDuration() {
2024-02-01 10:15:57 +01:00
+ return this.tickDuration;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Amount of nanoseconds remaining before the next tick should start.
2024-02-01 10:15:57 +01:00
+ * <p>
2021-06-11 14:02:28 +02:00
+ * If this value is negative, then that means the server has exceeded the tick time limit and TPS has been lost.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * Method will continuously return the updated time remaining value. (return value is not static)
2021-06-11 14:02:28 +02:00
+ *
+ * @return Amount of nanoseconds remaining before the next tick should start
+ */
+ public long getTimeRemaining() {
+ return this.timeEnd - System.nanoTime();
+ }
+
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}
diff --git a/src/main/java/com/destroystokyo/paper/event/server/ServerTickStartEvent.java b/src/main/java/com/destroystokyo/paper/event/server/ServerTickStartEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..7c15e6487e17b3255e2428d73aef344ee32a43fd
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/ServerTickStartEvent.java
@@ -0,0 +1,34 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper.event.server;
+
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class ServerTickStartEvent extends Event {
+
2024-02-01 10:15:57 +01:00
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2021-06-11 14:02:28 +02:00
+ private final int tickNumber;
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
+ public ServerTickStartEvent(final int tickNumber) {
2021-06-11 14:02:28 +02:00
+ this.tickNumber = tickNumber;
+ }
+
+ /**
+ * @return What tick this is going be since start (first tick = 1)
+ */
+ public int getTickNumber() {
2024-02-01 10:15:57 +01:00
+ return this.tickNumber;
2021-06-11 14:02:28 +02:00
+ }
+
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}