From 5aaf7a9342bc3c4c9c64fda46b08e9dd066d3484 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Tue, 16 Aug 2022 12:05:07 +0200 Subject: [PATCH] Fix 1.18.2 spigot bug --- pom.xml | 2 +- .../CoordinatesObfuscator.java | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 717c3bc..9f48c97 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.warp CoordinatesObfuscator - 1.4.6 + 1.4.7 CoordinatesObfuscator diff --git a/src/main/java/org/warp/coordinatesobfuscator/CoordinatesObfuscator.java b/src/main/java/org/warp/coordinatesobfuscator/CoordinatesObfuscator.java index 2081829..5817e9b 100644 --- a/src/main/java/org/warp/coordinatesobfuscator/CoordinatesObfuscator.java +++ b/src/main/java/org/warp/coordinatesobfuscator/CoordinatesObfuscator.java @@ -7,6 +7,12 @@ import com.comphenix.protocol.events.*; import com.comphenix.protocol.injector.GamePhase; import com.comphenix.protocol.wrappers.nbt.NbtBase; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; + +import java.lang.invoke.MethodType; + import org.bukkit.Bukkit; import org.bukkit.WorldBorder; import org.bukkit.entity.Player; @@ -28,6 +34,20 @@ public class CoordinatesObfuscator extends JavaPlugin implements Listener { public static final boolean DEBUG_ENABLED = "debug".equals(System.getProperty("coordinates_obfuscator.env")); public static final boolean DISALLOW_REMOVING_NONEXISTENT_COORDINATES = false; + + private static final MethodHandle CREATE_WORLD_BORDER_METHOD; + + static { + var lookup = MethodHandles.publicLookup(); + MethodHandle createWorldBorderMethod = null; + try { + Class worldBorderClass = lookup.findClass("org.bukkit.WorldBorder"); + createWorldBorderMethod = lookup.findStatic(Bukkit.class, "createWorldBorder", MethodType.methodType(worldBorderClass)); + } catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException e) { + // ignore + } + CREATE_WORLD_BORDER_METHOD = createWorldBorderMethod; + } private Logger logger; @Override @@ -46,7 +66,7 @@ public void onEnable() { final ProtocolManager pm = ProtocolLibrary.getProtocolManager(); - final HashSet packets = new HashSet(); + final HashSet packets = new HashSet<>(); // /Server side packets { @@ -204,7 +224,15 @@ public void onPlayerTeleport(final PlayerRespawnEvent event){ private void setMaxWorldBorder(final Player player){ Bukkit.getScheduler().runTaskLater(this, () -> { if (!player.isOnline()) return; - WorldBorder border = Bukkit.createWorldBorder(); + // Some bukkit versions don't have this method + if (CREATE_WORLD_BORDER_METHOD == null) return; + WorldBorder border; + try { + border = (WorldBorder) CREATE_WORLD_BORDER_METHOD.invokeExact(); + } catch (Throwable e) { + // This shouldn't happen + throw new RuntimeException(e); + } border.setCenter(player.getWorld().getWorldBorder().getCenter()); border.setSize(59999968E7); player.setWorldBorder(border);