From f9ac6804b29948bf5dc55c77501a8abb5fc9ad21 Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Thu, 10 Mar 2016 13:37:28 +1100 Subject: [PATCH 1/7] General refactoring --- .../com/lishid/openinv/Configuration.java | 146 ++++++++++++++ src/main/java/com/lishid/openinv/OpenInv.java | 179 ++++++++---------- .../java/com/lishid/openinv/Permissions.java | 5 +- .../openinv/commands/AnyChestCommand.java | 15 +- .../openinv/commands/OpenEnderCommand.java | 2 +- .../openinv/commands/OpenInvCommand.java | 7 +- .../openinv/commands/SearchEnderCommand.java | 6 + .../openinv/commands/SearchInvCommand.java | 6 + .../openinv/commands/SilentChestCommand.java | 19 +- .../commands/ToggleOpenInvCommand.java | 19 +- .../openinv/internal/AnySilentChest.java | 32 +++- .../openinv/internal/InventoryAccess.java | 14 +- .../openinv/internal/PlayerDataManager.java | 16 +- .../internal/SilentContainerChest.java | 5 +- .../openinv/internal/SilentInventory.java | 10 +- .../openinv/internal/SpecialEnderChest.java | 10 +- .../listeners/OpenInvEntityListener.java | 14 +- .../listeners/OpenInvInventoryListener.java | 9 +- .../listeners/OpenInvPlayerListener.java | 24 ++- src/main/resources/plugin.yml | 1 + 20 files changed, 381 insertions(+), 158 deletions(-) create mode 100644 src/main/java/com/lishid/openinv/Configuration.java diff --git a/src/main/java/com/lishid/openinv/Configuration.java b/src/main/java/com/lishid/openinv/Configuration.java new file mode 100644 index 0000000..b391ccf --- /dev/null +++ b/src/main/java/com/lishid/openinv/Configuration.java @@ -0,0 +1,146 @@ +package com.lishid.openinv; + +import org.bukkit.Material; +import org.bukkit.entity.Player; + +public class Configuration { + + private final OpenInv plugin; + + private Material openInvItem; + private boolean notifySilentChest; + private boolean notifyAnyChest; + + public Configuration(OpenInv plugin) { + this.plugin = plugin; + + // Check for config updates + ConfigUpdater configUpdater = new ConfigUpdater(plugin); + configUpdater.checkForUpdates(); + + // Load the config settings + load(); + } + + /** + * Loads OpenInv's config settings. + */ + public void load() { + // OpenInv Item + if (!plugin.getConfig().isSet("items.open-inv")) { + saveToConfig("items.open-inv", "STICK"); + } + + String itemName = plugin.getConfig().getString("items.open-inv", "STICK"); + Material material = Material.getMaterial(itemName); + + if (material == null) { + plugin.getLogger().warning("OpenInv item '" + itemName + "' does not match to a valid item. Defaulting to stick."); + material = Material.STICK; + } + + openInvItem = material; + + // Other Values + notifySilentChest = plugin.getConfig().getBoolean("notify.silent-chest", true); + notifyAnyChest = plugin.getConfig().getBoolean("notify.any-chest", true); + } + + /** + * Saves a value to the plugin config at the specified path. + * + * @param path the path to set the value to + * @param value the value to set to the path + */ + public void saveToConfig(String path, Object value) { + plugin.getConfig().set(path, value); + plugin.saveConfig(); + } + + /** + * Returns the OpenInv item Material. + * + * @return the OpenInv item Material + */ + public Material getOpenInvItem() { + return openInvItem; + } + + /** + * Returns whether or not notify silent chest is enabled. + * + * @return true if notify silent chest is enabled; false otherwise + */ + public boolean notifySilentChest() { + return notifySilentChest; + } + + /** + * Returns whether or not notify any chest is enabled. + * + * @return true if notify any chest is enabled; false otherwise + */ + public boolean notifyAnyChest() { + return notifyAnyChest; + } + + /** + * Returns a player's item OpenInv status. + * + * @param player the player to get the item OpenInv status of + * @return the player's item OpenInv status + */ + public boolean getPlayerItemOpenInvStatus(Player player) { + return plugin.getConfig().getBoolean("toggles.items.open-inv." + player.getUniqueId(), false); + } + + /** + * Returns a player's any chest status. + * + * @param player the player to get the any chest status of + * @return the player's any chest status + */ + public boolean getPlayerAnyChestStatus(Player player) { + return plugin.getConfig().getBoolean("toggles.any-chest." + player.getUniqueId(), true); + } + + /** + * Sets a player's any chest status. + * + * @param player the player to set the any chest status of + * @param status the status to set with + */ + public void setPlayerAnyChestStatus(Player player, boolean status) { + saveToConfig("toggles.any-chest." + player.getUniqueId(), status); + } + + /** + * Sets a player's item OpenInv status. + * + * @param player the player to set the item OpenInv status of + * @param status the status to set with + */ + public void setPlayerItemOpenInvStatus(Player player, boolean status) { + saveToConfig("toggles.items.open-inv." + player.getUniqueId(), status); + } + + /** + * Returns a player's silent chest status. + * + * @param player the player to get the silent chest status of + * @return the player's silent chest status + */ + public boolean getPlayerSilentChestStatus(Player player) { + return plugin.getConfig().getBoolean("toggles.silent-chest." + player.getUniqueId(), false); + } + + /** + * Sets a player's silent chest status. + * + * @param player the player to set the silent chest status of + * @param status the status to set with + */ + public void setPlayerSilentChestStatus(Player player, boolean status) { + saveToConfig("toggles.silent-chest." + player.getUniqueId(), status); + } +} diff --git a/src/main/java/com/lishid/openinv/OpenInv.java b/src/main/java/com/lishid/openinv/OpenInv.java index 863891b..08d563f 100644 --- a/src/main/java/com/lishid/openinv/OpenInv.java +++ b/src/main/java/com/lishid/openinv/OpenInv.java @@ -20,9 +20,7 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.permissions.Permissible; @@ -50,140 +48,112 @@ public class OpenInv extends JavaPlugin { public static final Map inventories = new HashMap(); public static final Map enderChests = new HashMap(); - public static OpenInv mainPlugin; + private PlayerDataManager playerLoader; + private InventoryAccess inventoryAccess; + private AnySilentChest anySilentChest; - private static PlayerDataManager playerLoader; - private static InventoryAccess inventoryAccess; - private static AnySilentChest anySilentChest; + private Configuration configuration; @Override public void onEnable() { - // Plugin - mainPlugin = this; - - // Config Updater - ConfigUpdater configUpdater = new ConfigUpdater(this); - configUpdater.checkForUpdates(); + // Config + configuration = new Configuration(this); // Initialize - playerLoader = new PlayerDataManager(); - inventoryAccess = new InventoryAccess(); - anySilentChest = new AnySilentChest(); + playerLoader = new PlayerDataManager(this); + inventoryAccess = new InventoryAccess(this); + anySilentChest = new AnySilentChest(this); // Save the default config.yml if it doesn't already exist saveDefaultConfig(); - // Register the plugin's events & commands - registerEvents(); - registerCommands(); - } - - private void registerEvents() { + // Register the plugin's events PluginManager pm = getServer().getPluginManager(); - pm.registerEvents(new OpenInvPlayerListener(), this); - pm.registerEvents(new OpenInvEntityListener(), this); - pm.registerEvents(new OpenInvInventoryListener(), this); - } + pm.registerEvents(new OpenInvPlayerListener(this), this); + pm.registerEvents(new OpenInvEntityListener(this), this); + pm.registerEvents(new OpenInvInventoryListener(this), this); - private void registerCommands() { + // Register the plugin's commands getCommand("openinv").setExecutor(new OpenInvCommand(this)); getCommand("openender").setExecutor(new OpenEnderCommand(this)); - getCommand("searchinv").setExecutor(new SearchInvCommand()); - getCommand("searchender").setExecutor(new SearchEnderCommand()); - getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand()); - getCommand("anychest").setExecutor(new AnyChestCommand()); - getCommand("silentchest").setExecutor(new SilentChestCommand()); + getCommand("searchinv").setExecutor(new SearchInvCommand(this)); + getCommand("searchender").setExecutor(new SearchEnderCommand(this)); + getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand(this)); + getCommand("anychest").setExecutor(new AnyChestCommand(this)); + getCommand("silentchest").setExecutor(new SilentChestCommand(this)); } - public static PlayerDataManager getPlayerLoader() { + /** + * Returns the plugin Configuration. + * + * @return the plugin Configuration + */ + public Configuration getConfiguration() { + return configuration; + } + + /** + * Returns an instance of PlayerDataManager. + * + * @return an instance of PlayerDataManager + */ + public PlayerDataManager getPlayerLoader() { return playerLoader; } - public static InventoryAccess getInventoryAccess() { + /** + * Returns an instance of InventoryAccess. + * + * @return an instance of InventoryAccess + */ + public InventoryAccess getInventoryAccess() { return inventoryAccess; } - public static AnySilentChest getAnySilentChest() { + /** + * Returns an instance of AnySilentChest. + * + * @return an instance of AnySilentChest + */ + public AnySilentChest getAnySilentChest() { return anySilentChest; } - public static Object getFromConfig(String path, Object defaultValue) { - Object val = mainPlugin.getConfig().get(path); - - if (val == null) { - mainPlugin.getConfig().set(path, defaultValue); - return defaultValue; - } else { - return val; - } + /** + * Logs a message to console. + * + * @param text the message to log + */ + public void log(String text) { + getLogger().info(text); } - public static void saveToConfig(String path, Object value) { - mainPlugin.getConfig().set(path, value); - mainPlugin.saveConfig(); - } - - public static Material getOpenInvItem() { - if (!mainPlugin.getConfig().isSet("items.open-inv")) { - saveToConfig("items.open-inv", "STICK"); - } - - String itemName = mainPlugin.getConfig().getString("items.open-inv", "STICK"); - Material material = Material.getMaterial(itemName); - if (material == null) { - mainPlugin.getLogger().warning("OpenInv item '" + itemName + "' does not match to a valid item. Defaulting to stick."); - material = Material.STICK; - } - - return material; - } - - public static boolean notifySilentChest() { - return mainPlugin.getConfig().getBoolean("notify.silent-chest", true); - } - - public static boolean notifyAnyChest() { - return mainPlugin.getConfig().getBoolean("notify.any-chest", true); - } - - public static boolean getPlayerAnyChestStatus(Player player) { - return mainPlugin.getConfig().getBoolean("toggles.any-chest." + player.getUniqueId(), true); - } - - public static void setPlayerAnyChestStatus(Player player, boolean status) { - saveToConfig("toggles.any-chest." + player.getUniqueId(), status); - } - - public static boolean getPlayerItemOpenInvStatus(Player player) { - return mainPlugin.getConfig().getBoolean("toggles.items.open-inv." + player.getUniqueId(), false); - } - - public static void setPlayerItemOpenInvStatus(Player player, boolean status) { - saveToConfig("toggles.items.open-inv." + player.getUniqueId(), status); - } - - public static boolean getPlayerSilentChestStatus(Player player) { - return mainPlugin.getConfig().getBoolean("toggles.silent-chest." + player.getUniqueId(), false); - } - - public static void setPlayerSilentChestStatus(Player player, boolean status) { - saveToConfig("toggles.silent-chest." + player.getUniqueId(), status); - } - - public static void log(String text) { - mainPlugin.getLogger().info("[OpenInv] " + text); - } - - public static void log(Throwable e) { - mainPlugin.getLogger().severe("[OpenInv] " + e.toString()); + /** + * Logs a Throwable to console. + * + * @param e the Throwable to log + */ + public void log(Throwable e) { + getLogger().severe(e.toString()); e.printStackTrace(); } + /** + * Sends an OpenInv message to a player. + * + * @param sender the CommandSender to message + * @param message the message to send + */ public static void sendMessage(CommandSender sender, String message) { sender.sendMessage(ChatColor.AQUA + "[OpenInv] " + ChatColor.WHITE + message); } + /** + * Outputs OpenInv help information to a player. + * + * @param player the player to show help to + */ public static void showHelp(Player player) { player.sendMessage(ChatColor.GREEN + "/openinv - Open a player's inventory."); player.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)"); @@ -209,6 +179,13 @@ public class OpenInv extends JavaPlugin { player.sendMessage(ChatColor.GREEN + " (aliases: sc, silent)"); } + /** + * Returns whether or not a player has a permission. + * + * @param player the player to check + * @param permission the permission node to check for + * @return true if the player has the permission; false otherwise + */ public static boolean hasPermission(Permissible player, String permission) { String[] parts = permission.split("\\."); String perm = ""; diff --git a/src/main/java/com/lishid/openinv/Permissions.java b/src/main/java/com/lishid/openinv/Permissions.java index f0de775..5021236 100644 --- a/src/main/java/com/lishid/openinv/Permissions.java +++ b/src/main/java/com/lishid/openinv/Permissions.java @@ -1,6 +1,9 @@ package com.lishid.openinv; -public class Permissions { +public final class Permissions { + + private Permissions() {} + public static final String PERM_OPENINV = "OpenInv.openinv"; public static final String PERM_OVERRIDE = "OpenInv.override"; public static final String PERM_EXEMPT = "OpenInv.exempt"; diff --git a/src/main/java/com/lishid/openinv/commands/AnyChestCommand.java b/src/main/java/com/lishid/openinv/commands/AnyChestCommand.java index 732f641..1cfdd85 100644 --- a/src/main/java/com/lishid/openinv/commands/AnyChestCommand.java +++ b/src/main/java/com/lishid/openinv/commands/AnyChestCommand.java @@ -24,9 +24,18 @@ import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; +import com.lishid.openinv.Configuration; public class AnyChestCommand implements CommandExecutor { + private final OpenInv plugin; + private final Configuration configuration; + + public AnyChestCommand(OpenInv plugin) { + this.plugin = plugin; + configuration = plugin.getConfiguration(); + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("anychest")) { @@ -44,15 +53,15 @@ public class AnyChestCommand implements CommandExecutor { if (args.length > 0) { if (args[0].equalsIgnoreCase("check")) { - String status = OpenInv.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + String status = configuration.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; OpenInv.sendMessage(player, "Any Chest is " + status + ChatColor.RESET + "."); return true; } } - OpenInv.setPlayerAnyChestStatus(player, !OpenInv.getPlayerAnyChestStatus(player)); + configuration.setPlayerAnyChestStatus(player, !configuration.getPlayerAnyChestStatus(player)); - String status = OpenInv.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + String status = configuration.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; OpenInv.sendMessage(player, "Any Chest is now " + status + ChatColor.RESET + "."); return true; diff --git a/src/main/java/com/lishid/openinv/commands/OpenEnderCommand.java b/src/main/java/com/lishid/openinv/commands/OpenEnderCommand.java index ceb6472..ab4320d 100644 --- a/src/main/java/com/lishid/openinv/commands/OpenEnderCommand.java +++ b/src/main/java/com/lishid/openinv/commands/OpenEnderCommand.java @@ -96,7 +96,7 @@ public class OpenEnderCommand implements CommandExecutor { @Override public void run() { // Try loading the player's data asynchronously - final Player target = OpenInv.getPlayerLoader().loadPlayer(uuid); + final Player target = plugin.getPlayerLoader().loadPlayer(uuid); if (target == null) { player.sendMessage(ChatColor.RED + "Player not found!"); return; diff --git a/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java b/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java index 75e093a..ce7e8ba 100644 --- a/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java +++ b/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java @@ -48,6 +48,7 @@ public class OpenInvCommand implements CommandExecutor { sender.sendMessage(ChatColor.RED + "You can't use this command from the console."); return true; } + if (!OpenInv.hasPermission(sender, Permissions.PERM_OPENINV)) { sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories."); return true; @@ -90,7 +91,7 @@ public class OpenInvCommand implements CommandExecutor { @Override public void run() { // Try loading the player's data asynchronously - final Player target = OpenInv.getPlayerLoader().loadPlayer(uuid); + final Player target = plugin.getPlayerLoader().loadPlayer(uuid); if (target == null) { player.sendMessage(ChatColor.RED + "Player not found!"); return; @@ -148,14 +149,10 @@ public class OpenInvCommand implements CommandExecutor { openInvHistory.put(player.getUniqueId(), target.getUniqueId()); // Create the inventory - /* SpecialPlayerInventory inv = OpenInv.inventories.get(target.getUniqueId()); if (inv == null) { inv = new SpecialPlayerInventory(target, target.isOnline()); } - */ - - SpecialPlayerInventory inv = new SpecialPlayerInventory(target, target.isOnline()); // Open the inventory player.openInventory(inv.getBukkitInventory()); diff --git a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java index 778a762..544b353 100644 --- a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java @@ -13,6 +13,12 @@ import com.lishid.openinv.Permissions; public class SearchEnderCommand implements CommandExecutor { + private OpenInv plugin; + + public SearchEnderCommand(OpenInv plugin) { + this.plugin = plugin; + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("searchender")) { diff --git a/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java b/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java index 46bf5f3..b7eb5df 100644 --- a/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java @@ -29,6 +29,12 @@ import com.lishid.openinv.Permissions; public class SearchInvCommand implements CommandExecutor { + private final OpenInv plugin; + + public SearchInvCommand(OpenInv plugin) { + this.plugin = plugin; + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("searchinv")) { diff --git a/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java b/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java index afdc8c6..73de4c5 100644 --- a/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java @@ -24,9 +24,18 @@ import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; +import com.lishid.openinv.Configuration; public class SilentChestCommand implements CommandExecutor { + private final OpenInv plugin; + private final Configuration configuration; + + public SilentChestCommand(OpenInv plugin) { + this.plugin = plugin; + configuration = plugin.getConfiguration(); + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("silentchest")) { @@ -44,16 +53,16 @@ public class SilentChestCommand implements CommandExecutor { if (args.length > 0) { if (args[0].equalsIgnoreCase("check")) { - String status = OpenInv.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; - OpenInv.sendMessage(player, "Silent Chest is " + status + ChatColor.RESET + "."); + String status = configuration.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + plugin.sendMessage(player, "Silent Chest is " + status + ChatColor.RESET + "."); return true; } } - OpenInv.setPlayerSilentChestStatus(player, !OpenInv.getPlayerSilentChestStatus(player)); + configuration.setPlayerSilentChestStatus(player, !configuration.getPlayerSilentChestStatus(player)); - String status = OpenInv.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; - OpenInv.sendMessage(player, "Silent Chest is now " + status + ChatColor.RESET + "."); + String status = configuration.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + plugin.sendMessage(player, "Silent Chest is now " + status + ChatColor.RESET + "."); return true; } diff --git a/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java b/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java index ac3c714..b6edd62 100644 --- a/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java +++ b/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java @@ -24,9 +24,18 @@ import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; +import com.lishid.openinv.Configuration; public class ToggleOpenInvCommand implements CommandExecutor { + private final OpenInv plugin; + private final Configuration configuration; + + public ToggleOpenInvCommand(OpenInv plugin) { + this.plugin = plugin; + configuration = plugin.getConfiguration(); + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("toggleopeninv")) { @@ -44,16 +53,16 @@ public class ToggleOpenInvCommand implements CommandExecutor { if (args.length > 0) { if (args[0].equalsIgnoreCase("check")) { - String status = OpenInv.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; - OpenInv.sendMessage(player, "OpenInv with " + ChatColor.GRAY + OpenInv.getOpenInvItem() + ChatColor.RESET + status + ChatColor.RESET + "."); + String status = configuration.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + plugin.sendMessage(player, "OpenInv with " + ChatColor.GRAY + configuration.getOpenInvItem() + ChatColor.RESET + status + ChatColor.RESET + "."); return true; } } - OpenInv.setPlayerItemOpenInvStatus(player, !OpenInv.getPlayerItemOpenInvStatus(player)); + configuration.setPlayerItemOpenInvStatus(player, !configuration.getPlayerItemOpenInvStatus(player)); - String status = OpenInv.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; - OpenInv.sendMessage(player, "OpenInv with " + ChatColor.GRAY + OpenInv.getOpenInvItem() + ChatColor.RESET + " is now " + status + ChatColor.RESET + "."); + String status = configuration.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + plugin.sendMessage(player, "OpenInv with " + ChatColor.GRAY + configuration.getOpenInvItem() + ChatColor.RESET + " is now " + status + ChatColor.RESET + "."); return true; } diff --git a/src/main/java/com/lishid/openinv/internal/AnySilentChest.java b/src/main/java/com/lishid/openinv/internal/AnySilentChest.java index 8347aa6..1a22b97 100644 --- a/src/main/java/com/lishid/openinv/internal/AnySilentChest.java +++ b/src/main/java/com/lishid/openinv/internal/AnySilentChest.java @@ -18,18 +18,34 @@ package com.lishid.openinv.internal; import java.util.Iterator; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; -// Volatile -import net.minecraft.server.v1_9_R1.*; +import net.minecraft.server.v1_9_R1.AxisAlignedBB; +import net.minecraft.server.v1_9_R1.Block; +import net.minecraft.server.v1_9_R1.BlockChest; import net.minecraft.server.v1_9_R1.BlockChest.Type; - -import org.bukkit.craftbukkit.v1_9_R1.entity.*; +import net.minecraft.server.v1_9_R1.BlockPosition; +import net.minecraft.server.v1_9_R1.Entity; +import net.minecraft.server.v1_9_R1.EntityOcelot; +import net.minecraft.server.v1_9_R1.EntityPlayer; +import net.minecraft.server.v1_9_R1.EnumDirection; +import net.minecraft.server.v1_9_R1.ITileInventory; +import net.minecraft.server.v1_9_R1.InventoryLargeChest; +import net.minecraft.server.v1_9_R1.TileEntity; +import net.minecraft.server.v1_9_R1.TileEntityChest; +import net.minecraft.server.v1_9_R1.World; public class AnySilentChest { + private final OpenInv plugin; + + public AnySilentChest(OpenInv plugin) { + this.plugin = plugin; + } + public boolean isAnyChestNeeded(Player p, int x, int y, int z) { // FOR REFERENCE, LOOK AT net.minecraft.server.BlockChest BlockPosition position = new BlockPosition(x, y, z); @@ -135,8 +151,8 @@ public class AnySilentChest { if (silentChest) { tileInventory = new SilentInventory(tileInventory); - if (OpenInv.notifySilentChest()) { - OpenInv.sendMessage(p, "You are opening a chest silently."); + if (plugin.getConfiguration().notifySilentChest()) { + plugin.sendMessage(p, "You are opening a chest silently."); } returnValue = false; @@ -144,8 +160,8 @@ public class AnySilentChest { player.openContainer(tileInventory); - if (anyChest && OpenInv.notifyAnyChest()) { - OpenInv.sendMessage(p, "You are opening a blocked chest."); + if (anyChest && plugin.getConfiguration().notifyAnyChest()) { + plugin.sendMessage(p, "You are opening a blocked chest."); } return returnValue; diff --git a/src/main/java/com/lishid/openinv/internal/InventoryAccess.java b/src/main/java/com/lishid/openinv/internal/InventoryAccess.java index cf8e2f1..9aacb51 100644 --- a/src/main/java/com/lishid/openinv/internal/InventoryAccess.java +++ b/src/main/java/com/lishid/openinv/internal/InventoryAccess.java @@ -18,19 +18,23 @@ package com.lishid.openinv.internal; import java.lang.reflect.Field; +import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftInventory; import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.Inventory; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; -// Volatile -import net.minecraft.server.v1_9_R1.*; - -import org.bukkit.craftbukkit.v1_9_R1.inventory.*; +import net.minecraft.server.v1_9_R1.IInventory; public class InventoryAccess { + private final OpenInv plugin; + + public InventoryAccess(OpenInv plugin) { + this.plugin = plugin; + } + public boolean check(Inventory inventory, HumanEntity player) { IInventory inv = grabInventory(inventory); @@ -64,7 +68,7 @@ public class InventoryAccess { try { result = (IInventory) f.get(inventory); } catch (Exception e) { - OpenInv.log(e); + plugin.log(e); } } } diff --git a/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java b/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java index 4e4aef0..2f2f8d9 100644 --- a/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java +++ b/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java @@ -20,18 +20,24 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.craftbukkit.v1_9_R1.CraftServer; import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; import com.mojang.authlib.GameProfile; -// Volatile -import net.minecraft.server.v1_9_R1.*; - -import org.bukkit.craftbukkit.v1_9_R1.*; +import net.minecraft.server.v1_9_R1.EntityPlayer; +import net.minecraft.server.v1_9_R1.MinecraftServer; +import net.minecraft.server.v1_9_R1.PlayerInteractManager; public class PlayerDataManager { + private OpenInv plugin; + + public PlayerDataManager(OpenInv plugin) { + this.plugin = plugin; + } + public Player loadPlayer(UUID uuid) { try { OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); @@ -55,7 +61,7 @@ public class PlayerDataManager { return target; } } catch (Exception e) { - OpenInv.log(e); + plugin.log(e); } return null; diff --git a/src/main/java/com/lishid/openinv/internal/SilentContainerChest.java b/src/main/java/com/lishid/openinv/internal/SilentContainerChest.java index 301c813..7c9c5c3 100644 --- a/src/main/java/com/lishid/openinv/internal/SilentContainerChest.java +++ b/src/main/java/com/lishid/openinv/internal/SilentContainerChest.java @@ -16,8 +16,9 @@ package com.lishid.openinv.internal; -// Volatile -import net.minecraft.server.v1_9_R1.*; +import net.minecraft.server.v1_9_R1.ContainerChest; +import net.minecraft.server.v1_9_R1.EntityHuman; +import net.minecraft.server.v1_9_R1.IInventory; public class SilentContainerChest extends ContainerChest { diff --git a/src/main/java/com/lishid/openinv/internal/SilentInventory.java b/src/main/java/com/lishid/openinv/internal/SilentInventory.java index ee802c5..f2b246e 100644 --- a/src/main/java/com/lishid/openinv/internal/SilentInventory.java +++ b/src/main/java/com/lishid/openinv/internal/SilentInventory.java @@ -7,8 +7,14 @@ import org.bukkit.craftbukkit.v1_9_R1.entity.CraftHumanEntity; import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.InventoryHolder; -// Volatile -import net.minecraft.server.v1_9_R1.*; +import net.minecraft.server.v1_9_R1.ChestLock; +import net.minecraft.server.v1_9_R1.Container; +import net.minecraft.server.v1_9_R1.ContainerChest; +import net.minecraft.server.v1_9_R1.EntityHuman; +import net.minecraft.server.v1_9_R1.IChatBaseComponent; +import net.minecraft.server.v1_9_R1.ITileInventory; +import net.minecraft.server.v1_9_R1.ItemStack; +import net.minecraft.server.v1_9_R1.PlayerInventory; public class SilentInventory implements ITileInventory { diff --git a/src/main/java/com/lishid/openinv/internal/SpecialEnderChest.java b/src/main/java/com/lishid/openinv/internal/SpecialEnderChest.java index 4c37370..f8b74af 100644 --- a/src/main/java/com/lishid/openinv/internal/SpecialEnderChest.java +++ b/src/main/java/com/lishid/openinv/internal/SpecialEnderChest.java @@ -16,17 +16,17 @@ package com.lishid.openinv.internal; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftInventory; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import com.lishid.openinv.OpenInv; -// Volatile -import net.minecraft.server.v1_9_R1.*; - -import org.bukkit.craftbukkit.v1_9_R1.entity.*; -import org.bukkit.craftbukkit.v1_9_R1.inventory.*; +import net.minecraft.server.v1_9_R1.InventoryEnderChest; +import net.minecraft.server.v1_9_R1.InventorySubcontainer; public class SpecialEnderChest extends InventorySubcontainer { diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvEntityListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvEntityListener.java index 0411f6f..b01a9f4 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvEntityListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvEntityListener.java @@ -25,8 +25,18 @@ import org.bukkit.event.Listener; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; +import com.lishid.openinv.Configuration; public class OpenInvEntityListener implements Listener { + + private final OpenInv plugin; + private final Configuration configuration; + + public OpenInvEntityListener(OpenInv plugin) { + this.plugin = plugin; + configuration = plugin.getConfiguration(); + } + @EventHandler(priority = EventPriority.LOWEST) public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { Entity attacker = event.getDamager(); @@ -38,8 +48,8 @@ public class OpenInvEntityListener implements Listener { Player player = (Player) attacker; - if (player.getInventory().getItemInMainHand().getType() == OpenInv.getOpenInvItem()) { - if (!OpenInv.getPlayerItemOpenInvStatus(player) || !OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) { + if (player.getInventory().getItemInMainHand().getType() == configuration.getOpenInvItem()) { + if (!configuration.getPlayerItemOpenInvStatus(player) || !OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) { return; } diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvInventoryListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvInventoryListener.java index e81fd5c..efe92fe 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvInventoryListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvInventoryListener.java @@ -25,12 +25,19 @@ import org.bukkit.inventory.Inventory; import com.lishid.openinv.OpenInv; public class OpenInvInventoryListener implements Listener { + + private final OpenInv plugin; + + public OpenInvInventoryListener(OpenInv plugin) { + this.plugin = plugin; + } + @EventHandler public void onInventoryClick(InventoryClickEvent event) { Inventory inventory = event.getInventory(); HumanEntity player = event.getWhoClicked(); - if (!OpenInv.getInventoryAccess().check(inventory, player)) { + if (!plugin.getInventoryAccess().check(inventory, player)) { event.setCancelled(true); } } diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java index 579d93d..7db5c9b 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java @@ -33,10 +33,20 @@ import org.bukkit.event.player.PlayerQuitEvent; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; +import com.lishid.openinv.Configuration; import com.lishid.openinv.internal.SpecialEnderChest; import com.lishid.openinv.internal.SpecialPlayerInventory; public class OpenInvPlayerListener implements Listener { + + private final OpenInv plugin; + private final Configuration configuration; + + public OpenInvPlayerListener(OpenInv plugin) { + this.plugin = plugin; + configuration = plugin.getConfiguration(); + } + @EventHandler(priority = EventPriority.LOWEST) public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); @@ -86,7 +96,7 @@ public class OpenInvPlayerListener implements Listener { // Ender Chests if (block.getType() == Material.ENDER_CHEST) { - if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && OpenInv.getPlayerSilentChestStatus(player)) { + if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && configuration.getPlayerSilentChestStatus(player)) { event.setCancelled(true); player.openInventory(player.getEnderChest()); return; @@ -101,13 +111,13 @@ public class OpenInvPlayerListener implements Listener { int y = block.getY(); int z = block.getZ(); - if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && OpenInv.getPlayerSilentChestStatus(player)) { + if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && configuration.getPlayerSilentChestStatus(player)) { silentChest = true; } - if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && OpenInv.getPlayerAnyChestStatus(player)) { + if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && configuration.getPlayerAnyChestStatus(player)) { try { - anyChest = OpenInv.getAnySilentChest().isAnyChestNeeded(player, x, y, z); + anyChest = plugin.getAnySilentChest().isAnyChestNeeded(player, x, y, z); } catch (Exception e) { player.sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit."); @@ -117,7 +127,7 @@ public class OpenInvPlayerListener implements Listener { // If the anyChest or silentChest is active if (anyChest || silentChest) { - if (!OpenInv.getAnySilentChest().activateChest(player, anyChest, silentChest, x, y, z)) { + if (!plugin.getAnySilentChest().activateChest(player, anyChest, silentChest, x, y, z)) { event.setCancelled(true); } } @@ -128,7 +138,7 @@ public class OpenInvPlayerListener implements Listener { // Signs if (block.getState() instanceof Sign) { try { - Sign sign = ((Sign) block.getState()); + Sign sign = (Sign) block.getState(); if (OpenInv.hasPermission(player, Permissions.PERM_OPENINV) && sign.getLine(0).equalsIgnoreCase("[openinv]")) { String text = sign.getLine(1).trim() + sign.getLine(2).trim() + sign.getLine(3).trim(); player.performCommand("openinv " + text); @@ -143,7 +153,7 @@ public class OpenInvPlayerListener implements Listener { } case RIGHT_CLICK_AIR: // OpenInv item - if (player.getInventory().getItemInMainHand().getType() == OpenInv.getOpenInvItem() && OpenInv.getPlayerItemOpenInvStatus(player) && OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) { + if (player.getInventory().getItemInMainHand().getType() == configuration.getOpenInvItem() && configuration.getPlayerItemOpenInvStatus(player) && OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) { player.performCommand("openinv"); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index aaaca8e..e5093ff 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,6 +2,7 @@ name: OpenInv main: com.lishid.openinv.OpenInv version: 2.3.6 author: lishid +authors: [ShadowRanger] description: > This plugin allows you to open a player's inventory as a chest and interact with it in real time. commands: From b8f4589b87b6ccb37b2a956b9377bc09509d36d9 Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Thu, 10 Mar 2016 13:47:17 +1100 Subject: [PATCH 2/7] General refactoring --- pom.xml | 2 +- .../com/lishid/openinv/Configuration.java | 9 ++++++++- src/main/java/com/lishid/openinv/OpenInv.java | 20 +++++++++---------- .../openinv/commands/SilentChestCommand.java | 4 ++-- .../commands/ToggleOpenInvCommand.java | 4 ++-- .../openinv/internal/AnySilentChest.java | 4 ++-- src/main/resources/plugin.yml | 2 +- 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index b92241e..bc36ee3 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.lishid openinv jar - 2.3.6 + 2.3.7 OpenInv http://dev.bukkit.org/bukkit-plugins/openinv/ diff --git a/src/main/java/com/lishid/openinv/Configuration.java b/src/main/java/com/lishid/openinv/Configuration.java index b391ccf..1a03d9a 100644 --- a/src/main/java/com/lishid/openinv/Configuration.java +++ b/src/main/java/com/lishid/openinv/Configuration.java @@ -46,13 +46,20 @@ public class Configuration { notifyAnyChest = plugin.getConfig().getBoolean("notify.any-chest", true); } + /** + * Reloads OpenInv's config settings. + */ + public void reload() { + load(); + } + /** * Saves a value to the plugin config at the specified path. * * @param path the path to set the value to * @param value the value to set to the path */ - public void saveToConfig(String path, Object value) { + private void saveToConfig(String path, Object value) { plugin.getConfig().set(path, value); plugin.saveConfig(); } diff --git a/src/main/java/com/lishid/openinv/OpenInv.java b/src/main/java/com/lishid/openinv/OpenInv.java index 08d563f..d6b18f0 100644 --- a/src/main/java/com/lishid/openinv/OpenInv.java +++ b/src/main/java/com/lishid/openinv/OpenInv.java @@ -56,6 +56,9 @@ public class OpenInv extends JavaPlugin { @Override public void onEnable() { + // Save the default config.yml if it doesn't already exist + saveDefaultConfig(); + // Config configuration = new Configuration(this); @@ -64,9 +67,6 @@ public class OpenInv extends JavaPlugin { inventoryAccess = new InventoryAccess(this); anySilentChest = new AnySilentChest(this); - // Save the default config.yml if it doesn't already exist - saveDefaultConfig(); - // Register the plugin's events PluginManager pm = getServer().getPluginManager(); @@ -155,27 +155,27 @@ public class OpenInv extends JavaPlugin { * @param player the player to show help to */ public static void showHelp(Player player) { - player.sendMessage(ChatColor.GREEN + "/openinv - Open a player's inventory."); + player.sendMessage(ChatColor.GREEN + "/openinv - Opens a player's inventory."); player.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)"); - player.sendMessage(ChatColor.GREEN + "/openender - Open a player's ender chest."); + player.sendMessage(ChatColor.GREEN + "/openender - Opens a player's ender chest."); player.sendMessage(ChatColor.GREEN + " (aliases: oe)"); player.sendMessage(ChatColor.GREEN + "/searchinv [minAmount] -"); - player.sendMessage(ChatColor.GREEN + " Search and list players having a specific item."); + player.sendMessage(ChatColor.GREEN + " Searches and lists players that have a specific item in their inventory."); player.sendMessage(ChatColor.GREEN + " (aliases: si)"); player.sendMessage(ChatColor.GREEN + "/searchender [minAmount] -"); - player.sendMessage(ChatColor.GREEN + " Search and list players having a specific item."); + player.sendMessage(ChatColor.GREEN + " Searches and lists players that have a specific item in their ender chest."); player.sendMessage(ChatColor.GREEN + " (aliases: se)"); - player.sendMessage(ChatColor.GREEN + "/toggleopeninv - Toggle the item openinv function."); + player.sendMessage(ChatColor.GREEN + "/toggleopeninv - Toggles the item openinv function."); player.sendMessage(ChatColor.GREEN + " (aliases: toi, toggleoi, toggleinv)"); - player.sendMessage(ChatColor.GREEN + "/anychest - Toggle the any chest function."); + player.sendMessage(ChatColor.GREEN + "/anychest - Toggles the any chest function."); player.sendMessage(ChatColor.GREEN + " (aliases: ac)"); - player.sendMessage(ChatColor.GREEN + "/silentchest - Toggle the silent chest function."); + player.sendMessage(ChatColor.GREEN + "/silentchest - Toggles the silent chest function."); player.sendMessage(ChatColor.GREEN + " (aliases: sc, silent)"); } diff --git a/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java b/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java index 73de4c5..6b14379 100644 --- a/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java @@ -54,7 +54,7 @@ public class SilentChestCommand implements CommandExecutor { if (args.length > 0) { if (args[0].equalsIgnoreCase("check")) { String status = configuration.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; - plugin.sendMessage(player, "Silent Chest is " + status + ChatColor.RESET + "."); + OpenInv.sendMessage(player, "Silent Chest is " + status + ChatColor.RESET + "."); return true; } } @@ -62,7 +62,7 @@ public class SilentChestCommand implements CommandExecutor { configuration.setPlayerSilentChestStatus(player, !configuration.getPlayerSilentChestStatus(player)); String status = configuration.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; - plugin.sendMessage(player, "Silent Chest is now " + status + ChatColor.RESET + "."); + OpenInv.sendMessage(player, "Silent Chest is now " + status + ChatColor.RESET + "."); return true; } diff --git a/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java b/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java index b6edd62..dcda462 100644 --- a/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java +++ b/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java @@ -54,7 +54,7 @@ public class ToggleOpenInvCommand implements CommandExecutor { if (args.length > 0) { if (args[0].equalsIgnoreCase("check")) { String status = configuration.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; - plugin.sendMessage(player, "OpenInv with " + ChatColor.GRAY + configuration.getOpenInvItem() + ChatColor.RESET + status + ChatColor.RESET + "."); + OpenInv.sendMessage(player, "OpenInv with " + ChatColor.GRAY + configuration.getOpenInvItem() + ChatColor.RESET + status + ChatColor.RESET + "."); return true; } } @@ -62,7 +62,7 @@ public class ToggleOpenInvCommand implements CommandExecutor { configuration.setPlayerItemOpenInvStatus(player, !configuration.getPlayerItemOpenInvStatus(player)); String status = configuration.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; - plugin.sendMessage(player, "OpenInv with " + ChatColor.GRAY + configuration.getOpenInvItem() + ChatColor.RESET + " is now " + status + ChatColor.RESET + "."); + OpenInv.sendMessage(player, "OpenInv with " + ChatColor.GRAY + configuration.getOpenInvItem() + ChatColor.RESET + " is now " + status + ChatColor.RESET + "."); return true; } diff --git a/src/main/java/com/lishid/openinv/internal/AnySilentChest.java b/src/main/java/com/lishid/openinv/internal/AnySilentChest.java index 1a22b97..ae43489 100644 --- a/src/main/java/com/lishid/openinv/internal/AnySilentChest.java +++ b/src/main/java/com/lishid/openinv/internal/AnySilentChest.java @@ -152,7 +152,7 @@ public class AnySilentChest { tileInventory = new SilentInventory(tileInventory); if (plugin.getConfiguration().notifySilentChest()) { - plugin.sendMessage(p, "You are opening a chest silently."); + OpenInv.sendMessage(p, "You are opening a chest silently."); } returnValue = false; @@ -161,7 +161,7 @@ public class AnySilentChest { player.openContainer(tileInventory); if (anyChest && plugin.getConfiguration().notifyAnyChest()) { - plugin.sendMessage(p, "You are opening a blocked chest."); + OpenInv.sendMessage(p, "You are opening a blocked chest."); } return returnValue; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e5093ff..d1be4db 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: OpenInv main: com.lishid.openinv.OpenInv -version: 2.3.6 +version: 2.3.7 author: lishid authors: [ShadowRanger] description: > From d42cc3e275e12f9417244b7c74ef6dced2bd8406 Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Thu, 10 Mar 2016 14:01:56 +1100 Subject: [PATCH 3/7] General refactoring --- src/main/java/com/lishid/openinv/OpenInv.java | 2 +- .../openinv/commands/SearchEnderCommand.java | 6 ------ .../openinv/internal/PlayerDataManager.java | 2 +- .../internal/SpecialPlayerInventory.java | 14 +++++++------ .../listeners/OpenInvPlayerListener.java | 21 +++++++++---------- .../com/lishid/openinv/utils/UUIDFetcher.java | 10 +++++++++ .../com/lishid/openinv/utils/UUIDUtil.java | 19 ++++++++++++----- 7 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/lishid/openinv/OpenInv.java b/src/main/java/com/lishid/openinv/OpenInv.java index d6b18f0..162ac72 100644 --- a/src/main/java/com/lishid/openinv/OpenInv.java +++ b/src/main/java/com/lishid/openinv/OpenInv.java @@ -78,7 +78,7 @@ public class OpenInv extends JavaPlugin { getCommand("openinv").setExecutor(new OpenInvCommand(this)); getCommand("openender").setExecutor(new OpenEnderCommand(this)); getCommand("searchinv").setExecutor(new SearchInvCommand(this)); - getCommand("searchender").setExecutor(new SearchEnderCommand(this)); + getCommand("searchender").setExecutor(new SearchEnderCommand()); getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand(this)); getCommand("anychest").setExecutor(new AnyChestCommand(this)); getCommand("silentchest").setExecutor(new SilentChestCommand(this)); diff --git a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java index 544b353..778a762 100644 --- a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java @@ -13,12 +13,6 @@ import com.lishid.openinv.Permissions; public class SearchEnderCommand implements CommandExecutor { - private OpenInv plugin; - - public SearchEnderCommand(OpenInv plugin) { - this.plugin = plugin; - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("searchender")) { diff --git a/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java b/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java index 2f2f8d9..87bab09 100644 --- a/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java +++ b/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java @@ -32,7 +32,7 @@ import net.minecraft.server.v1_9_R1.PlayerInteractManager; public class PlayerDataManager { - private OpenInv plugin; + private final OpenInv plugin; public PlayerDataManager(OpenInv plugin) { this.plugin = plugin; diff --git a/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java b/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java index 528a789..18adb68 100644 --- a/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java +++ b/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java @@ -18,17 +18,19 @@ package com.lishid.openinv.internal; import java.lang.reflect.Field; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftInventory; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; -// Volatile -import net.minecraft.server.v1_9_R1.*; - -import org.bukkit.craftbukkit.v1_9_R1.entity.*; -import org.bukkit.craftbukkit.v1_9_R1.inventory.*; - import com.lishid.openinv.OpenInv; +import net.minecraft.server.v1_9_R1.ContainerUtil; +import net.minecraft.server.v1_9_R1.EntityHuman; +import net.minecraft.server.v1_9_R1.ItemStack; +import net.minecraft.server.v1_9_R1.PlayerInventory; + public class SpecialPlayerInventory extends PlayerInventory { private final CraftInventory inventory = new CraftInventory(this); diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java index 7db5c9b..9c2f9e4 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java @@ -56,9 +56,9 @@ public class OpenInvPlayerListener implements Listener { inventory.playerOnline(event.getPlayer()); } - SpecialEnderChest chest = OpenInv.enderChests.get(player.getUniqueId()); - if (chest != null) { - chest.playerOnline(event.getPlayer()); + SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); + if (enderChest != null) { + enderChest.playerOnline(event.getPlayer()); } } @@ -71,9 +71,9 @@ public class OpenInvPlayerListener implements Listener { inventory.playerOffline(); } - SpecialEnderChest chest = OpenInv.enderChests.get(player.getUniqueId()); - if (chest != null) { - chest.playerOffline(); + SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); + if (enderChest != null) { + enderChest.playerOffline(); } } @@ -118,8 +118,7 @@ public class OpenInvPlayerListener implements Listener { if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && configuration.getPlayerAnyChestStatus(player)) { try { anyChest = plugin.getAnySilentChest().isAnyChestNeeded(player, x, y, z); - } - catch (Exception e) { + } catch (Exception e) { player.sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit."); e.printStackTrace(); } @@ -139,13 +138,13 @@ public class OpenInvPlayerListener implements Listener { if (block.getState() instanceof Sign) { try { Sign sign = (Sign) block.getState(); + if (OpenInv.hasPermission(player, Permissions.PERM_OPENINV) && sign.getLine(0).equalsIgnoreCase("[openinv]")) { String text = sign.getLine(1).trim() + sign.getLine(2).trim() + sign.getLine(3).trim(); player.performCommand("openinv " + text); } - } - catch (Exception e) { - player.sendMessage("Internal Error."); + } catch (Exception e) { + player.sendMessage(ChatColor.RED + "An internal error occured."); e.printStackTrace(); } diff --git a/src/main/java/com/lishid/openinv/utils/UUIDFetcher.java b/src/main/java/com/lishid/openinv/utils/UUIDFetcher.java index 2784fb4..cc0499a 100644 --- a/src/main/java/com/lishid/openinv/utils/UUIDFetcher.java +++ b/src/main/java/com/lishid/openinv/utils/UUIDFetcher.java @@ -15,6 +15,7 @@ import org.json.simple.parser.JSONParser; import com.google.common.collect.ImmutableList; public class UUIDFetcher implements Callable> { + private static final double PROFILES_PER_REQUEST = 100; private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; private final JSONParser jsonParser = new JSONParser(); @@ -34,11 +35,13 @@ public class UUIDFetcher implements Callable> { public Map call() throws Exception { Map uuidMap = new HashMap(); int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); + for (int i = 0; i < requests; i++) { HttpURLConnection connection = createConnection(); String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); writeBody(connection, body); JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); + for (Object profile : array) { JSONObject jsonProfile = (JSONObject) profile; String id = (String) jsonProfile.get("id"); @@ -46,10 +49,12 @@ public class UUIDFetcher implements Callable> { UUID uuid = UUIDFetcher.getUUID(id); uuidMap.put(name.toLowerCase(), uuid); } + if (rateLimiting && i != requests - 1) { Thread.sleep(100L); } } + return uuidMap; } @@ -62,12 +67,14 @@ public class UUIDFetcher implements Callable> { private static HttpURLConnection createConnection() throws Exception { URL url = new URL(PROFILE_URL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); + return connection; } @@ -79,6 +86,7 @@ public class UUIDFetcher implements Callable> { ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); + return byteBuffer.array(); } @@ -86,9 +94,11 @@ public class UUIDFetcher implements Callable> { if (array.length != 16) { throw new IllegalArgumentException("Illegal byte array length: " + array.length); } + ByteBuffer byteBuffer = ByteBuffer.wrap(array); long mostSignificant = byteBuffer.getLong(); long leastSignificant = byteBuffer.getLong(); + return new UUID(mostSignificant, leastSignificant); } diff --git a/src/main/java/com/lishid/openinv/utils/UUIDUtil.java b/src/main/java/com/lishid/openinv/utils/UUIDUtil.java index ea7c038..f7faad1 100644 --- a/src/main/java/com/lishid/openinv/utils/UUIDUtil.java +++ b/src/main/java/com/lishid/openinv/utils/UUIDUtil.java @@ -10,7 +10,10 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; -public class UUIDUtil { +public final class UUIDUtil { + + private UUIDUtil() {} + private static Player getPlayer(String name) { Validate.notNull(name, "Name cannot be null"); @@ -22,10 +25,12 @@ public class UUIDUtil { for (Player player : players) { if (player.getName().toLowerCase().startsWith(lowerName)) { int curDelta = player.getName().length() - lowerName.length(); + if (curDelta < delta) { found = player; delta = curDelta; } + if (curDelta == 0) break; } } @@ -39,14 +44,19 @@ public class UUIDUtil { return offlinePlayer.hasPlayedBefore() ? offlinePlayer.getUniqueId() : null; } + /** + * Returns the UUID of a player by their name. + * + * @param name the name of the player to get the UUID of + * @return the player's UUID or null + */ public static UUID getUUIDOf(String name) { UUID uuid; Player player = getPlayer(name); if (player != null) { uuid = player.getUniqueId(); - } - else { + } else { if (Bukkit.getServer().getOnlineMode()) { if (!Bukkit.getServer().isPrimaryThread()) { UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name)); @@ -55,8 +65,7 @@ public class UUIDUtil { try { response = fetcher.call(); uuid = response.get(name.toLowerCase()); - } - catch (Exception e) { + } catch (Exception e) { uuid = getUUIDLocally(name); } } else { From 99a7359be35c73e5e5ded6dadd644b0dadf1ee9b Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Thu, 10 Mar 2016 14:04:20 +1100 Subject: [PATCH 4/7] General refactoring --- .../java/com/lishid/openinv/commands/SearchEnderCommand.java | 2 +- src/main/java/com/lishid/openinv/commands/SearchInvCommand.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java index 778a762..aa30025 100644 --- a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java @@ -42,7 +42,7 @@ public class SearchEnderCommand implements CommandExecutor { } if (material == null) { - sender.sendMessage(ChatColor.RED + "Unknown item"); + sender.sendMessage(ChatColor.RED + "Unknown item."); return false; } diff --git a/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java b/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java index b7eb5df..481b635 100644 --- a/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java @@ -64,7 +64,7 @@ public class SearchInvCommand implements CommandExecutor { } if (material == null) { - sender.sendMessage(ChatColor.RED + "Unknown item"); + sender.sendMessage(ChatColor.RED + "Unknown item."); return false; } From c7f38adb3f19bf8e18627d59c53f1bb99f02bf4d Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Tue, 12 Apr 2016 14:25:09 +1000 Subject: [PATCH 5/7] Implement cbarber's player data glitch and online/offline event changes --- pom.xml | 4 ++-- .../openinv/commands/OpenInvCommand.java | 1 + .../openinv/internal/SpecialEnderChest.java | 2 +- .../internal/SpecialPlayerInventory.java | 7 ++++--- .../listeners/OpenInvPlayerListener.java | 21 ++++++++++++++----- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index bc36ee3..183edbe 100644 --- a/pom.xml +++ b/pom.xml @@ -23,14 +23,14 @@ org.bukkit bukkit - 1.9-R0.1-SNAPSHOT + 1.9.2-R0.1-SNAPSHOT provided org.bukkit craftbukkit - 1.9-R0.1-SNAPSHOT + 1.9.2-R0.1-SNAPSHOT provided diff --git a/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java b/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java index ce7e8ba..66effa6 100644 --- a/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java +++ b/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java @@ -106,6 +106,7 @@ public class OpenInvCommand implements CommandExecutor { if (player == null) { return; } + openInventory(player, target); } }); diff --git a/src/main/java/com/lishid/openinv/internal/SpecialEnderChest.java b/src/main/java/com/lishid/openinv/internal/SpecialEnderChest.java index f8b74af..f2adfde 100644 --- a/src/main/java/com/lishid/openinv/internal/SpecialEnderChest.java +++ b/src/main/java/com/lishid/openinv/internal/SpecialEnderChest.java @@ -51,7 +51,6 @@ public class SpecialEnderChest extends InventorySubcontainer { private void saveOnExit() { if (transaction.isEmpty() && !playerOnline) { owner.saveData(); - OpenInv.enderChests.remove(owner.getUniqueId()); } } @@ -82,6 +81,7 @@ public class SpecialEnderChest extends InventorySubcontainer { public void onClose(CraftHumanEntity who) { super.onClose(who); saveOnExit(); + OpenInv.enderChests.remove(owner.getUniqueId()); } @Override diff --git a/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java b/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java index 18adb68..b0994ad 100644 --- a/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java +++ b/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java @@ -35,15 +35,14 @@ public class SpecialPlayerInventory extends PlayerInventory { private final CraftInventory inventory = new CraftInventory(this); private final ItemStack[] extra = new ItemStack[4]; - private final ItemStack[][] arrays; private final CraftPlayer owner; + private ItemStack[][] arrays; private boolean playerOnline; public SpecialPlayerInventory(Player p, boolean online) { super(((CraftPlayer) p).getHandle()); this.owner = (CraftPlayer) p; reflectContents(getClass().getSuperclass(), player.inventory, this); - this.arrays = new ItemStack[][] { this.items, this.armor, this.extraSlots, this.extra }; this.playerOnline = online; OpenInv.inventories.put(owner.getUniqueId(), this); } @@ -70,6 +69,8 @@ public class SpecialPlayerInventory extends PlayerInventory { } catch (IllegalAccessException e) { e.printStackTrace(); } + + arrays = new ItemStack[][] { this.items, this.armor, this.extraSlots, this.extra }; } public Inventory getBukkitInventory() { @@ -79,7 +80,6 @@ public class SpecialPlayerInventory extends PlayerInventory { private void saveOnExit() { if (transaction.isEmpty() && !playerOnline) { owner.saveData(); - OpenInv.inventories.remove(owner.getUniqueId()); } } @@ -107,6 +107,7 @@ public class SpecialPlayerInventory extends PlayerInventory { public void onClose(CraftHumanEntity who) { super.onClose(who); this.saveOnExit(); + OpenInv.inventories.remove(owner.getUniqueId()); } @Override diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java index 9c2f9e4..2eb3ff1 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java @@ -30,6 +30,7 @@ import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.scheduler.BukkitRunnable; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; @@ -62,18 +63,28 @@ public class OpenInvPlayerListener implements Listener { } } - @EventHandler(priority = EventPriority.MONITOR) + @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { Player player = event.getPlayer(); - SpecialPlayerInventory inventory = OpenInv.inventories.get(player.getUniqueId()); + final SpecialPlayerInventory inventory = OpenInv.inventories.get(player.getUniqueId()); if (inventory != null) { - inventory.playerOffline(); + new BukkitRunnable() { + @Override + public void run() { + inventory.playerOffline(); + } + }.runTaskLater(plugin, 1); } - SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); + final SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); if (enderChest != null) { - enderChest.playerOffline(); + new BukkitRunnable() { + @Override + public void run() { + enderChest.playerOffline(); + } + }.runTaskLater(plugin, 1); } } From 4811e53d127c8954cd753179356a06d4b7d981ae Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Tue, 12 Apr 2016 14:33:44 +1000 Subject: [PATCH 6/7] General refactoring --- src/main/java/com/lishid/openinv/OpenInv.java | 4 ++-- .../lishid/openinv/listeners/OpenInvPlayerListener.java | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/lishid/openinv/OpenInv.java b/src/main/java/com/lishid/openinv/OpenInv.java index 162ac72..198faf0 100644 --- a/src/main/java/com/lishid/openinv/OpenInv.java +++ b/src/main/java/com/lishid/openinv/OpenInv.java @@ -48,12 +48,12 @@ public class OpenInv extends JavaPlugin { public static final Map inventories = new HashMap(); public static final Map enderChests = new HashMap(); + private Configuration configuration; + private PlayerDataManager playerLoader; private InventoryAccess inventoryAccess; private AnySilentChest anySilentChest; - private Configuration configuration; - @Override public void onEnable() { // Save the default config.yml if it doesn't already exist diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java index 2eb3ff1..f3eca18 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java @@ -77,14 +77,9 @@ public class OpenInvPlayerListener implements Listener { }.runTaskLater(plugin, 1); } - final SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); + SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); if (enderChest != null) { - new BukkitRunnable() { - @Override - public void run() { - enderChest.playerOffline(); - } - }.runTaskLater(plugin, 1); + enderChest.playerOffline(); } } From 90ea07308919616e9a5920dd146baec070da9bdd Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Tue, 12 Apr 2016 14:34:46 +1000 Subject: [PATCH 7/7] General refactoring --- .../lishid/openinv/listeners/OpenInvPlayerListener.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java index f3eca18..2eb3ff1 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java @@ -77,9 +77,14 @@ public class OpenInvPlayerListener implements Listener { }.runTaskLater(plugin, 1); } - SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); + final SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); if (enderChest != null) { - enderChest.playerOffline(); + new BukkitRunnable() { + @Override + public void run() { + enderChest.playerOffline(); + } + }.runTaskLater(plugin, 1); } }