1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-25 02:18:50 +00:00

fix: 修复获取在线玩家错误的问题

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-28 13:53:27 +08:00
parent 1bf555bea4
commit 96f266d430

View File

@ -1,96 +1,101 @@
package pw.yumc.YumCore.bukkit.compatible; package pw.yumc.YumCore.bukkit.compatible;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.Server; import org.bukkit.Server;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
/** /**
* Bukkit兼容类 * Bukkit兼容类
* *
* @since 2016年7月23日 下午1:04:56 * @since 2016年7月23日 下午1:04:56
* @author * @author
*/ */
public class C { public class C {
/** /**
* Bukkit Player兼容类 * Bukkit Player兼容类
* *
* @since 2016年7月23日 下午4:33:40 * @since 2016年7月23日 下午4:33:40
* @author * @author
*/ */
public static class Player { public static class Player {
private static Class<?> gameProfileClass; private static Class<?> gameProfileClass;
private static Constructor<?> gameProfileConstructor; private static Constructor<?> gameProfileConstructor;
private static Constructor<?> craftOfflinePlayerConstructor; private static Constructor<?> craftOfflinePlayerConstructor;
private static Method getOnlinePlayers; private static Method getOnlinePlayers;
static { static {
try { try {
// getOnlinePlayers start // getOnlinePlayers start
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers"); getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
if (getOnlinePlayers.getReturnType() != Player[].class) { if (getOnlinePlayers.getReturnType() != Player[].class) {
getOnlinePlayers = Bukkit.class.getDeclaredMethod("_INVALID_getOnlinePlayers"); for (final Method method : Bukkit.class.getDeclaredMethods()) {
} if (method.getReturnType() == Player[].class && method.getName().endsWith("getOnlinePlayers")) {
// getOnlinePlayers end getOnlinePlayers = method;
// getOfflinePlayer start }
try { }
gameProfileClass = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile"); }
} catch (final ClassNotFoundException e) { // getOnlinePlayers end
try { // getOfflinePlayer start
gameProfileClass = Class.forName("com.mojang.authlib.GameProfile"); try {
} catch (final ClassNotFoundException e1) { gameProfileClass = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile");
} } catch (final ClassNotFoundException e) {
} try {
gameProfileConstructor = gameProfileClass.getDeclaredConstructor(new Class[] { UUID.class, String.class }); gameProfileClass = Class.forName("com.mojang.authlib.GameProfile");
gameProfileConstructor.setAccessible(true); } catch (final ClassNotFoundException e1) {
final Class<? extends Server> craftServer = Bukkit.getServer().getClass(); }
final Class<?> craftOfflinePlayer = Class.forName(craftServer.getName().replace("CraftServer", "CraftOfflinePlayer")); }
craftOfflinePlayerConstructor = craftOfflinePlayer.getDeclaredConstructor(new Class[] { craftServer, gameProfileClass }); gameProfileConstructor = gameProfileClass.getDeclaredConstructor(new Class[] { UUID.class, String.class });
craftOfflinePlayerConstructor.setAccessible(true); gameProfileConstructor.setAccessible(true);
// getOfflinePlayer end final Class<? extends Server> craftServer = Bukkit.getServer().getClass();
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) { final Class<?> craftOfflinePlayer = Class.forName(craftServer.getName().replace("CraftServer", "CraftOfflinePlayer"));
Log.warning(Player.class.getSimpleName() + "兼容性工具初始化失败 可能造成部分功能不可用!"); craftOfflinePlayerConstructor = craftOfflinePlayer.getDeclaredConstructor(new Class[] { craftServer, gameProfileClass });
} craftOfflinePlayerConstructor.setAccessible(true);
} // getOfflinePlayer end
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) {
/** Log.warning(Player.class.getSimpleName() + "兼容性工具初始化失败 可能造成部分功能不可用!");
* 获取离线玩家(跳过网络获取) e.printStackTrace();
* }
* @param playerName }
* 玩家名称
* @return {@link OfflinePlayer} /**
*/ * 获取离线玩家(跳过网络获取)
public static OfflinePlayer getOfflinePlayer(final String playerName) { *
try { * @param playerName
final Object gameProfile = gameProfileConstructor.newInstance(new Object[] { UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(Charsets.UTF_8)), playerName }); * 玩家名称
final Object offlinePlayer = craftOfflinePlayerConstructor.newInstance(new Object[] { Bukkit.getServer(), gameProfile }); * @return {@link OfflinePlayer}
return (OfflinePlayer) offlinePlayer; */
} catch (final Throwable var5) { public static OfflinePlayer getOfflinePlayer(final String playerName) {
return Bukkit.getOfflinePlayer(playerName); try {
} final Object gameProfile = gameProfileConstructor.newInstance(new Object[] { UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(Charsets.UTF_8)), playerName });
} final Object offlinePlayer = craftOfflinePlayerConstructor.newInstance(new Object[] { Bukkit.getServer(), gameProfile });
return (OfflinePlayer) offlinePlayer;
/** } catch (final Throwable var5) {
* 获取在线玩家 return Bukkit.getOfflinePlayer(playerName);
* }
* @return 在线玩家 }
*/
public static Collection<? extends org.bukkit.entity.Player> getOnlinePlayers() { /**
try { * 获取在线玩家
return Arrays.asList((org.bukkit.entity.Player[]) getOnlinePlayers.invoke(null)); *
} catch (final Throwable e) { * @return 在线玩家
return Bukkit.getOnlinePlayers(); */
} public static Collection<? extends org.bukkit.entity.Player> getOnlinePlayers() {
} try {
} return Arrays.asList((org.bukkit.entity.Player[]) getOnlinePlayers.invoke(null));
} } catch (final Throwable e) {
return Bukkit.getOnlinePlayers();
}
}
}
}