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

View file

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