Merge pull request #2890 from MeFisto94/fix-1-16-worldserver-regression

Fixes an OfflinePlayerUtil breakaged caused by NMS Changes
This commit is contained in:
NotMyFault 2020-08-21 18:29:03 +02:00 committed by GitHub
commit 28fc29c3ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 5 deletions

View file

@ -69,6 +69,7 @@ import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.PremiumVerification; import com.plotsquared.core.util.PremiumVerification;
import com.plotsquared.core.util.ReflectionUtils;
import com.plotsquared.core.util.RegExUtil; import com.plotsquared.core.util.RegExUtil;
import com.plotsquared.core.util.entity.EntityCategories; import com.plotsquared.core.util.entity.EntityCategories;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
@ -139,6 +140,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -153,7 +155,15 @@ public class PlayerEventListener extends PlotListener implements Listener {
private PlayerMoveEvent moveTmp; private PlayerMoveEvent moveTmp;
private String internalVersion; private String internalVersion;
private final Predicate<Player> isRealPlayerCheck;
{ {
Class<?> playerImplementation = ReflectionUtils.getCbClass("entity.CraftPlayer");
if (playerImplementation == null) {
isRealPlayerCheck = p -> true;
} else {
isRealPlayerCheck = p -> p.getClass() == playerImplementation;
}
try { try {
fieldPlayer = PlayerEvent.class.getDeclaredField("player"); fieldPlayer = PlayerEvent.class.getDeclaredField("player");
fieldPlayer.setAccessible(true); fieldPlayer.setAccessible(true);
@ -346,6 +356,9 @@ public class PlayerEventListener extends PlotListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onTeleport(PlayerTeleportEvent event) { public void onTeleport(PlayerTeleportEvent event) {
if (!isRealPlayerCheck.test(event.getPlayer())) {
return;
}
Player player = event.getPlayer(); Player player = event.getPlayer();
BukkitPlayer pp = BukkitUtil.getPlayer(player); BukkitPlayer pp = BukkitUtil.getPlayer(player);
Plot lastPlot = pp.getMeta(PlotPlayer.META_LAST_PLOT); 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) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void playerMove(PlayerMoveEvent event) { public void playerMove(PlayerMoveEvent event) {
if (!isRealPlayerCheck.test(event.getPlayer())) {
return;
}
org.bukkit.Location from = event.getFrom(); org.bukkit.Location from = event.getFrom();
org.bukkit.Location to = event.getTo(); org.bukkit.Location to = event.getTo();
int x2; int x2;
@ -643,6 +659,9 @@ public class PlayerEventListener extends PlotListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onWorldChanged(PlayerChangedWorldEvent event) { public void onWorldChanged(PlayerChangedWorldEvent event) {
if (!isRealPlayerCheck.test(event.getPlayer())) {
return;
}
Player player = event.getPlayer(); Player player = event.getPlayer();
BukkitPlayer pp = BukkitUtil.getPlayer(player); BukkitPlayer pp = BukkitUtil.getPlayer(player);
// Delete last location // Delete last location

View file

@ -63,11 +63,11 @@ public class OfflinePlayerUtil {
Object profile = newGameProfile(id, name); Object profile = newGameProfile(id, name);
Class<?> entityPlayerClass = getNmsClass("EntityPlayer"); Class<?> entityPlayerClass = getNmsClass("EntityPlayer");
Constructor entityPlayerConstructor = Constructor entityPlayerConstructor =
makeConstructor(entityPlayerClass, getNmsClass("MinecraftServer"), makeConstructor(entityPlayerClass, getNmsClass("MinecraftServer"),
getNmsClass("WorldServer"), getUtilClass("com.mojang.authlib.GameProfile"), getNmsClass("WorldServer"), getUtilClass("com.mojang.authlib.GameProfile"),
getNmsClass("PlayerInteractManager")); getNmsClass("PlayerInteractManager"));
Object entityPlayer = Object entityPlayer =
callConstructor(entityPlayerConstructor, server, worldServer, profile, interactManager); callConstructor(entityPlayerConstructor, server, worldServer, profile, interactManager);
return (Player) getBukkitEntity(entityPlayer); return (Player) getBukkitEntity(entityPlayer);
} }
@ -97,6 +97,26 @@ public class OfflinePlayerUtil {
return callConstructor(c, worldServer); return callConstructor(c, worldServer);
} }
public static Object getWorldServer116() {
Object server = getMinecraftServer();
Class<?> minecraftServerClass = getNmsClass("MinecraftServer");
Class<?> genericResourceKey = getNmsClass("ResourceKey");
Class<?> minecraftKey = getNmsClass("MinecraftKey");
// MinecraftKey + MinecraftKey -> ResourceKey
Method constructResourceKey = makeMethod(genericResourceKey, "a", minecraftKey, minecraftKey);
// MinecraftKey(String)
Constructor<Object> 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, resourceKey);
}
public static Object getWorldServerNew() { public static Object getWorldServerNew() {
Object server = getMinecraftServer(); Object server = getMinecraftServer();
Class<?> minecraftServerClass = getNmsClass("MinecraftServer"); Class<?> minecraftServerClass = getNmsClass("MinecraftServer");
@ -115,7 +135,11 @@ public class OfflinePlayerUtil {
try { try {
o = callMethod(getWorldServer, server, 0); o = callMethod(getWorldServer, server, 0);
} catch (final RuntimeException e) { } catch (final RuntimeException e) {
o = getWorldServerNew(); try {
o = getWorldServerNew();
} catch (final RuntimeException f) {
o = getWorldServer116();
}
} }
return o; return o;
} }