From 96f266d430decf75d96020297f50483170bc107a Mon Sep 17 00:00:00 2001 From: 502647092 Date: Thu, 28 Jul 2016 13:53:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E7=8E=A9=E5=AE=B6=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/YumCore/bukkit/compatible/C.java | 197 +++++++++--------- 1 file changed, 101 insertions(+), 96 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java b/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java index 479dc97..66eccba 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java @@ -1,96 +1,101 @@ -package pw.yumc.YumCore.bukkit.compatible; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collection; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.Server; - -import com.google.common.base.Charsets; - -import pw.yumc.YumCore.bukkit.Log; - -/** - * Bukkit兼容类 - * - * @since 2016年7月23日 下午1:04:56 - * @author 喵♂呜 - */ -public class C { - /** - * Bukkit Player兼容类 - * - * @since 2016年7月23日 下午4:33:40 - * @author 喵♂呜 - */ - public static class Player { - private static Class gameProfileClass; - private static Constructor gameProfileConstructor; - private static Constructor craftOfflinePlayerConstructor; - - private static Method getOnlinePlayers; - - static { - try { - // getOnlinePlayers start - getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers"); - if (getOnlinePlayers.getReturnType() != Player[].class) { - getOnlinePlayers = Bukkit.class.getDeclaredMethod("_INVALID_getOnlinePlayers"); - } - // getOnlinePlayers end - // getOfflinePlayer start - try { - gameProfileClass = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile"); - } catch (final ClassNotFoundException e) { - try { - gameProfileClass = Class.forName("com.mojang.authlib.GameProfile"); - } catch (final ClassNotFoundException e1) { - } - } - gameProfileConstructor = gameProfileClass.getDeclaredConstructor(new Class[] { UUID.class, String.class }); - gameProfileConstructor.setAccessible(true); - final Class craftServer = Bukkit.getServer().getClass(); - final Class craftOfflinePlayer = Class.forName(craftServer.getName().replace("CraftServer", "CraftOfflinePlayer")); - craftOfflinePlayerConstructor = craftOfflinePlayer.getDeclaredConstructor(new Class[] { craftServer, gameProfileClass }); - craftOfflinePlayerConstructor.setAccessible(true); - // getOfflinePlayer end - } catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) { - Log.warning(Player.class.getSimpleName() + "兼容性工具初始化失败 可能造成部分功能不可用!"); - } - } - - /** - * 获取离线玩家(跳过网络获取) - * - * @param playerName - * 玩家名称 - * @return {@link OfflinePlayer} - */ - public static OfflinePlayer getOfflinePlayer(final String 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 getOnlinePlayers() { - try { - return Arrays.asList((org.bukkit.entity.Player[]) getOnlinePlayers.invoke(null)); - } catch (final Throwable e) { - return Bukkit.getOnlinePlayers(); - } - } - } -} +package pw.yumc.YumCore.bukkit.compatible; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; + +import com.google.common.base.Charsets; + +import pw.yumc.YumCore.bukkit.Log; + +/** + * Bukkit兼容类 + * + * @since 2016年7月23日 下午1:04:56 + * @author 喵♂呜 + */ +public class C { + /** + * Bukkit Player兼容类 + * + * @since 2016年7月23日 下午4:33:40 + * @author 喵♂呜 + */ + public static class Player { + private static Class gameProfileClass; + private static Constructor gameProfileConstructor; + private static Constructor craftOfflinePlayerConstructor; + + private static Method getOnlinePlayers; + + static { + try { + // getOnlinePlayers start + getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers"); + if (getOnlinePlayers.getReturnType() != Player[].class) { + for (final Method method : Bukkit.class.getDeclaredMethods()) { + if (method.getReturnType() == Player[].class && method.getName().endsWith("getOnlinePlayers")) { + getOnlinePlayers = method; + } + } + } + // getOnlinePlayers end + // getOfflinePlayer start + try { + gameProfileClass = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile"); + } catch (final ClassNotFoundException e) { + try { + gameProfileClass = Class.forName("com.mojang.authlib.GameProfile"); + } catch (final ClassNotFoundException e1) { + } + } + gameProfileConstructor = gameProfileClass.getDeclaredConstructor(new Class[] { UUID.class, String.class }); + gameProfileConstructor.setAccessible(true); + final Class craftServer = Bukkit.getServer().getClass(); + final Class craftOfflinePlayer = Class.forName(craftServer.getName().replace("CraftServer", "CraftOfflinePlayer")); + 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 { + 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 getOnlinePlayers() { + try { + return Arrays.asList((org.bukkit.entity.Player[]) getOnlinePlayers.invoke(null)); + } catch (final Throwable e) { + return Bukkit.getOnlinePlayers(); + } + } + } +}