From 019a3ba2a6c61d324ab782e7735734407f686c4a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 10 Oct 2023 17:41:14 +0200 Subject: [PATCH 1/6] Add TPSWarpUtils --- .../src/de/steamwar/core/TPSWarpUtils.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java b/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java new file mode 100644 index 0000000..3dd14e9 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java @@ -0,0 +1,58 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.core; + +import com.comphenix.tinyprotocol.Reflection; +import de.steamwar.core.Core; +import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitTask; + +import java.util.function.LongSupplier; + +@UtilityClass +public class TPSWarpUtils { + + private static long nanoOffset = 0; + private static long nanoDOffset = 0; + private static BukkitTask bukkitTask = null; + + static { + Class clazz = Reflection.getClass(Core.getVersion() <= 15 ? "{nms}.SystemUtils" : "net.minecraft.SystemUtils"); + Reflection.FieldAccessor fieldAccessor = Reflection.getField(clazz, LongSupplier.class, 0); + fieldAccessor.set(clazz, (LongSupplier) () -> System.nanoTime() + nanoDOffset); + } + + public static void warp(double tps) { + double d = 50 - (50 / (tps / 20.0)); + nanoDOffset = Math.max(0, (long) (d * 1000000)); + if (nanoDOffset == 0) { + if (bukkitTask == null) return; + bukkitTask.cancel(); + bukkitTask = null; + } else if (bukkitTask == null) { + bukkitTask = Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> nanoOffset += nanoDOffset, 1, 1); + } + } + + public static boolean isWarping() { + return nanoDOffset > 0; + } +} -- 2.39.2 From e202adeb865f188ffb9619c06caed117e44c7272 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 10 Oct 2023 18:07:21 +0200 Subject: [PATCH 2/6] Add TPSWarpUtils --- .../src/de/steamwar/core/ProtocolWrapper19.java | 7 +++++++ .../src/de/steamwar/core/ProtocolWrapper.java | 5 +++++ SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java | 10 +++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java b/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java index b4b0f8f..f4f399f 100644 --- a/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java +++ b/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java @@ -22,6 +22,7 @@ package de.steamwar.core; import com.comphenix.tinyprotocol.Reflection; import com.mojang.authlib.GameProfile; import com.mojang.datafixers.util.Pair; +import net.minecraft.SystemUtils; import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket; import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket; import net.minecraft.world.level.EnumGamemode; @@ -30,6 +31,7 @@ import org.bukkit.GameMode; import java.util.Collections; import java.util.EnumSet; import java.util.List; +import java.util.function.LongSupplier; public class ProtocolWrapper19 implements ProtocolWrapper { @@ -54,4 +56,9 @@ public class ProtocolWrapper19 implements ProtocolWrapper { updatePlayers.set(packet, Collections.singletonList(new ClientboundPlayerInfoUpdatePacket.b(profile.getId(), profile, false, 0, EnumGamemode.a(mode.getValue()), null, null))); return packet; } + + @Override + public void initTPSWarp(LongSupplier longSupplier) { + SystemUtils.a = () -> System.nanoTime() + longSupplier.getAsLong(); + } } diff --git a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java index 6d140ca..0de99c3 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java @@ -23,6 +23,8 @@ import com.comphenix.tinyprotocol.Reflection; import com.mojang.authlib.GameProfile; import org.bukkit.GameMode; +import java.util.function.LongSupplier; + public interface ProtocolWrapper { Class itemStack = Reflection.getClass("{nms.world.item}.ItemStack"); @@ -42,6 +44,9 @@ public interface ProtocolWrapper { Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode); + default void initTPSWarp(LongSupplier longSupplier) { + } + enum PlayerInfoAction { ADD, GAMEMODE, diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java b/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java index 3dd14e9..aca02ca 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java @@ -35,9 +35,13 @@ public class TPSWarpUtils { private static BukkitTask bukkitTask = null; static { - Class clazz = Reflection.getClass(Core.getVersion() <= 15 ? "{nms}.SystemUtils" : "net.minecraft.SystemUtils"); - Reflection.FieldAccessor fieldAccessor = Reflection.getField(clazz, LongSupplier.class, 0); - fieldAccessor.set(clazz, (LongSupplier) () -> System.nanoTime() + nanoDOffset); + if (Core.getVersion() <= 18) { + Class clazz = Reflection.getClass("{nms}.SystemUtils"); + Reflection.FieldAccessor fieldAccessor = Reflection.getField(clazz, LongSupplier.class, 0); + fieldAccessor.set(clazz, (LongSupplier) () -> System.nanoTime() + nanoDOffset); + } else { + ProtocolWrapper.impl.initTPSWarp(() -> nanoDOffset); + } } public static void warp(double tps) { -- 2.39.2 From d7134b691690c3e8a34f6c5f9e4cb4d8a7fff8a9 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 10 Oct 2023 18:10:43 +0200 Subject: [PATCH 3/6] Add TPSWarpUtils --- SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java | 3 +++ SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java | 8 +------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java index 0de99c3..89cb1ea 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java @@ -45,6 +45,9 @@ public interface ProtocolWrapper { Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode); default void initTPSWarp(LongSupplier longSupplier) { + if (Core.getVersion() > 19) return; + Class systemUtils = Reflection.getClass("{nms}.SystemUtils"); + Reflection.getField(systemUtils, LongSupplier.class, 0).set(systemUtils, longSupplier); } enum PlayerInfoAction { diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java b/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java index aca02ca..91d7296 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java @@ -35,13 +35,7 @@ public class TPSWarpUtils { private static BukkitTask bukkitTask = null; static { - if (Core.getVersion() <= 18) { - Class clazz = Reflection.getClass("{nms}.SystemUtils"); - Reflection.FieldAccessor fieldAccessor = Reflection.getField(clazz, LongSupplier.class, 0); - fieldAccessor.set(clazz, (LongSupplier) () -> System.nanoTime() + nanoDOffset); - } else { - ProtocolWrapper.impl.initTPSWarp(() -> nanoDOffset); - } + ProtocolWrapper.impl.initTPSWarp(() -> nanoDOffset); } public static void warp(double tps) { -- 2.39.2 From 1c5015adf49e173661aa474e477909d775780673 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 10 Oct 2023 18:15:50 +0200 Subject: [PATCH 4/6] Add TPSWarpUtils --- SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java | 1 - 1 file changed, 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java index 89cb1ea..d803970 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java @@ -45,7 +45,6 @@ public interface ProtocolWrapper { Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode); default void initTPSWarp(LongSupplier longSupplier) { - if (Core.getVersion() > 19) return; Class systemUtils = Reflection.getClass("{nms}.SystemUtils"); Reflection.getField(systemUtils, LongSupplier.class, 0).set(systemUtils, longSupplier); } -- 2.39.2 From b40bb81b7fcfeacde69d3bdf061d84e6101ec400 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 10 Oct 2023 18:18:17 +0200 Subject: [PATCH 5/6] Add TPSWarpUtils --- SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java index d803970..e678749 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java @@ -46,7 +46,7 @@ public interface ProtocolWrapper { default void initTPSWarp(LongSupplier longSupplier) { Class systemUtils = Reflection.getClass("{nms}.SystemUtils"); - Reflection.getField(systemUtils, LongSupplier.class, 0).set(systemUtils, longSupplier); + Reflection.getField(systemUtils, LongSupplier.class, 0).set(systemUtils, (LongSupplier) () -> System.nanoTime() + longSupplier.getAsLong()); } enum PlayerInfoAction { -- 2.39.2 From 1a112cff6b086e3947d15679340ccbb56e7b2907 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 10 Oct 2023 18:20:13 +0200 Subject: [PATCH 6/6] Add TPSWarpUtils --- SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java b/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java index 91d7296..3550a99 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWarpUtils.java @@ -35,7 +35,7 @@ public class TPSWarpUtils { private static BukkitTask bukkitTask = null; static { - ProtocolWrapper.impl.initTPSWarp(() -> nanoDOffset); + ProtocolWrapper.impl.initTPSWarp(() -> nanoOffset); } public static void warp(double tps) { -- 2.39.2