Fixed offline inventory access.

This commit is contained in:
lishid 2014-07-29 06:53:14 -04:00
parent 9271c50a84
commit c2c7996da5
3 changed files with 93 additions and 289 deletions

View file

@ -17,170 +17,72 @@
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;
import com.lishid.openinv.OpenInv;
import com.lishid.openinv.internal.IPlayerDataManager;
import net.minecraft.util.com.mojang.authlib.GameProfile;
/**
* @author lishid, JuicyDev (reflection)
*/
public class PlayerDataManager {
//Volatile
import net.minecraft.server.v1_7_R3.*;
private Plugin plugin;
private String versionString;
private int version;
private static String playerFolderName;
import org.bukkit.craftbukkit.v1_7_R3.*;
public PlayerDataManager(Plugin plugin) {
this.plugin = plugin;
this.versionString = this.plugin
.getServer()
.getClass()
.getPackage()
.getName()
.substring(
this.plugin.getServer().getClass().getPackage()
.getName().lastIndexOf('.') + 1);
public class PlayerDataManager implements IPlayerDataManager {
public Player loadPlayer(String name) {
try {
this.version = Integer.valueOf(versionString.replace("v", "")
.replace("_", "").replace("R", ""));
} catch (NumberFormatException e) { // Fallback
this.version = 173;
}
playerFolderName = version >= 173 ? "playerdata" : "players";
}
UUID uuid = matchUser(name);
if (uuid == null) {
return null;
}
/**
* @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()) {
// 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)
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)));
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));
// 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();
}
return null;
}
/**
* @param name
* Name of the player
* @return Instance of the player (null if doesn't exist)
*/
public Player loadPlayer(String name) {
try {
UUID uuid = matchUser(name);
if (uuid == null)
return null;
Player target = loadPlayer(uuid);
if (target != null) {
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();

View file

@ -17,170 +17,72 @@
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;
import com.lishid.openinv.OpenInv;
import com.lishid.openinv.internal.IPlayerDataManager;
import net.minecraft.util.com.mojang.authlib.GameProfile;
/**
* @author lishid, JuicyDev (reflection)
*/
public class PlayerDataManager {
//Volatile
import net.minecraft.server.v1_7_R4.*;
private Plugin plugin;
private String versionString;
private int version;
private static String playerFolderName;
import org.bukkit.craftbukkit.v1_7_R4.*;
public PlayerDataManager(Plugin plugin) {
this.plugin = plugin;
this.versionString = this.plugin
.getServer()
.getClass()
.getPackage()
.getName()
.substring(
this.plugin.getServer().getClass().getPackage()
.getName().lastIndexOf('.') + 1);
public class PlayerDataManager implements IPlayerDataManager {
public Player loadPlayer(String name) {
try {
this.version = Integer.valueOf(versionString.replace("v", "")
.replace("_", "").replace("R", ""));
} catch (NumberFormatException e) { // Fallback
this.version = 173;
}
playerFolderName = version >= 173 ? "playerdata" : "players";
}
UUID uuid = matchUser(name);
if (uuid == null) {
return null;
}
/**
* @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()) {
// 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)
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)));
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));
// 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();
}
return null;
}
/**
* @param name
* Name of the player
* @return Instance of the player (null if doesn't exist)
*/
public Player loadPlayer(String name) {
try {
UUID uuid = matchUser(name);
if (uuid == null)
return null;
Player target = loadPlayer(uuid);
if (target != null) {
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();

View file

@ -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.