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"), BLOCK_POSITION("core", "BlockPosition"),
CHAT_MESSAGE_TYPE("network.chat", "ChatMessageType"), CHAT_MESSAGE_TYPE("network.chat", "ChatMessageType"),
CHUNK("world.level.chunk", "Chunk"), CHUNK("world.level.chunk", "Chunk"),
CLIENTBOUND_INITIALIZE_BORDER_PACKET("network.protocol.game", "ClientboundInitializeBorderPacket"), // Added in 1.17
ENCHANTMENT_MANAGER("world.item.enchantment", "EnchantmentManager"), ENCHANTMENT_MANAGER("world.item.enchantment", "EnchantmentManager"),
ENTITY("world.entity", "Entity"), ENTITY("world.entity", "Entity"),
ENTITY_INSENTIENT("world.entity", "EntityInsentient"), ENTITY_INSENTIENT("world.entity", "EntityInsentient"),

View File

@ -1,7 +1,7 @@
package com.songoda.core.utils; package com.songoda.core.utils;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.compatibility.ClassMapping; import com.songoda.core.compatibility.ClassMapping;
import com.songoda.core.compatibility.ServerVersion;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -66,7 +66,7 @@ public class NMSUtils {
public static void sendPacket(Player player, Object packet) { public static void sendPacket(Player player, Object packet) {
try { try {
Object handle = player.getClass().getMethod("getHandle").invoke(player); 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); playerConnection.getClass().getMethod("sendPacket", ClassMapping.PACKET.getClazz()).invoke(playerConnection, packet);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,8 +1,9 @@
package com.songoda.core.world; package com.songoda.core.world;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.compatibility.ClassMapping; import com.songoda.core.compatibility.ClassMapping;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.utils.NMSUtils; import com.songoda.core.utils.NMSUtils;
import net.minecraft.world.level.border.WorldBorder;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -11,27 +12,34 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class SWorldBorder { public class SWorldBorder {
private static Class<?> packetPlayOutWorldBorderEnumClass; private static Class<?> packetPlayOutWorldBorderEnumClass;
private static Class<?> worldBorderClass; private static Class<?> worldBorderClass;
private static Class<?> craftWorldClass; private static Class<?> craftWorldClass;
private static Constructor<?> packetPlayOutWorldBorderConstructor; private static Constructor<?> packetPlayOutWorldBorderConstructor;
private static Constructor<?> clientboundInitializeBorderPacketConstructor;
static { static {
try { 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_17)) {
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) Class<?> clientboundInitializeBorderPacketClass = ClassMapping.CLIENTBOUND_INITIALIZE_BORDER_PACKET.getClazz();
packetPlayOutWorldBorderEnumClass = packetPlayOutWorldBorder.getDeclaredClasses()[0]; clientboundInitializeBorderPacketConstructor = clientboundInitializeBorderPacketClass.getConstructor(worldBorderClass);
else } else {
packetPlayOutWorldBorderEnumClass = packetPlayOutWorldBorder.getDeclaredClasses()[1]; Class<?> packetPlayOutWorldBorder = ClassMapping.PACKET_PLAY_OUT_WORLD_BORDER.getClazz();
worldBorderClass = ClassMapping.WORLD_BORDER.getClazz(); if (packetPlayOutWorldBorder != null) {
craftWorldClass = NMSUtils.getCraftClass("CraftWorld"); if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11))
packetPlayOutWorldBorderEnumClass = packetPlayOutWorldBorder.getDeclaredClasses()[0];
packetPlayOutWorldBorderConstructor = packetPlayOutWorldBorder.getConstructor(worldBorderClass, else
packetPlayOutWorldBorderEnumClass); packetPlayOutWorldBorderEnumClass = packetPlayOutWorldBorder.getDeclaredClasses()[1];
packetPlayOutWorldBorderConstructor = packetPlayOutWorldBorder.getConstructor(worldBorderClass,
packetPlayOutWorldBorderEnumClass);
}
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -60,7 +68,7 @@ public class SWorldBorder {
Method setWarningTime = worldBorder.getClass().getMethod("setWarningTime", int.class); Method setWarningTime = worldBorder.getClass().getMethod("setWarningTime", int.class);
setWarningTime.invoke(worldBorder, 0); setWarningTime.invoke(worldBorder, 0);
Method setWarningDistance = worldBorder.getClass().getMethod("setWarningDistance", int.class); Method setWarningDistance = worldBorder.getClass().getMethod("setWarningDistance", int.class);
setWarningDistance.invoke(worldBorder, 0); setWarningDistance.invoke(worldBorder, 0);
@ -68,15 +76,20 @@ public class SWorldBorder {
double.class, long.class); double.class, long.class);
if (color == Color.Green) { 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) { } 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"}) if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_17)) {
Object packet = packetPlayOutWorldBorderConstructor.newInstance(worldBorder, Object packet = clientboundInitializeBorderPacketConstructor.newInstance(worldBorder);
Enum.valueOf((Class<Enum>) packetPlayOutWorldBorderEnumClass, "INITIALIZE")); NMSUtils.sendPacket(player, packet);
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) { } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }