diff --git a/Compatibility/src/com/songoda/core/compatibility/ClassMapping.java b/Compatibility/src/com/songoda/core/compatibility/ClassMapping.java index 1e190849..2322167b 100644 --- a/Compatibility/src/com/songoda/core/compatibility/ClassMapping.java +++ b/Compatibility/src/com/songoda/core/compatibility/ClassMapping.java @@ -13,6 +13,7 @@ public enum ClassMapping { BLOCK_POSITION("core", "BlockPosition"), CHAT_MESSAGE_TYPE("network.chat", "ChatMessageType"), CHUNK("world.level.chunk", "Chunk"), + CLIENTBOUND_INITIALIZE_BORDER_PACKET("network.protocol.game", "ClientboundInitializeBorderPacket"), // Added in 1.17 ENCHANTMENT_MANAGER("world.item.enchantment", "EnchantmentManager"), ENTITY("world.entity", "Entity"), ENTITY_INSENTIENT("world.entity", "EntityInsentient"), diff --git a/Core/src/main/java/com/songoda/core/utils/NMSUtils.java b/Core/src/main/java/com/songoda/core/utils/NMSUtils.java index 9876b531..d8534a16 100644 --- a/Core/src/main/java/com/songoda/core/utils/NMSUtils.java +++ b/Core/src/main/java/com/songoda/core/utils/NMSUtils.java @@ -1,7 +1,7 @@ package com.songoda.core.utils; -import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.compatibility.ClassMapping; +import com.songoda.core.compatibility.ServerVersion; import org.bukkit.entity.Player; import java.lang.reflect.Field; @@ -66,7 +66,7 @@ public class NMSUtils { public static void sendPacket(Player player, Object packet) { try { Object handle = player.getClass().getMethod("getHandle").invoke(player); - Object playerConnection = handle.getClass().getField("playerConnection").get(handle); + Object playerConnection = handle.getClass().getField(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_17) ? "b" : "playerConnection").get(handle); playerConnection.getClass().getMethod("sendPacket", ClassMapping.PACKET.getClazz()).invoke(playerConnection, packet); } catch (Exception e) { e.printStackTrace(); diff --git a/Core/src/main/java/com/songoda/core/world/SWorldBorder.java b/Core/src/main/java/com/songoda/core/world/SWorldBorder.java index 1cfb4ad8..b0bd2280 100644 --- a/Core/src/main/java/com/songoda/core/world/SWorldBorder.java +++ b/Core/src/main/java/com/songoda/core/world/SWorldBorder.java @@ -1,8 +1,9 @@ package com.songoda.core.world; -import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.compatibility.ClassMapping; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.utils.NMSUtils; +import net.minecraft.world.level.border.WorldBorder; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -11,27 +12,34 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class SWorldBorder { - + private static Class packetPlayOutWorldBorderEnumClass; private static Class worldBorderClass; private static Class craftWorldClass; private static Constructor packetPlayOutWorldBorderConstructor; + private static Constructor clientboundInitializeBorderPacketConstructor; + static { try { - Class packetPlayOutWorldBorder = ClassMapping.PACKET_PLAY_OUT_WORLD_BORDER.getClazz(); + worldBorderClass = ClassMapping.WORLD_BORDER.getClazz(); + craftWorldClass = NMSUtils.getCraftClass("CraftWorld"); - if(packetPlayOutWorldBorder != null) { - if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) - packetPlayOutWorldBorderEnumClass = packetPlayOutWorldBorder.getDeclaredClasses()[0]; - else - packetPlayOutWorldBorderEnumClass = packetPlayOutWorldBorder.getDeclaredClasses()[1]; - - worldBorderClass = ClassMapping.WORLD_BORDER.getClazz(); - craftWorldClass = NMSUtils.getCraftClass("CraftWorld"); - - packetPlayOutWorldBorderConstructor = packetPlayOutWorldBorder.getConstructor(worldBorderClass, - packetPlayOutWorldBorderEnumClass); + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_17)) { + Class clientboundInitializeBorderPacketClass = ClassMapping.CLIENTBOUND_INITIALIZE_BORDER_PACKET.getClazz(); + clientboundInitializeBorderPacketConstructor = clientboundInitializeBorderPacketClass.getConstructor(worldBorderClass); + } else { + Class packetPlayOutWorldBorder = ClassMapping.PACKET_PLAY_OUT_WORLD_BORDER.getClazz(); + + if (packetPlayOutWorldBorder != null) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) + packetPlayOutWorldBorderEnumClass = packetPlayOutWorldBorder.getDeclaredClasses()[0]; + else + packetPlayOutWorldBorderEnumClass = packetPlayOutWorldBorder.getDeclaredClasses()[1]; + + packetPlayOutWorldBorderConstructor = packetPlayOutWorldBorder.getConstructor(worldBorderClass, + packetPlayOutWorldBorderEnumClass); + } } } catch (Exception e) { e.printStackTrace(); @@ -60,7 +68,7 @@ public class SWorldBorder { Method setWarningTime = worldBorder.getClass().getMethod("setWarningTime", int.class); setWarningTime.invoke(worldBorder, 0); - + Method setWarningDistance = worldBorder.getClass().getMethod("setWarningDistance", int.class); setWarningDistance.invoke(worldBorder, 0); @@ -68,15 +76,20 @@ public class SWorldBorder { double.class, long.class); if (color == Color.Green) { - transitionSizeBetween.invoke(worldBorder, size - 0.1D, size, 20000000L); + transitionSizeBetween.invoke(worldBorder, size - 0.1D, size, Long.MAX_VALUE); } else if (color == Color.Red) { - transitionSizeBetween.invoke(worldBorder, size, size - 1.0D, 20000000L); + transitionSizeBetween.invoke(worldBorder, size, size - 1.0D, Long.MAX_VALUE); } - @SuppressWarnings({"unchecked", "rawtypes"}) - Object packet = packetPlayOutWorldBorderConstructor.newInstance(worldBorder, - Enum.valueOf((Class) packetPlayOutWorldBorderEnumClass, "INITIALIZE")); - NMSUtils.sendPacket(player, packet); + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_17)) { + Object packet = clientboundInitializeBorderPacketConstructor.newInstance(worldBorder); + NMSUtils.sendPacket(player, packet); + } else { + @SuppressWarnings({"unchecked", "rawtypes"}) + Object packet = packetPlayOutWorldBorderConstructor.newInstance(worldBorder, + Enum.valueOf((Class) packetPlayOutWorldBorderEnumClass, "INITIALIZE")); + NMSUtils.sendPacket(player, packet); + } } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { e.printStackTrace(); }