Make inventory lookup of offline players asynchron

This commit is contained in:
Max Lee 2015-04-03 01:30:42 +02:00
parent ad0e55cf4c
commit 6c1fb14cda
2 changed files with 86 additions and 46 deletions

View file

@ -18,6 +18,7 @@ package com.lishid.openinv.commands;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -54,7 +55,6 @@ public class OpenEnderPluginCommand implements CommandExecutor {
}
Player player = (Player) sender;
boolean offline = false;
// History management
String history = openEnderHistory.get(player);
@ -64,10 +64,7 @@ public class OpenEnderPluginCommand implements CommandExecutor {
openEnderHistory.put(player, history);
}
// Target selecting
Player target;
String name = "";
final String name;
// Read from history if target is not named
if (args.length < 1) {
@ -83,36 +80,60 @@ public class OpenEnderPluginCommand implements CommandExecutor {
name = args[0];
}
target = this.plugin.getServer().getPlayer(name);
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) {
// Try loading the player's data
target = OpenInv.playerLoader.loadPlayer(name);
if (target == null) {
sender.sendMessage(ChatColor.RED + "Player " + name + " not found!");
return true;
}
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);
}
});
}
});
} else {
openInventory(player, target);
}
if (target != sender && !OpenInv.hasPermission(sender, Permissions.PERM_ENDERCHEST_ALL)) {
sender.sendMessage(ChatColor.RED + "You do not have permission to access other player's enderchest");
return true;
return true;
}
private void openInventory(Player player, Player target) {
if (target == null) {
player.sendMessage(ChatColor.RED + "Player not found!");
return;
}
if (target != player && !OpenInv.hasPermission(player, Permissions.PERM_ENDERCHEST_ALL)) {
player.sendMessage(ChatColor.RED + "You do not have permission to access other player's enderchest");
return;
}
// Record the target
history = target.getName();
openEnderHistory.put(player, history);
openEnderHistory.put(player, target.getName());
// Create the inventory
ISpecialEnderChest chest = OpenInv.enderChests.get(target.getName().toLowerCase());
if (chest == null) {
chest = InternalAccessor.Instance.newSpecialEnderChest(target, !offline);
chest = InternalAccessor.Instance.newSpecialEnderChest(target, target.isOnline());
}
// Open the inventory
player.openInventory(chest.getBukkitInventory());
return true;
return;
}
}

View file

@ -18,6 +18,7 @@ package com.lishid.openinv.commands;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -53,7 +54,6 @@ public class OpenInvPluginCommand implements CommandExecutor {
}
Player player = (Player) sender;
boolean offline = false;
// History management
String history = openInvHistory.get(player);
@ -63,62 +63,81 @@ public class OpenInvPluginCommand implements CommandExecutor {
openInvHistory.put(player, history);
}
// Target selecting
Player target;
String name = "";
final String name;
// Read from history if target is not named
if (args.length < 1) {
name = history;
}
else {
} else {
name = args[0];
}
target = this.plugin.getServer().getPlayer(name);
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) {
// Try loading the player's data
target = OpenInv.playerLoader.loadPlayer(name);
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);
}
});
}
});
} else {
openInventory(player, target);
}
return true;
}
if (target == null) {
sender.sendMessage(ChatColor.RED + "Player " + name + " not found!");
return true;
}
private void openInventory(Player player, Player target) {
if (target == null) {
player.sendMessage(ChatColor.RED + "Player not found!");
return;
}
// Permissions checks
if (!OpenInv.hasPermission(player, Permissions.PERM_OVERRIDE) && OpenInv.hasPermission(target, Permissions.PERM_EXEMPT)) {
sender.sendMessage(ChatColor.RED + target.getDisplayName() + "'s inventory is protected!");
return true;
player.sendMessage(ChatColor.RED + target.getDisplayName() + "'s inventory is protected!");
return;
}
// Crosswork check
if ((!OpenInv.hasPermission(player, Permissions.PERM_CROSSWORLD) && !OpenInv.hasPermission(player, Permissions.PERM_OVERRIDE)) && target.getWorld() != player.getWorld()) {
sender.sendMessage(ChatColor.RED + target.getDisplayName() + " is not in your world!");
return true;
player.sendMessage(ChatColor.RED + target.getDisplayName() + " is not in your world!");
return;
}
// Self-open check
if (!OpenInv.hasPermission(player, Permissions.PERM_OPENSELF) && target.equals(player)) {
sender.sendMessage(ChatColor.RED + "You're not allowed to openinv yourself.");
return true;
player.sendMessage(ChatColor.RED + "You're not allowed to openinv yourself.");
return;
}
// Record the target
history = target.getName();
openInvHistory.put(player, history);
openInvHistory.put(player, target.getName());
// Create the inventory
ISpecialPlayerInventory inv = OpenInv.inventories.get(target.getName().toLowerCase());
if (inv == null) {
inv = InternalAccessor.Instance.newSpecialPlayerInventory(target, !offline);
inv = InternalAccessor.Instance.newSpecialPlayerInventory(target, target.isOnline());
}
// Open the inventory
player.openInventory(inv.getBukkitInventory());
return true;
return;
}
}