3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-15 04:20:04 +01:00
Paper/patches/api/0167-Server-Tick-Events.patch

117 Zeilen
3.6 KiB
Diff

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..c879588693c930226049e60393f2f23aad1588b9
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/ServerTickEndEvent.java
@@ -0,0 +1,62 @@
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();
+ }
+
+ @Override
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
+ }
+}
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..890153657ea5b756a8a3a038d6b53857e0d17fea
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/ServerTickStartEvent.java
@@ -0,0 +1,35 @@
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
+ }
+
+ @Override
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
+ }
+}