Migrate to UUID usage
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -28,88 +29,93 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.internal.ISpecialEnderChest;
|
||||
import com.lishid.openinv.internal.InternalAccessor;
|
||||
import com.lishid.openinv.internal.SpecialEnderChest;
|
||||
import com.lishid.openinv.utils.UUIDUtil;
|
||||
|
||||
public class OpenEnderPluginCommand implements CommandExecutor {
|
||||
private final OpenInv plugin;
|
||||
public static Map<Player, String> openEnderHistory = new ConcurrentHashMap<Player, String>();
|
||||
public static final Map<UUID, UUID> openEnderHistory = new ConcurrentHashMap<UUID, UUID>();
|
||||
|
||||
public OpenEnderPluginCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_ENDERCHEST)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to access player enderchest");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("?")) {
|
||||
OpenInv.ShowHelp((Player) sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
// History management
|
||||
String history = openEnderHistory.get(player);
|
||||
|
||||
if (history == null || history == "") {
|
||||
history = player.getName();
|
||||
openEnderHistory.put(player, history);
|
||||
}
|
||||
|
||||
final String name;
|
||||
|
||||
// Read from history if target is not named
|
||||
if (args.length < 1) {
|
||||
if (history != null && history != "") {
|
||||
name = history;
|
||||
}
|
||||
else {
|
||||
sender.sendMessage(ChatColor.RED + "OpenEnder history is empty!");
|
||||
if (command.getName().equalsIgnoreCase("openender")) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
name = args[0];
|
||||
}
|
||||
|
||||
final String playername = player.getName();
|
||||
Player target = plugin.getServer().getPlayer(name);
|
||||
// Targeted player was not found online, start asynchron lookup in files
|
||||
if (target == null) {
|
||||
sender.sendMessage(ChatColor.GREEN + "Starting inventory lookup.");
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Try loading the player's data asynchronly
|
||||
final Player target = OpenInv.playerLoader.loadPlayer(name);
|
||||
// Back to synchron to send messages and display inventory
|
||||
Bukkit.getScheduler().runTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Player player = Bukkit.getPlayer(playername);
|
||||
// If sender is no longer online after loading the target. Abort!
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
openInventory(player, target);
|
||||
}
|
||||
});
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_ENDERCHEST)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to access player enderchest");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("?")) {
|
||||
OpenInv.ShowHelp((Player) sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
// History management
|
||||
UUID history = openEnderHistory.get(player.getUniqueId());
|
||||
|
||||
if (history == null) {
|
||||
history = player.getUniqueId();
|
||||
openEnderHistory.put(player.getUniqueId(), history);
|
||||
}
|
||||
|
||||
final UUID uuid;
|
||||
|
||||
// Read from history if target is not named
|
||||
if (args.length < 1) {
|
||||
if (history != null) {
|
||||
uuid = history;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
openInventory(player, target);
|
||||
else {
|
||||
sender.sendMessage(ChatColor.RED + "OpenEnder history is empty!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uuid = UUIDUtil.getUUIDOf(args[0]);
|
||||
}
|
||||
|
||||
final UUID playerUUID = player.getUniqueId();
|
||||
Player target = Bukkit.getPlayer(uuid);
|
||||
// Targeted player was not found online, start asynchron lookup in files
|
||||
if (target == null) {
|
||||
sender.sendMessage(ChatColor.GREEN + "Starting inventory lookup.");
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Try loading the player's data asynchronly
|
||||
final Player target = OpenInv.playerLoader.loadPlayer(uuid);
|
||||
// Back to synchron to send messages and display inventory
|
||||
Bukkit.getScheduler().runTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Player player = Bukkit.getPlayer(playerUUID);
|
||||
// If sender is no longer online after loading the target. Abort!
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
openInventory(player, target);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
openInventory(player, target);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void openInventory(Player player, Player target) {
|
||||
@@ -136,17 +142,15 @@ public class OpenEnderPluginCommand implements CommandExecutor {
|
||||
}
|
||||
|
||||
// Record the target
|
||||
openEnderHistory.put(player, target.getName());
|
||||
openEnderHistory.put(player.getUniqueId(), target.getUniqueId());
|
||||
|
||||
// Create the inventory
|
||||
ISpecialEnderChest chest = OpenInv.enderChests.get(target.getName().toLowerCase());
|
||||
SpecialEnderChest chest = OpenInv.enderChests.get(target.getUniqueId());
|
||||
if (chest == null) {
|
||||
chest = InternalAccessor.Instance.newSpecialEnderChest(target, target.isOnline());
|
||||
chest = new SpecialEnderChest(target, target.isOnline());
|
||||
}
|
||||
|
||||
// Open the inventory
|
||||
player.openInventory(chest.getBukkitInventory());
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user