From 007c55bd5fad2ef4cb593a5105055784bee1064c Mon Sep 17 00:00:00 2001 From: Redmancometh Date: Mon, 27 Jul 2015 12:21:56 -0500 Subject: [PATCH 1/2] Fixed stuff. --- .../bukkit/alphachest/AlphaChestPlugin.java | 101 +++---- .../bukkit/alphachest/ChestManager.java | 6 + .../bukkit/alphachest/ChestSaveQueue.java | 60 ++++ .../bukkit/alphachest/InventoryIO.java | 153 ++++++----- .../alphachest/VirtualChestManager.java | 259 ++++++++---------- .../alphachest/commands/ChestCommand.java | 106 +++---- .../commands/ClearChestCommand.java | 94 ++++--- .../alphachest/commands/DisposalCommand.java | 61 ++--- .../alphachest/commands/SaveChestCommand.java | 64 +++-- .../commands/SaveChestsCommand.java | 40 +-- .../alphachest/listeners/PlayerListener.java | 83 +++--- .../alphachest/utils/ShutdownThread.java | 12 + 12 files changed, 555 insertions(+), 484 deletions(-) create mode 100644 src/main/java/net/sradonia/bukkit/alphachest/ChestManager.java create mode 100644 src/main/java/net/sradonia/bukkit/alphachest/ChestSaveQueue.java create mode 100644 src/main/java/net/sradonia/bukkit/alphachest/utils/ShutdownThread.java diff --git a/src/main/java/net/sradonia/bukkit/alphachest/AlphaChestPlugin.java b/src/main/java/net/sradonia/bukkit/alphachest/AlphaChestPlugin.java index 07bc69a..37df435 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/AlphaChestPlugin.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/AlphaChestPlugin.java @@ -1,63 +1,72 @@ package net.sradonia.bukkit.alphachest; import java.io.File; -import java.util.logging.Logger; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import org.bukkit.plugin.java.JavaPlugin; import net.sradonia.bukkit.alphachest.commands.*; import net.sradonia.bukkit.alphachest.listeners.PlayerListener; -public class AlphaChestPlugin extends JavaPlugin { - - private Logger logger; - - private VirtualChestManager chestManager; - +public class AlphaChestPlugin extends JavaPlugin +{ private Teller teller; - @Override - public void onEnable() { - // Save a copy of the default config.yml if one doesn't already exist - saveDefaultConfig(); - - // Initialize some classes and objects - logger = getLogger(); - - File chestFolder = new File(getDataFolder(), "chests"); - chestManager = new VirtualChestManager(chestFolder, logger); - - teller = new Teller(this); - - // Set the plugin's command executors - getCommand("chest").setExecutor(new ChestCommand(chestManager)); - getCommand("clearchest").setExecutor(new ClearChestCommand(chestManager)); - getCommand("disposal").setExecutor(new DisposalCommand(chestManager)); - getCommand("savechests").setExecutor(new SaveChestsCommand(chestManager)); - getCommand("workbench").setExecutor(new WorkbenchCommand()); - - // Register the plugin's events - getServer().getPluginManager().registerEvents(new PlayerListener(this, chestManager), this); - - // Schedule an auto-save task - int autosaveInterval = getConfig().getInt("autosave") * 1200; + public void onEnable() + { + saveDefaultConfig(); + File chestFolder = new File(getDataFolder(), "chests"); + VirtualChestManager.initChestMan(chestFolder); + setTeller(new Teller(this)); + getCommand("chest").setExecutor(new ChestCommand()); + getCommand("clearchest").setExecutor(new ClearChestCommand()); + getCommand("savechests").setExecutor(new SaveChestsCommand()); + getCommand("workbench").setExecutor(new WorkbenchCommand()); + getServer().getPluginManager().registerEvents(new PlayerListener(this), this); + int autosaveInterval = getConfig().getInt("autosave") * 1200; + if (autosaveInterval > 0) + { + getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() + { + public void run() + { + try + { + int savedChests = CompletableFuture.supplyAsync(() -> VirtualChestManager.saveAll()).get(); + if (savedChests > 0 && !getConfig().getBoolean("silentAutosave")) + { + System.out.println("Auto-saved " + savedChests + " chests"); + } + } + catch (InterruptedException | ExecutionException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }, autosaveInterval, autosaveInterval); + } + } - if (autosaveInterval > 0) { - getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { - public void run() { - int savedChests = chestManager.save(); + @Override + public void onDisable() + { + System.out.println("[AlphaChest] Saving inventories!"); + while (true) + { + VirtualChestManager.saveAll(); + break; + } + } - if (savedChests > 0 && !getConfig().getBoolean("silentAutosave")) { - logger.info("Auto-saved " + savedChests + " chests"); - } - } - }, autosaveInterval, autosaveInterval); - } + public Teller getTeller() + { + return teller; } - @Override - public void onDisable() { - int savedChests = chestManager.save(); - logger.info("Saved " + savedChests + " chests"); + public void setTeller(Teller teller) + { + this.teller = teller; } } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/ChestManager.java b/src/main/java/net/sradonia/bukkit/alphachest/ChestManager.java new file mode 100644 index 0000000..540e9fe --- /dev/null +++ b/src/main/java/net/sradonia/bukkit/alphachest/ChestManager.java @@ -0,0 +1,6 @@ +package net.sradonia.bukkit.alphachest; + +public class ChestManager +{ + +} diff --git a/src/main/java/net/sradonia/bukkit/alphachest/ChestSaveQueue.java b/src/main/java/net/sradonia/bukkit/alphachest/ChestSaveQueue.java new file mode 100644 index 0000000..fb512bd --- /dev/null +++ b/src/main/java/net/sradonia/bukkit/alphachest/ChestSaveQueue.java @@ -0,0 +1,60 @@ +package net.sradonia.bukkit.alphachest; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.UUID; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +import org.bukkit.inventory.Inventory; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.Map.Entry; + +public class ChestSaveQueue extends BukkitRunnable +{ + public File dataFolder; + private static final String ymlEx = ".chest.yml"; + + public ChestSaveQueue(File dataFolder) + { + this.dataFolder = dataFolder; + } + + private static BlockingQueue> queue = new ArrayBlockingQueue(500); + int maximumIterations = 50; + + public void saveChests() + { + int tracker = 0; + for (Iterator> iter = queue.iterator(); iter.hasNext();) + { + if (tracker > 50) + { + break; + } + + tracker++; + } + } + + public void saveChest(UUID playerUUID, Inventory chest) + { + final File chestFile = new File(dataFolder + "/chests/", playerUUID + ymlEx); + try + { + InventoryIO.saveToYaml(chest, chestFile); + } + catch (IOException e) + { + System.out.println("Couldn't save chest file: " + chestFile.getName()); + } + } + + @Override + public void run() + { + + } +} diff --git a/src/main/java/net/sradonia/bukkit/alphachest/InventoryIO.java b/src/main/java/net/sradonia/bukkit/alphachest/InventoryIO.java index 4cfa1e2..46f71a2 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/InventoryIO.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/InventoryIO.java @@ -15,103 +15,122 @@ /** * Utility class to store inventories to file and read them back again. */ -public class InventoryIO { +public class InventoryIO +{ /** * Loads an inventory from a plain-text file. * - * @param file the text file to load + * @param file + * the text file to load * @return the loaded inventory - * @throws IOException if the file could not be read + * @throws IOException + * if the file could not be read * @deprecated use {@link #loadFromYaml} instead */ @Deprecated - public static Inventory loadFromTextFile(File file) throws IOException { - final Inventory inventory = Bukkit.getServer().createInventory(null, 6 * 9); - - final BufferedReader in = new BufferedReader(new FileReader(file)); - - String line; - int slot = 0; - - while ((line = in.readLine()) != null) { - if (!line.equals("")) { - final String[] parts = line.split(":"); - - try { - int type = Integer.parseInt(parts[0]); - int amount = Integer.parseInt(parts[1]); - short damage = Short.parseShort(parts[2]); - - if (type != 0) { - inventory.setItem(slot, new ItemStack(type, amount, damage)); - } - } catch (NumberFormatException e) { - // ignore - } - - ++slot; - } - } - - in.close(); - - return inventory; + public static Inventory loadFromTextFile(File file) throws IOException + { + final Inventory inventory = Bukkit.getServer().createInventory(null, 6 * 9); + final BufferedReader in = new BufferedReader(new FileReader(file)); + String line; + int slot = 0; + + while ((line = in.readLine()) != null) + { + if (!line.equals("")) + { + final String[] parts = line.split(":"); + + try + { + int type = Integer.parseInt(parts[0]); + int amount = Integer.parseInt(parts[1]); + short damage = Short.parseShort(parts[2]); + + if (type != 0) + { + inventory.setItem(slot, new ItemStack(type, amount, damage)); + } + } + catch (NumberFormatException e) + { + // ignore + } + + ++slot; + } + } + + in.close(); + + return inventory; } /** * Loads an inventory from a YAML configuration file. * - * @param file the YAML file to load + * @param file + * the YAML file to load * @return the loaded inventory - * @throws IOException if the file could not be read - * @throws InvalidConfigurationException if the file could not be parsed + * @throws IOException + * if the file could not be read + * @throws InvalidConfigurationException + * if the file could not be parsed */ - public static Inventory loadFromYaml(File file) throws IOException, InvalidConfigurationException { - YamlConfiguration yaml = new YamlConfiguration(); - yaml.load(file); - - int inventorySize = yaml.getInt("size", 6 * 9); + public static Inventory loadFromYaml(File file) throws IOException, InvalidConfigurationException + { + YamlConfiguration yaml = new YamlConfiguration(); + yaml.load(file); - Inventory inventory = Bukkit.getServer().createInventory(null, inventorySize); + int inventorySize = yaml.getInt("size", 6 * 9); - ConfigurationSection items = yaml.getConfigurationSection("items"); + Inventory inventory = Bukkit.getServer().createInventory(null, inventorySize); - for (int slot = 0; slot < inventorySize; slot++) { - String slotString = String.valueOf(slot); + ConfigurationSection items = yaml.getConfigurationSection("items"); - if (items.isItemStack(slotString)) { - ItemStack itemStack = items.getItemStack(slotString); - inventory.setItem(slot, itemStack); - } - } + for (int slot = 0; slot < inventorySize; slot++) + { + String slotString = String.valueOf(slot); - return inventory; + if (items.isItemStack(slotString)) + { + ItemStack itemStack = items.getItemStack(slotString); + inventory.setItem(slot, itemStack); + } + } + return inventory; } /** * Saves an inventory to a YAML configuration file. * - * @param inventory the inventory to save - * @param file the YAML file to write - * @throws IOException if the file could not be written + * @param inventory + * the inventory to save + * @param file + * the YAML file to write + * @throws IOException + * if the file could not be written */ - public static void saveToYaml(Inventory inventory, File file) throws IOException { - YamlConfiguration yaml = new YamlConfiguration(); + public static void saveToYaml(Inventory inventory, File file) throws IOException + { + YamlConfiguration yaml = new YamlConfiguration(); - int inventorySize = inventory.getSize(); - yaml.set("size", inventorySize); + int inventorySize = inventory.getSize(); + yaml.set("size", inventorySize); - ConfigurationSection items = yaml.createSection("items"); + ConfigurationSection items = yaml.createSection("items"); - for (int slot = 0; slot < inventorySize; slot++) { - ItemStack stack = inventory.getItem(slot); + for (int slot = 0; slot < inventorySize; slot++) + { + ItemStack stack = inventory.getItem(slot); - if (stack != null) { - items.set(String.valueOf(slot), stack); - } - } + if (stack != null) + { + items.set(String.valueOf(slot), stack); + } + } - yaml.save(file); + yaml.save(file); } } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/VirtualChestManager.java b/src/main/java/net/sradonia/bukkit/alphachest/VirtualChestManager.java index 76beed5..e991ba1 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/VirtualChestManager.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/VirtualChestManager.java @@ -1,176 +1,135 @@ package net.sradonia.bukkit.alphachest; - import java.io.File; -import java.io.FilenameFilter; import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; -import java.util.logging.Level; -import java.util.logging.Logger; - +import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; - -public class VirtualChestManager { - - private static final String YAML_CHEST_EXTENSION = ".chest.yml"; - private static final int YAML_EXTENSION_LENGTH = YAML_CHEST_EXTENSION.length(); - - private final File dataFolder; - private final Logger logger; - private final Map chests = new HashMap<>(); - - public VirtualChestManager(File dataFolder, Logger logger) { - this.logger = logger; - this.dataFolder = dataFolder; - - load(); +public class VirtualChestManager +{ + private static final String ymlEx = ".chest.yml"; + private static File dataFolder; + public static Map chests = new ConcurrentHashMap<>(); + + public static void initChestMan(File dataFolder) + { + VirtualChestManager.dataFolder = dataFolder; } - /** - * Loads all existing chests from the data folder. - */ - private void load() { - dataFolder.mkdirs(); - - FilenameFilter filter = new FilenameFilter() { - public boolean accept(File dir, String name) { - return name.endsWith(YAML_CHEST_EXTENSION); - } - }; - - for (File chestFile : dataFolder.listFiles(filter)) { - String chestFileName = chestFile.getName(); - try { - try { - UUID playerUUID = UUID.fromString(chestFileName.substring(0, chestFileName.length() - YAML_EXTENSION_LENGTH)); - chests.put(playerUUID, InventoryIO.loadFromYaml(chestFile)); - } catch (IllegalArgumentException e) { - // Assume that the filename isn't a UUID, and is therefore an old player-name chest - String playerName = chestFileName.substring(0, chestFileName.length() - YAML_EXTENSION_LENGTH); - boolean flagPlayerNotFound = true; - - for (OfflinePlayer player : Bukkit.getOfflinePlayers()) { - // Search all the known players, load inventory, flag old file for deletion - if (player.getName().equalsIgnoreCase(playerName)) { - flagPlayerNotFound = false; - chests.put(player.getUniqueId(), InventoryIO.loadFromYaml(chestFile)); - chestFile.deleteOnExit(); - } - } - - if (flagPlayerNotFound) { - logger.log(Level.WARNING, "Couldn't load chest file: " + chestFileName); - } - } - } catch (Exception e) { - logger.log(Level.WARNING, "Couldn't load chest file: " + chestFileName); - } - } - - logger.info("Loaded " + chests.size() + " chests"); + public static void addChest(Player p, Inventory i) + { + chests.put(p.getUniqueId(), i); } - /** - * Saves all existing chests to the data folder. - * - * @return the number of successfully written chests - */ - public int save() { - int savedChests = 0; - - dataFolder.mkdirs(); - - Iterator> chestIterator = chests.entrySet().iterator(); - - while (chestIterator.hasNext()) { - final Entry entry = chestIterator.next(); - final UUID playerUUID = entry.getKey(); - final Inventory chest = entry.getValue(); - - final File chestFile = new File(dataFolder, playerUUID.toString() + YAML_CHEST_EXTENSION); - - if (chest == null) { - // Chest got removed, so we have to delete the file. - chestFile.delete(); - chestIterator.remove(); - } else { - try { - // Write the chest file in YAML format - InventoryIO.saveToYaml(chest, chestFile); - - savedChests++; - } catch (IOException e) { - logger.log(Level.WARNING, "Couldn't save chest file: " + chestFile.getName(), e); - } - } - } - - return savedChests; + public static void addChest(UUID p, Inventory i) + { + chests.put(p, i); } - /** - * Saves a specified player's chest to the data folder. - * - * @param playerUUID the UUID of the player to save the chest of - */ - public void saveChest(UUID playerUUID) { - dataFolder.mkdirs(); + public static void saveChest(UUID playerUUID) + { + final String uuidString = playerUUID.toString(); + final Inventory chest = chests.get(playerUUID); + if (chest != null) + { + File chestFile = new File(dataFolder, uuidString + ymlEx); + try + { + InventoryIO.saveToYaml(chest, chestFile); + } + catch (IOException e) + { + System.out.println("Couldn't save chest file: " + chestFile.getName()); + } + } + } - final String uuidString = playerUUID.toString(); - final Inventory chest = chests.get(playerUUID); - final File chestFile = new File(dataFolder, uuidString + YAML_CHEST_EXTENSION); + // Never run this pl0x + public static int saveAll() + { + for (Entry chest : chests.entrySet()) + { + try + { + File chestFile = new File(dataFolder, chest.getKey() + ymlEx); + InventoryIO.saveToYaml(chest.getValue(), chestFile); + } + catch (Exception e) + { + System.out.println("Couldn't save chest file: " + chest.getKey()); + continue; + } + } + return chests.size(); + } - if (chest == null) { - // Chest got removed, so we have to delete the file. - chestFile.delete(); - } else { - try { - // Write the chest file in YAML format - InventoryIO.saveToYaml(chest, chestFile); - } catch (IOException e) { - logger.log(Level.WARNING, "Couldn't save chest file: " + chestFile.getName(), e); - } - } + public static void removeEntry(Player p) + { + chests.remove(p.getUniqueId()); } - /** - * Gets a player's virtual chest. - * - * @param playerUUID the UUID of the player - * @return the player's virtual chest. - */ - public Inventory getChest(UUID playerUUID) { - Inventory chest = chests.get(playerUUID); + public static void removeChest(Player p) + { + chests.put(p.getUniqueId(), Bukkit.createInventory(null, 36)); + } - if (chest == null) { - chest = Bukkit.getServer().createInventory(null, 6 * 9); - chests.put(playerUUID, chest); - } + public static void removeChest(UUID uuid) + { - return chest; } - /** - * Clears a player's virtual chest. - * - * @param playerUUID the UUID of the player - */ - public void removeChest(UUID playerUUID) { - // Put a null to the map so we remember to delete the file when saving! - chests.put(playerUUID, null); + public static void setChest(Player p) + { + Inventory chest = chests.get(p.getUniqueId()); + if (chest == null) + { + try + { + File chestFile = new File(dataFolder, p.getUniqueId() + ymlEx); + if (chestFile.exists()) + { + chests.put(p.getUniqueId(), InventoryIO.loadFromYaml(chestFile)); + } + else + { + Inventory newInv = Bukkit.createInventory(null, 36); + chests.put(p.getUniqueId(), newInv); + InventoryIO.saveToYaml(newInv, chestFile); + } + } + catch (Throwable t) + { + t.printStackTrace(); + } + } } - /** - * Gets the number of virtual chests. - * - * @return the number of virtual chests - */ - public int getChestCount() { - return chests.size(); + public static Inventory getChest(UUID uuid) + { + Inventory chest = chests.get(uuid); + if (chest == null) + { + try + { + File chestFile = new File(dataFolder, uuid + ymlEx); + if (chestFile.exists()) + { + chests.put(uuid, InventoryIO.loadFromYaml(chestFile)); + } + else + { + chestFile.createNewFile(); + } + } + catch (Throwable t) + { + t.printStackTrace(); + } + } + return chest; } + } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/commands/ChestCommand.java b/src/main/java/net/sradonia/bukkit/alphachest/commands/ChestCommand.java index de832c4..d2e1193 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/commands/ChestCommand.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/commands/ChestCommand.java @@ -13,63 +13,71 @@ import net.sradonia.bukkit.alphachest.VirtualChestManager; import net.sradonia.bukkit.alphachest.utils.BukkitUtil; -public class ChestCommand implements CommandExecutor { - - private final VirtualChestManager chestManager; - - public ChestCommand(VirtualChestManager chestManager) { - this.chestManager = chestManager; - } - +public class ChestCommand implements CommandExecutor +{ @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (command.getName().equalsIgnoreCase("chest")) { - // Make sure the sender is a player - if (!(sender instanceof Player)) { - Teller.tell(sender, Type.ERROR, "Only players are able to open chests."); - return true; - } + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) + { + if (command.getName().equalsIgnoreCase("chest")) + { + // Make sure the sender is a player + if (!(sender instanceof Player)) + { + Teller.tell(sender, Type.ERROR, "Only players are able to open chests."); + return true; + } - Player player = (Player) sender; + Player player = (Player) sender; - // Prevent opening of the chest in Creative Mode - if (player.getGameMode().equals(GameMode.CREATIVE) && !player.hasPermission("alphachest.chest.creativeMode")) { - Teller.tell(sender, Type.ERROR, "You are not allowed to open your chest in Creative Mode!"); - return true; - } + // Prevent opening of the chest in Creative Mode + if (player.getGameMode().equals(GameMode.CREATIVE) && !player.hasPermission("alphachest.chest.creativeMode")) + { + Teller.tell(sender, Type.ERROR, "You are not allowed to open your chest in Creative Mode!"); + return true; + } - switch (args.length) { - case 0: - // Open the player's own chest - if (player.hasPermission("alphachest.chest")) { - Inventory chest = chestManager.getChest(player.getUniqueId()); - player.openInventory(chest); - } else { - Teller.tell(sender, Type.ERROR, "You are not allowed to use this command."); - } + switch (args.length) + { + case 0: + // Open the player's own chest + if (player.hasPermission("alphachest.chest")) + { + Inventory chest = VirtualChestManager.getChest(player.getUniqueId()); + player.openInventory(chest); + } + else + { + Teller.tell(sender, Type.ERROR, "You are not allowed to use this command."); + } - return true; - case 1: - // Open someone else's chest - if (player.hasPermission("alphachest.admin")) { - OfflinePlayer target = BukkitUtil.getOfflinePlayerByName(args[0]); + return true; + case 1: + // Open someone else's chest + if (player.hasPermission("alphachest.admin")) + { + OfflinePlayer target = BukkitUtil.getOfflinePlayerByName(args[0]); - if (target != null) { - Inventory chest = chestManager.getChest(target.getUniqueId()); - player.openInventory(chest); - } else { - Teller.tell(player, Type.ERROR, String.format("Chest for %s not found", args[0])); - } - } else { - Teller.tell(player, Type.ERROR, "You are not allowed to open other user's chests."); - } + if (target != null) + { + Inventory chest = VirtualChestManager.getChest(target.getUniqueId()); + player.openInventory(chest); + } + else + { + Teller.tell(player, Type.ERROR, String.format("Chest for %s not found", args[0])); + } + } + else + { + Teller.tell(player, Type.ERROR, "You are not allowed to open other user's chests."); + } - return true; - } + return true; + } - return false; - } + return false; + } - return false; + return false; } } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/commands/ClearChestCommand.java b/src/main/java/net/sradonia/bukkit/alphachest/commands/ClearChestCommand.java index 5797846..a7e506d 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/commands/ClearChestCommand.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/commands/ClearChestCommand.java @@ -11,55 +11,67 @@ import net.sradonia.bukkit.alphachest.VirtualChestManager; import net.sradonia.bukkit.alphachest.utils.BukkitUtil; -public class ClearChestCommand implements CommandExecutor { - - private final VirtualChestManager chestManager; - - public ClearChestCommand(VirtualChestManager chestManager) { - this.chestManager = chestManager; +public class ClearChestCommand implements CommandExecutor +{ + public ClearChestCommand() + { + } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (command.getName().equalsIgnoreCase("clearchest")) { - switch (args.length) { - case 0: - // Make sure the sender is a player - if (!(sender instanceof Player)) { - Teller.tell(sender, Type.ERROR, "Only players are able to clear their own chests."); - return true; - } + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) + { + if (command.getName().equalsIgnoreCase("clearchest")) + { + switch (args.length) + { + case 0: + // Make sure the sender is a player + if (!(sender instanceof Player)) + { + Teller.tell(sender, Type.ERROR, "Only players are able to clear their own chests."); + return true; + } - Player player = (Player) sender; + Player player = (Player) sender; - if (player.hasPermission("alphachest.chest")) { - chestManager.removeChest(player.getUniqueId()); - Teller.tell(player, Type.SUCCESS, "Successfully cleared your chest."); - } else { - Teller.tell(player, Type.ERROR, "You are not allowed to use this command."); - } + if (player.hasPermission("alphachest.chest")) + { + VirtualChestManager.removeChest(player.getUniqueId()); + Teller.tell(player, Type.SUCCESS, "Successfully cleared your chest."); + } + else + { + Teller.tell(player, Type.ERROR, "You are not allowed to use this command."); + } - return true; - case 1: - if (sender.hasPermission("alphachest.admin")) { - OfflinePlayer target = BukkitUtil.getOfflinePlayerByName(args[0]); + return true; + case 1: + if (sender.hasPermission("alphachest.admin")) + { + OfflinePlayer target = BukkitUtil.getOfflinePlayerByName(args[0]); - if (target != null) { - chestManager.removeChest(target.getUniqueId()); - Teller.tell(sender, Type.SUCCESS, "Successfully cleared " + args[0] + "\'s chest."); - } else { - Teller.tell(sender, Type.ERROR, String.format("Chest for %s not found", args[0])); - } - } else { - Teller.tell(sender, Type.ERROR, "You are not allowed to clear other user's chests."); - } + if (target != null) + { + VirtualChestManager.removeChest(target.getUniqueId()); + Teller.tell(sender, Type.SUCCESS, "Successfully cleared " + args[0] + "\'s chest."); + } + else + { + Teller.tell(sender, Type.ERROR, String.format("Chest for %s not found", args[0])); + } + } + else + { + Teller.tell(sender, Type.ERROR, "You are not allowed to clear other user's chests."); + } - return true; - default: - return false; - } - } + return true; + default: + return false; + } + } - return false; + return false; } } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/commands/DisposalCommand.java b/src/main/java/net/sradonia/bukkit/alphachest/commands/DisposalCommand.java index 80ebe5b..0d64df2 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/commands/DisposalCommand.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/commands/DisposalCommand.java @@ -1,44 +1,41 @@ package net.sradonia.bukkit.alphachest.commands; - import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; - import net.sradonia.bukkit.alphachest.Teller; -import net.sradonia.bukkit.alphachest.VirtualChestManager; - -public class DisposalCommand implements CommandExecutor { - - private final VirtualChestManager chestManager; - - public DisposalCommand(VirtualChestManager chestManager) { - this.chestManager = chestManager; - } +public class DisposalCommand implements CommandExecutor +{ @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (command.getName().equalsIgnoreCase("disposal")) { - // Make sure the sender is a player - if (!(sender instanceof Player)) { - Teller.tell(sender, Teller.Type.ERROR, "Only players are able to open virtual workbenches."); - return true; - } - - Player player = (Player) sender; - - if (player.hasPermission("alphachest.disposal")) { - Inventory disposal = Bukkit.getServer().createInventory(player, 27, "Disposal"); - player.openInventory(disposal); - } else { - Teller.tell(sender, Teller.Type.ERROR, "You are not allowed to use this command."); - } - - return true; - } - - return false; + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) + { + if (command.getName().equalsIgnoreCase("disposal")) + { + // Make sure the sender is a player + if (!(sender instanceof Player)) + { + Teller.tell(sender, Teller.Type.ERROR, "Only players are able to open virtual workbenches."); + return true; + } + + Player player = (Player) sender; + + if (player.hasPermission("alphachest.disposal")) + { + Inventory disposal = Bukkit.getServer().createInventory(player, 27, "Disposal"); + player.openInventory(disposal); + } + else + { + Teller.tell(sender, Teller.Type.ERROR, "You are not allowed to use this command."); + } + + return true; + } + + return false; } } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/commands/SaveChestCommand.java b/src/main/java/net/sradonia/bukkit/alphachest/commands/SaveChestCommand.java index c953357..8c4ead2 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/commands/SaveChestCommand.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/commands/SaveChestCommand.java @@ -9,37 +9,41 @@ import net.sradonia.bukkit.alphachest.VirtualChestManager; import net.sradonia.bukkit.alphachest.utils.BukkitUtil; -public class SaveChestCommand implements CommandExecutor { - - private final VirtualChestManager chestManager; - - public SaveChestCommand(VirtualChestManager chestManager) { - this.chestManager = chestManager; - } +public class SaveChestCommand implements CommandExecutor +{ @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (command.getName().equalsIgnoreCase("savechest")) { - if (args.length == 1) { - if (sender.hasPermission("alphachest.save")) { - OfflinePlayer target = BukkitUtil.getOfflinePlayerByName(args[0]); - - if (target != null) { - chestManager.saveChest(target.getUniqueId()); - Teller.tell(sender, Teller.Type.SUCCESS, "Successfully saved " + args[0] + "\'s chest."); - } else { - Teller.tell(sender, Teller.Type.ERROR, String.format("Chest for %s not found", args[0])); - } - } else { - Teller.tell(sender, Teller.Type.ERROR, "You are not allowed to use this command."); - } - - return true; - } - - return false; - } - - return false; + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) + { + if (command.getName().equalsIgnoreCase("savechest")) + { + if (args.length == 1) + { + if (sender.hasPermission("alphachest.save")) + { + OfflinePlayer target = BukkitUtil.getOfflinePlayerByName(args[0]); + + if (target != null) + { + VirtualChestManager.saveChest(target.getUniqueId()); + Teller.tell(sender, Teller.Type.SUCCESS, "Successfully saved " + args[0] + "\'s chest."); + } + else + { + Teller.tell(sender, Teller.Type.ERROR, String.format("Chest for %s not found", args[0])); + } + } + else + { + Teller.tell(sender, Teller.Type.ERROR, "You are not allowed to use this command."); + } + + return true; + } + + return false; + } + + return false; } } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/commands/SaveChestsCommand.java b/src/main/java/net/sradonia/bukkit/alphachest/commands/SaveChestsCommand.java index 92f1f11..79b8f19 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/commands/SaveChestsCommand.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/commands/SaveChestsCommand.java @@ -8,27 +8,31 @@ import net.sradonia.bukkit.alphachest.Teller.Type; import net.sradonia.bukkit.alphachest.VirtualChestManager; -public class SaveChestsCommand implements CommandExecutor { +public class SaveChestsCommand implements CommandExecutor +{ - private final VirtualChestManager chestManager; - - public SaveChestsCommand(VirtualChestManager chestManager) { - this.chestManager = chestManager; + public SaveChestsCommand() + { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (command.getName().equalsIgnoreCase("savechests")) { - if (sender.hasPermission("alphachest.save")) { - int savedChests = chestManager.save(); - Teller.tell(sender, Type.SUCCESS, "Saved " + savedChests + " chests."); - } else { - Teller.tell(sender, Type.ERROR, "You are not allowed to use this command."); - } - - return true; - } - - return false; + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) + { + if (command.getName().equalsIgnoreCase("savechests")) + { + if (sender.hasPermission("alphachest.save")) + { + int savedChests = VirtualChestManager.saveAll(); + Teller.tell(sender, Type.SUCCESS, "Saved " + savedChests + " chests."); + } + else + { + Teller.tell(sender, Type.ERROR, "You are not allowed to use this command."); + } + + return true; + } + + return false; } } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/listeners/PlayerListener.java b/src/main/java/net/sradonia/bukkit/alphachest/listeners/PlayerListener.java index 86c2407..365389c 100644 --- a/src/main/java/net/sradonia/bukkit/alphachest/listeners/PlayerListener.java +++ b/src/main/java/net/sradonia/bukkit/alphachest/listeners/PlayerListener.java @@ -1,66 +1,47 @@ package net.sradonia.bukkit.alphachest.listeners; -import java.util.List; +import java.util.concurrent.CompletableFuture; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitRunnable; -import net.sradonia.bukkit.alphachest.AlphaChestPlugin; import net.sradonia.bukkit.alphachest.VirtualChestManager; -public class PlayerListener implements Listener { +public class PlayerListener implements Listener +{ + private Plugin pl; - private final AlphaChestPlugin plugin; - private final VirtualChestManager chestManager; - - private boolean clearOnDeath; - private boolean dropOnDeath; - - public PlayerListener(AlphaChestPlugin plugin, VirtualChestManager chestManager) { - this.plugin = plugin; - this.chestManager = chestManager; - - // Load the death event settings - clearOnDeath = plugin.getConfig().getBoolean("clearOnDeath"); - dropOnDeath = plugin.getConfig().getBoolean("dropOnDeath"); + public PlayerListener(Plugin pl) + { + this.pl = pl; } - /** - * Handles a player's death and clears the chest or drops its contents depending on configuration and permissions. - */ - @EventHandler(ignoreCancelled = true) - public void onPlayerDeath(final PlayerDeathEvent event) { - final Player player = event.getEntity(); - - boolean drop = dropOnDeath; - boolean clear = dropOnDeath || clearOnDeath; - - if (player.hasPermission("alphachest.keepOnDeath")) { - drop = false; - clear = false; - } else if (player.hasPermission("alphachest.dropOnDeath")) { - drop = true; - clear = true; - } else if (player.hasPermission("alphachest.clearOnDeath")) { - drop = false; - clear = true; - } - - if (drop) { - List drops = event.getDrops(); - Inventory chest = chestManager.getChest(player.getUniqueId()); - - for (int i = 0; i < chest.getSize(); i++) { - drops.add(chest.getItem(i)); - } - } + @EventHandler + public void onLogin(PlayerLoginEvent e) + { + CompletableFuture.runAsync(() -> VirtualChestManager.setChest(e.getPlayer())); + } - if (clear) { - chestManager.removeChest(player.getUniqueId()); - } + @EventHandler + public void onLogout(PlayerQuitEvent e) + { + CompletableFuture.runAsync(() -> VirtualChestManager.saveChest(e.getPlayer().getUniqueId())); + new BukkitRunnable() + { + final Player p = e.getPlayer(); + public void run() + { + if (!p.isOnline()) + { + CompletableFuture.runAsync(() -> VirtualChestManager.removeEntry(e.getPlayer())); + } + } + }.runTaskLater(pl, 2400); } + } diff --git a/src/main/java/net/sradonia/bukkit/alphachest/utils/ShutdownThread.java b/src/main/java/net/sradonia/bukkit/alphachest/utils/ShutdownThread.java new file mode 100644 index 0000000..40aa2b7 --- /dev/null +++ b/src/main/java/net/sradonia/bukkit/alphachest/utils/ShutdownThread.java @@ -0,0 +1,12 @@ +package net.sradonia.bukkit.alphachest.utils; + +import net.sradonia.bukkit.alphachest.VirtualChestManager; + +public class ShutdownThread extends Thread +{ + public void run() + { + System.out.println("[AlphaChests] Shutdown Hook Called!"); + VirtualChestManager.saveAll(); + } +} From 935addc8e1dae9aaf5a4e1e09c9bd749facdb7ec Mon Sep 17 00:00:00 2001 From: Redmancometh Date: Mon, 27 Jul 2015 12:23:40 -0500 Subject: [PATCH 2/2] oops --- .../java/net/sradonia/bukkit/alphachest/ChestManager.java | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 src/main/java/net/sradonia/bukkit/alphachest/ChestManager.java diff --git a/src/main/java/net/sradonia/bukkit/alphachest/ChestManager.java b/src/main/java/net/sradonia/bukkit/alphachest/ChestManager.java deleted file mode 100644 index 540e9fe..0000000 --- a/src/main/java/net/sradonia/bukkit/alphachest/ChestManager.java +++ /dev/null @@ -1,6 +0,0 @@ -package net.sradonia.bukkit.alphachest; - -public class ChestManager -{ - -}