From 48d3c2105d71219948b55d2fe9f7d227e2b15049 Mon Sep 17 00:00:00 2001 From: MeFisto94 Date: Thu, 6 Aug 2020 13:56:58 +0200 Subject: [PATCH 1/3] Fixes an OfflinePlayerUtil breakage caused by NMS Changes --- .../bukkit/util/OfflinePlayerUtil.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/OfflinePlayerUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/OfflinePlayerUtil.java index 41ae4dedd..197b05cd6 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/OfflinePlayerUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/OfflinePlayerUtil.java @@ -97,6 +97,17 @@ public class OfflinePlayerUtil { return callConstructor(c, worldServer); } + public static Object getWorldServer116() { + Object server = getMinecraftServer(); + Class minecraftServerClass = getNmsClass("MinecraftServer"); + Class dimensionManager = getNmsClass("DimensionManager"); + Class genericResourceKey = getNmsClass("ResourceKey"); + Object overworld = getField(makeField(dimensionManager, "OVERWORLD"), null); + Method getWorldServer = + makeMethod(minecraftServerClass, "getWorldServer", genericResourceKey); + return callMethod(getWorldServer, server, overworld); + } + public static Object getWorldServerNew() { Object server = getMinecraftServer(); Class minecraftServerClass = getNmsClass("MinecraftServer"); @@ -115,7 +126,11 @@ public class OfflinePlayerUtil { try { o = callMethod(getWorldServer, server, 0); } catch (final RuntimeException e) { - o = getWorldServerNew(); + try { + o = getWorldServerNew(); + } catch (final RuntimeException f) { + o = getWorldServer116(); + } } return o; } From 6c9b73b100a5ae2cf175b19d6424e0cedbba8604 Mon Sep 17 00:00:00 2001 From: MeFisto94 Date: Sun, 16 Aug 2020 14:02:03 +0200 Subject: [PATCH 2/3] Apply patch by @SirYwell to construct the ResourceKey without consulting the DimensionManager --- .../bukkit/util/OfflinePlayerUtil.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/OfflinePlayerUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/OfflinePlayerUtil.java index 197b05cd6..94a6acedf 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/OfflinePlayerUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/OfflinePlayerUtil.java @@ -63,11 +63,11 @@ public class OfflinePlayerUtil { Object profile = newGameProfile(id, name); Class entityPlayerClass = getNmsClass("EntityPlayer"); Constructor entityPlayerConstructor = - makeConstructor(entityPlayerClass, getNmsClass("MinecraftServer"), - getNmsClass("WorldServer"), getUtilClass("com.mojang.authlib.GameProfile"), - getNmsClass("PlayerInteractManager")); + makeConstructor(entityPlayerClass, getNmsClass("MinecraftServer"), + getNmsClass("WorldServer"), getUtilClass("com.mojang.authlib.GameProfile"), + getNmsClass("PlayerInteractManager")); Object entityPlayer = - callConstructor(entityPlayerConstructor, server, worldServer, profile, interactManager); + callConstructor(entityPlayerConstructor, server, worldServer, profile, interactManager); return (Player) getBukkitEntity(entityPlayer); } @@ -100,12 +100,21 @@ public class OfflinePlayerUtil { public static Object getWorldServer116() { Object server = getMinecraftServer(); Class minecraftServerClass = getNmsClass("MinecraftServer"); - Class dimensionManager = getNmsClass("DimensionManager"); Class genericResourceKey = getNmsClass("ResourceKey"); - Object overworld = getField(makeField(dimensionManager, "OVERWORLD"), null); + Class minecraftKey = getNmsClass("MinecraftKey"); + // MinecraftKey + MinecraftKey -> ResourceKey + Method constructResourceKey = makeMethod(genericResourceKey, "a", minecraftKey, minecraftKey); + // MinecraftKey(String) + Constructor minecraftKeyConstructor = makeConstructor(minecraftKey, String.class); + // minecraft:overworld + Object overworldKey = callConstructor(minecraftKeyConstructor, "overworld"); + // minecraft:dimension + Object dimensionKey = callConstructor(minecraftKeyConstructor, "dimension"); + // ResourceKey[minecraft:dimension / minecraft:overworld] + Object resourceKey = callMethod(constructResourceKey, null, dimensionKey, overworldKey); Method getWorldServer = makeMethod(minecraftServerClass, "getWorldServer", genericResourceKey); - return callMethod(getWorldServer, server, overworld); + return callMethod(getWorldServer, server, resourceKey); } public static Object getWorldServerNew() { From 61d1dcc231b5e1657a7b0fdae5089e21bcaa317c Mon Sep 17 00:00:00 2001 From: MeFisto94 Date: Sun, 16 Aug 2020 14:09:32 +0200 Subject: [PATCH 3/3] Ignore Events if they are not caused by real players (i.e. citizens) --- .../bukkit/listener/PlayerEventListener.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java index c4d7c6d90..8392591b5 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -69,6 +69,7 @@ import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.PremiumVerification; +import com.plotsquared.core.util.ReflectionUtils; import com.plotsquared.core.util.RegExUtil; import com.plotsquared.core.util.entity.EntityCategories; import com.plotsquared.core.util.task.TaskManager; @@ -139,6 +140,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.UUID; +import java.util.function.Predicate; import java.util.regex.Pattern; /** @@ -153,7 +155,15 @@ public class PlayerEventListener extends PlotListener implements Listener { private PlayerMoveEvent moveTmp; private String internalVersion; + private final Predicate isRealPlayerCheck; + { + Class playerImplementation = ReflectionUtils.getCbClass("entity.CraftPlayer"); + if (playerImplementation == null) { + isRealPlayerCheck = p -> true; + } else { + isRealPlayerCheck = p -> p.getClass() == playerImplementation; + } try { fieldPlayer = PlayerEvent.class.getDeclaredField("player"); fieldPlayer.setAccessible(true); @@ -346,6 +356,9 @@ public class PlayerEventListener extends PlotListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onTeleport(PlayerTeleportEvent event) { + if (!isRealPlayerCheck.test(event.getPlayer())) { + return; + } Player player = event.getPlayer(); BukkitPlayer pp = BukkitUtil.getPlayer(player); Plot lastPlot = pp.getMeta(PlotPlayer.META_LAST_PLOT); @@ -452,6 +465,9 @@ public class PlayerEventListener extends PlotListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void playerMove(PlayerMoveEvent event) { + if (!isRealPlayerCheck.test(event.getPlayer())) { + return; + } org.bukkit.Location from = event.getFrom(); org.bukkit.Location to = event.getTo(); int x2; @@ -643,6 +659,9 @@ public class PlayerEventListener extends PlotListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onWorldChanged(PlayerChangedWorldEvent event) { + if (!isRealPlayerCheck.test(event.getPlayer())) { + return; + } Player player = event.getPlayer(); BukkitPlayer pp = BukkitUtil.getPlayer(player); // Delete last location