Added support for Worldborders in 1.17

This commit is contained in:
Brianna 2021-06-21 17:18:18 -05:00
parent ebf0d06434
commit 711125aa67
3 changed files with 37 additions and 23 deletions

View File

@ -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"),

View File

@ -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();

View File

@ -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;
@ -17,22 +18,29 @@ public class SWorldBorder {
private static Class<?> craftWorldClass;
private static Constructor<?> packetPlayOutWorldBorderConstructor;
private static Constructor<?> clientboundInitializeBorderPacketConstructor;
static {
try {
worldBorderClass = ClassMapping.WORLD_BORDER.getClazz();
craftWorldClass = NMSUtils.getCraftClass("CraftWorld");
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 (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);
}
}
} catch (Exception e) {
e.printStackTrace();
}
@ -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);
}
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<Enum>) packetPlayOutWorldBorderEnumClass, "INITIALIZE"));
NMSUtils.sendPacket(player, packet);
}
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
e.printStackTrace();
}