2015-07-30 19:24:01 +02:00
|
|
|
package com.plotsquared.bukkit.util;
|
2015-06-08 12:30:46 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
import static com.intellectualcrafters.plot.util.ReflectionUtils.callConstructor;
|
|
|
|
import static com.intellectualcrafters.plot.util.ReflectionUtils.callMethod;
|
|
|
|
import static com.intellectualcrafters.plot.util.ReflectionUtils.getCbClass;
|
|
|
|
import static com.intellectualcrafters.plot.util.ReflectionUtils.getNmsClass;
|
|
|
|
import static com.intellectualcrafters.plot.util.ReflectionUtils.getUtilClass;
|
|
|
|
import static com.intellectualcrafters.plot.util.ReflectionUtils.makeConstructor;
|
|
|
|
import static com.intellectualcrafters.plot.util.ReflectionUtils.makeMethod;
|
2015-06-08 12:30:46 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
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
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
public class OfflinePlayerUtil
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
public static Player loadPlayer(final String name)
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
return loadPlayer(Bukkit.getOfflinePlayer(name));
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
public static Player loadPlayer(final UUID id)
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
return loadPlayer(Bukkit.getOfflinePlayer(id));
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
public static Player loadPlayer(final OfflinePlayer player)
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
private static Player loadPlayer(final UUID id, final String name)
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
final Object server = getMinecraftServer();
|
|
|
|
final Object interactManager = newPlayerInteractManager();
|
|
|
|
final Object worldServer = getWorldServer();
|
|
|
|
final Object profile = newGameProfile(id, name);
|
|
|
|
final Class<?> entityPlayerClass = getNmsClass("EntityPlayer");
|
2015-09-11 12:09:22 +02:00
|
|
|
final Constructor entityPlayerConstructor = makeConstructor(entityPlayerClass, getNmsClass("MinecraftServer"), getNmsClass("WorldServer"), getUtilClass("com.mojang.authlib.GameProfile"),
|
|
|
|
getNmsClass("PlayerInteractManager"));
|
2015-06-08 12:30:46 +02:00
|
|
|
final Object entityPlayer = callConstructor(entityPlayerConstructor, server, worldServer, profile, interactManager);
|
|
|
|
final Player player = (Player) getBukkitEntity(entityPlayer);
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
private static Object newGameProfile(final UUID id, final String name)
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
final Class<?> gameProfileClass = getUtilClass("com.mojang.authlib.GameProfile");
|
2015-09-11 12:09:22 +02:00
|
|
|
if (gameProfileClass == null)
|
|
|
|
{ //Before uuids
|
2015-06-08 12:30:46 +02:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
Constructor gameProfileConstructor = null;
|
|
|
|
gameProfileConstructor = makeConstructor(gameProfileClass, UUID.class, String.class);
|
2015-09-11 12:09:22 +02:00
|
|
|
if (gameProfileConstructor == null)
|
|
|
|
{ //Verson 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-11 12:09:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ //Version has uuid constructor
|
2015-06-08 12:30:46 +02:00
|
|
|
return callConstructor(gameProfileConstructor, id, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
private static Object newPlayerInteractManager()
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
final Object worldServer = getWorldServer();
|
|
|
|
final Class<?> playerInteractClass = getNmsClass("PlayerInteractManager");
|
|
|
|
final Class<?> worldClass = getNmsClass("World");
|
|
|
|
final Constructor c = makeConstructor(playerInteractClass, worldClass);
|
|
|
|
return callConstructor(c, worldServer);
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
private static Object getWorldServer()
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
final Object server = getMinecraftServer();
|
|
|
|
final Class<?> minecraftServerClass = getNmsClass("MinecraftServer");
|
|
|
|
final Method getWorldServer = makeMethod(minecraftServerClass, "getWorldServer", int.class);
|
|
|
|
return callMethod(getWorldServer, server, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//NMS Utils
|
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
private static Object getMinecraftServer()
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
return callMethod(makeMethod(getCbClass("CraftServer"), "getServer"), Bukkit.getServer());
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
private static Entity getBukkitEntity(final Object o)
|
|
|
|
{
|
2015-06-08 12:30:46 +02:00
|
|
|
final Method getBukkitEntity = makeMethod(o.getClass(), "getBukkitEntity");
|
|
|
|
return callMethod(getBukkitEntity, o);
|
|
|
|
}
|
|
|
|
}
|