From e202adeb865f188ffb9619c06caed117e44c7272 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 10 Oct 2023 18:07:21 +0200 Subject: [PATCH] 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) {