diff --git a/src/com/lishid/openinv/internal/v1_7_R3/PlayerDataManager.java b/src/com/lishid/openinv/internal/v1_7_R3/PlayerDataManager.java index dc67739..2976b83 100644 --- a/src/com/lishid/openinv/internal/v1_7_R3/PlayerDataManager.java +++ b/src/com/lishid/openinv/internal/v1_7_R3/PlayerDataManager.java @@ -17,171 +17,73 @@ package com.lishid.openinv.internal.v1_7_R3; import java.io.File; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; import java.util.UUID; -import net.minecraft.util.com.mojang.authlib.GameProfile; - import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import org.bukkit.World; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import com.lishid.openinv.utils.ReflectionUtil; -import com.lishid.openinv.utils.ReflectionUtil.PackageType; -import com.lishid.openinv.utils.ReflectionUtil.SubPackageType; - -/** - * @author lishid, JuicyDev (reflection) - */ -public class PlayerDataManager { - - private Plugin plugin; - private String versionString; - private int version; - private static String playerFolderName; - - public PlayerDataManager(Plugin plugin) { - this.plugin = plugin; - - this.versionString = this.plugin - .getServer() - .getClass() - .getPackage() - .getName() - .substring( - this.plugin.getServer().getClass().getPackage() - .getName().lastIndexOf('.') + 1); - try { - this.version = Integer.valueOf(versionString.replace("v", "") - .replace("_", "").replace("R", "")); - } catch (NumberFormatException e) { // Fallback - this.version = 173; - } - playerFolderName = version >= 173 ? "playerdata" : "players"; - } - - /** - * @param uuid - * UUID of the player - * @return Instance of the player (null if doesn't exist) - */ - public Player loadPlayer(UUID uuid) { - try { - File playerFolder = new File( - ((World) Bukkit.getWorlds().get(0)).getWorldFolder(), - playerFolderName); - if (!playerFolder.exists()) { - return null; - } - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); - if (player == null) - return null; - - GameProfile profile = new GameProfile(player.getUniqueId(), - player.getName()); - - Class obc_CraftServer = ReflectionUtil.getClass("CraftServer", - PackageType.CRAFTBUKKIT); - Object craftServer = obc_CraftServer.cast(Bukkit.getServer()); - Method m_getServer = ReflectionUtil.getMethod(obc_CraftServer, - "getServer"); - Class nms_MinecraftServer = ReflectionUtil.getClass( - "MinecraftServer", PackageType.MINECRAFT_SERVER); - Object minecraftServer = m_getServer.invoke(craftServer); - - Class nms_EntityPlayer = ReflectionUtil.getClass("EntityPlayer", - PackageType.MINECRAFT_SERVER); - Class nms_WorldServer = ReflectionUtil.getClass("WorldServer", - PackageType.MINECRAFT_SERVER); - Class nms_PlayerInteractManager = ReflectionUtil.getClass( - "PlayerInteractManager", PackageType.MINECRAFT_SERVER); - Object worldServer = ReflectionUtil.getMethod(nms_MinecraftServer, - "getWorldServer", Integer.class).invoke(minecraftServer, 0); - - Constructor c_EntityPlayer = ReflectionUtil.getConstructor( - nms_EntityPlayer, nms_MinecraftServer, nms_WorldServer, - GameProfile.class, nms_PlayerInteractManager); - Constructor c_PlayerInteractManager = ReflectionUtil - .getConstructor(nms_PlayerInteractManager, nms_WorldServer); - Object playerInteractManager = c_PlayerInteractManager - .newInstance(worldServer); - - Object entityPlayer = c_EntityPlayer.newInstance(minecraftServer, - worldServer, profile, playerInteractManager); - - Class obc_CraftPlayer = ReflectionUtil.getClass("CraftPlayer", - SubPackageType.ENTITY); - Method m_getBukkitEntity = ReflectionUtil.getMethod( - nms_EntityPlayer, "getBukkitEntity"); - - Player target = entityPlayer == null ? null - : (Player) obc_CraftPlayer.cast(m_getBukkitEntity - .invoke(entityPlayer)); - - if (target != null) { - target.loadData(); - return target; - } - } catch (Exception e) { - e.printStackTrace(); - } - - return null; - } - - /** - * @param name - * Name of the player - * @return Instance of the player (null if doesn't exist) - */ +import com.lishid.openinv.OpenInv; +import com.lishid.openinv.internal.IPlayerDataManager; +import net.minecraft.util.com.mojang.authlib.GameProfile; + +//Volatile +import net.minecraft.server.v1_7_R3.*; + +import org.bukkit.craftbukkit.v1_7_R3.*; + +public class PlayerDataManager implements IPlayerDataManager { public Player loadPlayer(String name) { try { UUID uuid = matchUser(name); - if (uuid == null) + if (uuid == null) { return null; - - Player target = loadPlayer(uuid); + } + + // Default player folder + File playerfolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "playerdata"); + if (!playerfolder.exists()) { + return null; + } + + OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); + if (player == null) { + return null; + } + GameProfile profile = new GameProfile(uuid, player.getName()); + MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); + // Create an entity to load the player data + EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), profile, new PlayerInteractManager(server.getWorldServer(0))); + + // Get the bukkit entity + Player target = (entity == null) ? null : entity.getBukkitEntity(); if (target != null) { + // Load data + target.loadData(); + // Return the entity return target; } - } catch (Exception e) { - e.printStackTrace(); } - + catch (Exception e) { + OpenInv.log(e); + } + return null; } - + private static UUID matchUser(String search) { - File playerFolder = new File( - ((World) Bukkit.getWorlds().get(0)).getWorldFolder(), - playerFolderName); - if (!playerFolder.exists() || !playerFolder.isDirectory()) - return null; - if (search == null) - return null; - UUID found = null; - + String lowerSearch = search.toLowerCase(); int delta = 2147483647; - - for (File f : playerFolder.listFiles()) { - if (!f.getName().endsWith(".dat")) - continue; - String uuidString = f.getName().substring(0, - f.getName().length() - 4); - UUID uuid = UUID.fromString(uuidString); - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); + + OfflinePlayer[] offlinePlayers = Bukkit.getOfflinePlayers(); + for (OfflinePlayer player : offlinePlayers) { String name = player.getName(); - + if (name.equalsIgnoreCase(search)) - return uuid; - + return player.getUniqueId(); + if (name.toLowerCase().startsWith(lowerSearch)) { int curDelta = name.length() - lowerSearch.length(); if (curDelta < delta) { @@ -193,7 +95,7 @@ public class PlayerDataManager { } } } - + return found; } } diff --git a/src/com/lishid/openinv/internal/v1_7_R4/PlayerDataManager.java b/src/com/lishid/openinv/internal/v1_7_R4/PlayerDataManager.java index 3fe113d..4f26cb9 100644 --- a/src/com/lishid/openinv/internal/v1_7_R4/PlayerDataManager.java +++ b/src/com/lishid/openinv/internal/v1_7_R4/PlayerDataManager.java @@ -17,171 +17,73 @@ package com.lishid.openinv.internal.v1_7_R4; import java.io.File; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; import java.util.UUID; -import net.minecraft.util.com.mojang.authlib.GameProfile; - import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import org.bukkit.World; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import com.lishid.openinv.utils.ReflectionUtil; -import com.lishid.openinv.utils.ReflectionUtil.PackageType; -import com.lishid.openinv.utils.ReflectionUtil.SubPackageType; - -/** - * @author lishid, JuicyDev (reflection) - */ -public class PlayerDataManager { - - private Plugin plugin; - private String versionString; - private int version; - private static String playerFolderName; - - public PlayerDataManager(Plugin plugin) { - this.plugin = plugin; - - this.versionString = this.plugin - .getServer() - .getClass() - .getPackage() - .getName() - .substring( - this.plugin.getServer().getClass().getPackage() - .getName().lastIndexOf('.') + 1); - try { - this.version = Integer.valueOf(versionString.replace("v", "") - .replace("_", "").replace("R", "")); - } catch (NumberFormatException e) { // Fallback - this.version = 173; - } - playerFolderName = version >= 173 ? "playerdata" : "players"; - } - - /** - * @param uuid - * UUID of the player - * @return Instance of the player (null if doesn't exist) - */ - public Player loadPlayer(UUID uuid) { - try { - File playerFolder = new File( - ((World) Bukkit.getWorlds().get(0)).getWorldFolder(), - playerFolderName); - if (!playerFolder.exists()) { - return null; - } - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); - if (player == null) - return null; - - GameProfile profile = new GameProfile(player.getUniqueId(), - player.getName()); - - Class obc_CraftServer = ReflectionUtil.getClass("CraftServer", - PackageType.CRAFTBUKKIT); - Object craftServer = obc_CraftServer.cast(Bukkit.getServer()); - Method m_getServer = ReflectionUtil.getMethod(obc_CraftServer, - "getServer"); - Class nms_MinecraftServer = ReflectionUtil.getClass( - "MinecraftServer", PackageType.MINECRAFT_SERVER); - Object minecraftServer = m_getServer.invoke(craftServer); - - Class nms_EntityPlayer = ReflectionUtil.getClass("EntityPlayer", - PackageType.MINECRAFT_SERVER); - Class nms_WorldServer = ReflectionUtil.getClass("WorldServer", - PackageType.MINECRAFT_SERVER); - Class nms_PlayerInteractManager = ReflectionUtil.getClass( - "PlayerInteractManager", PackageType.MINECRAFT_SERVER); - Object worldServer = ReflectionUtil.getMethod(nms_MinecraftServer, - "getWorldServer", Integer.class).invoke(minecraftServer, 0); - - Constructor c_EntityPlayer = ReflectionUtil.getConstructor( - nms_EntityPlayer, nms_MinecraftServer, nms_WorldServer, - GameProfile.class, nms_PlayerInteractManager); - Constructor c_PlayerInteractManager = ReflectionUtil - .getConstructor(nms_PlayerInteractManager, nms_WorldServer); - Object playerInteractManager = c_PlayerInteractManager - .newInstance(worldServer); - - Object entityPlayer = c_EntityPlayer.newInstance(minecraftServer, - worldServer, profile, playerInteractManager); - - Class obc_CraftPlayer = ReflectionUtil.getClass("CraftPlayer", - SubPackageType.ENTITY); - Method m_getBukkitEntity = ReflectionUtil.getMethod( - nms_EntityPlayer, "getBukkitEntity"); - - Player target = entityPlayer == null ? null - : (Player) obc_CraftPlayer.cast(m_getBukkitEntity - .invoke(entityPlayer)); - - if (target != null) { - target.loadData(); - return target; - } - } catch (Exception e) { - e.printStackTrace(); - } - - return null; - } - - /** - * @param name - * Name of the player - * @return Instance of the player (null if doesn't exist) - */ +import com.lishid.openinv.OpenInv; +import com.lishid.openinv.internal.IPlayerDataManager; +import net.minecraft.util.com.mojang.authlib.GameProfile; + +//Volatile +import net.minecraft.server.v1_7_R4.*; + +import org.bukkit.craftbukkit.v1_7_R4.*; + +public class PlayerDataManager implements IPlayerDataManager { public Player loadPlayer(String name) { try { UUID uuid = matchUser(name); - if (uuid == null) + if (uuid == null) { return null; - - Player target = loadPlayer(uuid); + } + + // Default player folder + File playerfolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "playerdata"); + if (!playerfolder.exists()) { + return null; + } + + OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); + if (player == null) { + return null; + } + GameProfile profile = new GameProfile(uuid, player.getName()); + MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); + // Create an entity to load the player data + EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), profile, new PlayerInteractManager(server.getWorldServer(0))); + + // Get the bukkit entity + Player target = (entity == null) ? null : entity.getBukkitEntity(); if (target != null) { + // Load data + target.loadData(); + // Return the entity return target; } - } catch (Exception e) { - e.printStackTrace(); } - + catch (Exception e) { + OpenInv.log(e); + } + return null; } - + private static UUID matchUser(String search) { - File playerFolder = new File( - ((World) Bukkit.getWorlds().get(0)).getWorldFolder(), - playerFolderName); - if (!playerFolder.exists() || !playerFolder.isDirectory()) - return null; - if (search == null) - return null; - UUID found = null; - + String lowerSearch = search.toLowerCase(); int delta = 2147483647; - - for (File f : playerFolder.listFiles()) { - if (!f.getName().endsWith(".dat")) - continue; - String uuidString = f.getName().substring(0, - f.getName().length() - 4); - UUID uuid = UUID.fromString(uuidString); - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); + + OfflinePlayer[] offlinePlayers = Bukkit.getOfflinePlayers(); + for (OfflinePlayer player : offlinePlayers) { String name = player.getName(); - + if (name.equalsIgnoreCase(search)) - return uuid; - + return player.getUniqueId(); + if (name.toLowerCase().startsWith(lowerSearch)) { int curDelta = name.length() - lowerSearch.length(); if (curDelta < delta) { @@ -193,7 +95,7 @@ public class PlayerDataManager { } } } - + return found; } } diff --git a/src/plugin.yml b/src/plugin.yml index 0815f19..97d541b 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: OpenInv main: com.lishid.openinv.OpenInv -version: 2.1.9 +version: 2.2.2 author: lishid description: > This plugin allows you to open a player's inventory as a chest and interact with it in real time.