2020-04-16 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-04-11 02:19:18 +02:00
|
|
|
package com.plotsquared.bukkit.util;
|
2015-06-08 12:30:46 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.Player;
|
2015-06-08 12:30:46 +02:00
|
|
|
|
2016-02-14 02:01:18 +01:00
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2020-04-15 21:26:54 +02:00
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.callConstructor;
|
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.callMethod;
|
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.getCbClass;
|
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.getField;
|
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.getNmsClass;
|
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.getUtilClass;
|
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.makeConstructor;
|
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.makeField;
|
|
|
|
import static com.plotsquared.core.util.ReflectionUtils.makeMethod;
|
2018-08-10 20:30:05 +02:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class OfflinePlayerUtil {
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static Player loadPlayer(OfflinePlayer player) {
|
2015-09-13 06:04:31 +02:00
|
|
|
if (player == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (player instanceof Player) {
|
|
|
|
return (Player) player;
|
|
|
|
}
|
2015-06-08 12:30:46 +02:00
|
|
|
return loadPlayer(player.getUniqueId(), player.getName());
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
private static Player loadPlayer(UUID id, String name) {
|
|
|
|
Object server = getMinecraftServer();
|
|
|
|
Object interactManager = newPlayerInteractManager();
|
|
|
|
Object worldServer = getWorldServer();
|
|
|
|
Object profile = newGameProfile(id, name);
|
|
|
|
Class<?> entityPlayerClass = getNmsClass("EntityPlayer");
|
2018-08-10 17:01:10 +02:00
|
|
|
Constructor entityPlayerConstructor =
|
|
|
|
makeConstructor(entityPlayerClass, getNmsClass("MinecraftServer"),
|
|
|
|
getNmsClass("WorldServer"), getUtilClass("com.mojang.authlib.GameProfile"),
|
2016-02-14 02:01:18 +01:00
|
|
|
getNmsClass("PlayerInteractManager"));
|
2018-08-10 17:01:10 +02:00
|
|
|
Object entityPlayer =
|
|
|
|
callConstructor(entityPlayerConstructor, server, worldServer, profile, interactManager);
|
2016-02-14 02:01:18 +01:00
|
|
|
return (Player) getBukkitEntity(entityPlayer);
|
2015-06-08 12:30:46 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
private static Object newGameProfile(UUID id, String name) {
|
|
|
|
Class<?> gameProfileClass = getUtilClass("com.mojang.authlib.GameProfile");
|
2015-09-13 06:04:31 +02:00
|
|
|
if (gameProfileClass == null) { //Before uuids
|
2015-06-08 12:30:46 +02:00
|
|
|
return name;
|
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
Constructor gameProfileConstructor =
|
|
|
|
makeConstructor(gameProfileClass, UUID.class, String.class);
|
2016-03-29 06:56:44 +02:00
|
|
|
if (gameProfileConstructor == null) { //Version has string constructor
|
2015-06-08 12:30:46 +02:00
|
|
|
gameProfileConstructor = makeConstructor(gameProfileClass, String.class, String.class);
|
|
|
|
return callConstructor(gameProfileConstructor, id.toString(), name);
|
2015-09-13 06:04:31 +02:00
|
|
|
} else { //Version has uuid constructor
|
2015-06-08 12:30:46 +02:00
|
|
|
return callConstructor(gameProfileConstructor, id, name);
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
private static Object newPlayerInteractManager() {
|
2016-03-23 02:41:37 +01:00
|
|
|
Object worldServer = getWorldServer();
|
|
|
|
Class<?> playerInteractClass = getNmsClass("PlayerInteractManager");
|
|
|
|
Class<?> worldClass = getNmsClass("World");
|
|
|
|
Constructor c = makeConstructor(playerInteractClass, worldClass);
|
2015-06-08 12:30:46 +02:00
|
|
|
return callConstructor(c, worldServer);
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2020-04-08 22:52:13 +02:00
|
|
|
public static Object getWorldServerNew() {
|
|
|
|
Object server = getMinecraftServer();
|
|
|
|
Class<?> minecraftServerClass = getNmsClass("MinecraftServer");
|
|
|
|
Class<?> dimensionManager = getNmsClass("DimensionManager");
|
|
|
|
Object overworld = getField(makeField(dimensionManager, "OVERWORLD"), null);
|
|
|
|
Method getWorldServer = makeMethod(minecraftServerClass, "getWorldServer", dimensionManager);
|
|
|
|
return callMethod(getWorldServer, server, overworld);
|
|
|
|
}
|
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
private static Object getWorldServer() {
|
2016-03-23 02:41:37 +01:00
|
|
|
Object server = getMinecraftServer();
|
|
|
|
Class<?> minecraftServerClass = getNmsClass("MinecraftServer");
|
|
|
|
Method getWorldServer = makeMethod(minecraftServerClass, "getWorldServer", int.class);
|
2020-04-08 22:52:13 +02:00
|
|
|
Object o;
|
|
|
|
try {
|
|
|
|
o = callMethod(getWorldServer, server, 0);
|
|
|
|
} catch (final RuntimeException e) {
|
|
|
|
o = getWorldServerNew();
|
|
|
|
}
|
|
|
|
return o;
|
2015-06-08 12:30:46 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-06-08 12:30:46 +02:00
|
|
|
//NMS Utils
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
private static Object getMinecraftServer() {
|
2015-06-08 12:30:46 +02:00
|
|
|
return callMethod(makeMethod(getCbClass("CraftServer"), "getServer"), Bukkit.getServer());
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
private static Entity getBukkitEntity(Object o) {
|
|
|
|
Method getBukkitEntity = makeMethod(o.getClass(), "getBukkitEntity");
|
2015-06-08 12:30:46 +02:00
|
|
|
return callMethod(getBukkitEntity, o);
|
|
|
|
}
|
|
|
|
}
|