diff --git a/gpl.txt b/LICENSE.txt similarity index 100% rename from gpl.txt rename to LICENSE.txt diff --git a/pom.xml b/pom.xml index ef32711..63f5b9e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.lishid openinv jar - 2.3.0 + 2.3.1 OpenInv http://dev.bukkit.org/bukkit-plugins/openinv/ diff --git a/src/main/java/com/lishid/openinv/ConfigUpdater.java b/src/main/java/com/lishid/openinv/ConfigUpdater.java new file mode 100644 index 0000000..5d4f42e --- /dev/null +++ b/src/main/java/com/lishid/openinv/ConfigUpdater.java @@ -0,0 +1,161 @@ +package com.lishid.openinv; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; + +import com.lishid.openinv.utils.UUIDUtil; + +public class ConfigUpdater { + private final OpenInv plugin; + + private static final int LATEST_CONFIG_VERSION = 2; + + public ConfigUpdater(OpenInv plugin) { + this.plugin = plugin; + } + + private int getConfigVersion() { + return plugin.getConfig().getInt("config-version", 1); + } + + private boolean isConfigOutdated() { + return getConfigVersion() < LATEST_CONFIG_VERSION; + } + + public void checkForUpdates() { + if (isConfigOutdated()) { + plugin.getLogger().info("[Config] Update found! Performing update..."); + updateConfig(); + } else { + plugin.getLogger().info("[Config] Update not found. Config is already up-to-date."); + } + } + + private void updateConfig() { + // Get the old config settings + int itemOpenInvItemId = plugin.getConfig().getInt("ItemOpenInvItemID", 280); + boolean checkForUpdates = plugin.getConfig().getBoolean("CheckForUpdates", true); + boolean notifySilentChest = plugin.getConfig().getBoolean("NotifySilentChest", true); + boolean notifyAnyChest = plugin.getConfig().getBoolean("NotifyAnyChest", true); + + Map anyChestToggles = null; + Map itemOpenInvToggles = null; + Map silentChestToggles = null; + + if (plugin.getConfig().isSet("AnyChest")) { + anyChestToggles = updateAnyChestToggles(); + } + + if (plugin.getConfig().isSet("ItemOpenInv")) { + itemOpenInvToggles = updateItemOpenInvToggles(); + } + + if (plugin.getConfig().isSet("SilentChest")) { + silentChestToggles = updateSilentChestToggles(); + } + + // Clear the old config + for (String key : plugin.getConfig().getKeys(false)) { + plugin.getConfig().set(key, null); + } + + // Set the new config options + plugin.getConfig().set("config-version", LATEST_CONFIG_VERSION); + plugin.getConfig().set("check-for-updates", checkForUpdates); + plugin.getConfig().set("items.open-inv", getMaterialById(itemOpenInvItemId).toString()); + plugin.getConfig().set("notify.any-chest", notifyAnyChest); + plugin.getConfig().set("notify.silent-chest", notifySilentChest); + + if (anyChestToggles != null && !anyChestToggles.isEmpty()) { + for (Map.Entry entry : anyChestToggles.entrySet()) { + plugin.getConfig().set("toggles.any-chest." + entry.getKey(), entry.getValue()); + } + } + + if (itemOpenInvToggles != null && !itemOpenInvToggles.isEmpty()) { + for (Map.Entry entry : itemOpenInvToggles.entrySet()) { + plugin.getConfig().set("toggles.items.open-inv." + entry.getKey(), entry.getValue()); + } + } + + if (silentChestToggles != null && !silentChestToggles.isEmpty()) { + for (Map.Entry entry : silentChestToggles.entrySet()) { + plugin.getConfig().set("toggles.silent-chest." + entry.getKey(), entry.getValue()); + } + } + + // Save the new config + plugin.saveConfig(); + + plugin.getLogger().info("[Config] Update complete."); + + } + + private Map updateAnyChestToggles() { + Map toggles = new HashMap(); + + ConfigurationSection anyChestSection = plugin.getConfig().getConfigurationSection("AnyChest"); + Set keys = anyChestSection.getKeys(false); + if (keys == null || keys.isEmpty()) return null; + + for (String playerName : keys) { + UUID uuid = UUIDUtil.getUUIDOf(playerName); + if (uuid != null) { + boolean toggled = anyChestSection.getBoolean(playerName + ".toggle", false); + toggles.put(uuid, toggled); + } + } + + return toggles; + } + + private Map updateItemOpenInvToggles() { + Map toggles = new HashMap(); + + ConfigurationSection anyChestSection = plugin.getConfig().getConfigurationSection("ItemOpenInv"); + Set keys = anyChestSection.getKeys(false); + if (keys == null || keys.isEmpty()) return null; + + for (String playerName : keys) { + UUID uuid = UUIDUtil.getUUIDOf(playerName); + if (uuid != null) { + boolean toggled = anyChestSection.getBoolean(playerName + ".toggle", false); + toggles.put(uuid, toggled); + } + } + + return toggles; + } + + private Map updateSilentChestToggles() { + Map toggles = new HashMap(); + + ConfigurationSection silentChestSection = plugin.getConfig().getConfigurationSection("SilentChest"); + Set keys = silentChestSection.getKeys(false); + if (keys == null || keys.isEmpty()) return null; + + for (String playerName : keys) { + UUID uuid = UUIDUtil.getUUIDOf(playerName); + if (uuid != null) { + boolean toggled = silentChestSection.getBoolean(playerName + ".toggle", false); + toggles.put(uuid, toggled); + } + } + + return toggles; + } + + @SuppressWarnings("deprecation") + private Material getMaterialById(int id) { + Material material = Material.getMaterial(id); + if (material == null) { + material = Material.STICK; + } + return material; + } +} diff --git a/src/main/java/com/lishid/openinv/OpenInv.java b/src/main/java/com/lishid/openinv/OpenInv.java index d67bf99..e300fc0 100644 --- a/src/main/java/com/lishid/openinv/OpenInv.java +++ b/src/main/java/com/lishid/openinv/OpenInv.java @@ -19,16 +19,21 @@ package com.lishid.openinv; import java.util.HashMap; import java.util.Map; import java.util.UUID; -import java.util.logging.Logger; import org.bukkit.ChatColor; -import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.Material; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.permissions.Permissible; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -import com.lishid.openinv.commands.*; +import com.lishid.openinv.commands.AnyChestCommand; +import com.lishid.openinv.commands.OpenEnderCommand; +import com.lishid.openinv.commands.OpenInvCommand; +import com.lishid.openinv.commands.SearchInvCommand; +import com.lishid.openinv.commands.SilentChestCommand; +import com.lishid.openinv.commands.ToggleOpenInvCommand; import com.lishid.openinv.internal.AnySilentChest; import com.lishid.openinv.internal.InventoryAccess; import com.lishid.openinv.internal.PlayerDataManager; @@ -44,93 +49,67 @@ import com.lishid.openinv.listeners.OpenInvPlayerListener; * @author lishid */ public class OpenInv extends JavaPlugin { - public static final Logger logger = Logger.getLogger("Minecraft.OpenInv"); - public static final Map inventories = new HashMap(); public static final Map enderChests = new HashMap(); public static OpenInv mainPlugin; - public static PlayerDataManager playerLoader; - public static InventoryAccess inventoryAccess; - public static AnySilentChest anySilentChest; + private static PlayerDataManager playerLoader; + private static InventoryAccess inventoryAccess; + private static AnySilentChest anySilentChest; @Override public void onEnable() { - // Get plugin manager - PluginManager pm = getServer().getPluginManager(); + // Plugin + mainPlugin = this; + // Config + ConfigUpdater configUpdater = new ConfigUpdater(this); + configUpdater.checkForUpdates(); + + // Initialize playerLoader = new PlayerDataManager(); inventoryAccess = new InventoryAccess(); anySilentChest = new AnySilentChest(); - mainPlugin = this; - FileConfiguration config = getConfig(); - config.set("CheckForUpdates", config.getBoolean("CheckForUpdates", true)); - config.set("NotifySilentChest", config.getBoolean("NotifySilentChest", true)); - config.set("NotifyAnyChest", config.getBoolean("NotifyAnyChest", true)); - config.set("ItemOpenInvItemID", config.getInt("ItemOpenInvItemID", 280)); - config.addDefault("ItemOpenInvItemID", 280); - config.addDefault("CheckForUpdates", true); - config.addDefault("NotifySilentChest", true); - config.addDefault("NotifyAnyChest", true); - config.options().copyDefaults(true); - saveConfig(); + // Save the default config.yml if it doesn't already exist + saveDefaultConfig(); + + // Register the plugin's events & commands + registerEvents(); + registerCommands(); + } + + private void registerEvents() { + PluginManager pm = getServer().getPluginManager(); pm.registerEvents(new OpenInvPlayerListener(), this); pm.registerEvents(new OpenInvEntityListener(), this); pm.registerEvents(new OpenInvInventoryListener(), this); - - getCommand("openinv").setExecutor(new OpenInvPluginCommand(this)); - getCommand("searchinv").setExecutor(new SearchInvPluginCommand()); - getCommand("toggleopeninv").setExecutor(new ToggleOpenInvPluginCommand()); - getCommand("silentchest").setExecutor(new SilentChestPluginCommand()); - getCommand("anychest").setExecutor(new AnyChestPluginCommand()); - getCommand("openender").setExecutor(new OpenEnderPluginCommand(this)); } - public static boolean notifySilentChest() { - return mainPlugin.getConfig().getBoolean("NotifySilentChest", true); + private void registerCommands() { + getCommand("openinv").setExecutor(new OpenInvCommand(this)); + getCommand("searchinv").setExecutor(new SearchInvCommand()); + getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand()); + getCommand("silentchest").setExecutor(new SilentChestCommand()); + getCommand("anychest").setExecutor(new AnyChestCommand()); + getCommand("openender").setExecutor(new OpenEnderCommand(this)); } - public static boolean notifyAnyChest() { - return mainPlugin.getConfig().getBoolean("NotifyAnyChest", true); + public static PlayerDataManager getPlayerLoader() { + return playerLoader; } - public static boolean getPlayerItemOpenInvStatus(String name) { - return mainPlugin.getConfig().getBoolean("ItemOpenInv." + name.toLowerCase() + ".toggle", false); + public static InventoryAccess getInventoryAccess() { + return inventoryAccess; } - public static void setPlayerItemOpenInvStatus(String name, boolean status) { - mainPlugin.getConfig().set("ItemOpenInv." + name.toLowerCase() + ".toggle", status); - mainPlugin.saveConfig(); - } - - public static boolean getPlayerSilentChestStatus(String name) { - return mainPlugin.getConfig().getBoolean("SilentChest." + name.toLowerCase() + ".toggle", false); - } - - public static void setPlayerSilentChestStatus(String name, boolean status) { - mainPlugin.getConfig().set("SilentChest." + name.toLowerCase() + ".toggle", status); - mainPlugin.saveConfig(); - } - - public static boolean getPlayerAnyChestStatus(String name) { - return mainPlugin.getConfig().getBoolean("AnyChest." + name.toLowerCase() + ".toggle", true); - } - - public static void setPlayerAnyChestStatus(String name, boolean status) { - mainPlugin.getConfig().set("AnyChest." + name.toLowerCase() + ".toggle", status); - mainPlugin.saveConfig(); - } - - public static int getItemOpenInvItem() { - if (mainPlugin.getConfig().get("ItemOpenInvItemID") == null) { - saveToConfig("ItemOpenInvItemID", 280); - } - return mainPlugin.getConfig().getInt("ItemOpenInvItemID", 280); + public static AnySilentChest getAnySilentChest() { + return anySilentChest; } + /* public static Object getFromConfig(String data, Object defaultValue) { Object val = mainPlugin.getConfig().get(data); if (val == null) { @@ -141,27 +120,83 @@ public class OpenInv extends JavaPlugin { return val; } } + */ public static void saveToConfig(String data, Object value) { mainPlugin.getConfig().set(data, value); mainPlugin.saveConfig(); } - /** - * Log an information - */ - public static void log(String text) { - logger.info("[OpenInv] " + text); + 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"); + return Material.getMaterial(itemName); + } + + 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(), false); + } + + 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); } /** - * Log an error + * Logs a given message to console. + * + * @param text the text to log + */ + public static void log(String text) { + mainPlugin.getLogger().info("[OpenInv] " + text); + } + + /** + * Logs an error to console. + * + * @param e the throwable error to log */ public static void log(Throwable e) { - logger.severe("[OpenInv] " + e.toString()); + mainPlugin.getLogger().severe("[OpenInv] " + e.toString()); e.printStackTrace(); } + /** + * Sends a specified message to a given CommandSender with the OpenInv prefix. + * + * @param sender the CommandSender to message + * @param message the message to send to the player + */ + public static void sendMessage(CommandSender sender, String message) { + sender.sendMessage(ChatColor.AQUA + "[OpenInv] " + ChatColor.WHITE + message); + } + public static void showHelp(Player player) { player.sendMessage(ChatColor.GREEN + "/openinv - Open a player's inventory"); player.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)"); diff --git a/src/main/java/com/lishid/openinv/commands/AnyChestPluginCommand.java b/src/main/java/com/lishid/openinv/commands/AnyChestCommand.java similarity index 68% rename from src/main/java/com/lishid/openinv/commands/AnyChestPluginCommand.java rename to src/main/java/com/lishid/openinv/commands/AnyChestCommand.java index 21423fe..3bcde82 100644 --- a/src/main/java/com/lishid/openinv/commands/AnyChestPluginCommand.java +++ b/src/main/java/com/lishid/openinv/commands/AnyChestCommand.java @@ -25,30 +25,34 @@ import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; -public class AnyChestPluginCommand implements CommandExecutor { +public class AnyChestCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("anychest")) { if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "You can't use this from the console."); + sender.sendMessage(ChatColor.RED + "You can't use this command from the console."); return true; } + if (!OpenInv.hasPermission(sender, Permissions.PERM_ANYCHEST)) { - sender.sendMessage(ChatColor.RED + "You do not have permission to use anychest."); + sender.sendMessage(ChatColor.RED + "You do not have permission to use any chest."); return true; } + Player player = (Player) sender; + if (args.length > 0) { if (args[0].equalsIgnoreCase("check")) { - if (OpenInv.getPlayerAnyChestStatus(sender.getName())) - sender.sendMessage("AnyChest is ON."); - else - sender.sendMessage("AnyChest is OFF."); + String status = OpenInv.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + OpenInv.sendMessage(player, "Any Chest is " + status + ChatColor.RESET + "."); + return true; } } - OpenInv.setPlayerAnyChestStatus(sender.getName(), !OpenInv.getPlayerAnyChestStatus(sender.getName())); - sender.sendMessage("AnyChest is now " + (OpenInv.getPlayerAnyChestStatus(sender.getName()) ? "On" : "Off") + "."); + OpenInv.setPlayerAnyChestStatus(player, !OpenInv.getPlayerAnyChestStatus(player)); + + String status = OpenInv.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/OpenEnderPluginCommand.java b/src/main/java/com/lishid/openinv/commands/OpenEnderCommand.java similarity index 92% rename from src/main/java/com/lishid/openinv/commands/OpenEnderPluginCommand.java rename to src/main/java/com/lishid/openinv/commands/OpenEnderCommand.java index 8c934a9..4fbaa0c 100644 --- a/src/main/java/com/lishid/openinv/commands/OpenEnderPluginCommand.java +++ b/src/main/java/com/lishid/openinv/commands/OpenEnderCommand.java @@ -32,11 +32,11 @@ import com.lishid.openinv.Permissions; import com.lishid.openinv.internal.SpecialEnderChest; import com.lishid.openinv.utils.UUIDUtil; -public class OpenEnderPluginCommand implements CommandExecutor { +public class OpenEnderCommand implements CommandExecutor { private final OpenInv plugin; private final Map openEnderHistory = new ConcurrentHashMap(); - public OpenEnderPluginCommand(OpenInv plugin) { + public OpenEnderCommand(OpenInv plugin) { this.plugin = plugin; } @@ -44,7 +44,7 @@ public class OpenEnderPluginCommand implements CommandExecutor { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("openender")) { if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "You can't use this from the console."); + sender.sendMessage(ChatColor.RED + "You can't use this command from the console."); return true; } @@ -82,6 +82,11 @@ public class OpenEnderPluginCommand implements CommandExecutor { } else { uuid = UUIDUtil.getUUIDOf(args[0]); + + if (uuid == null) { + player.sendMessage(ChatColor.RED + "Player not found!"); + return true; + } } final UUID playerUUID = player.getUniqueId(); @@ -93,11 +98,12 @@ public class OpenEnderPluginCommand implements CommandExecutor { @Override public void run() { // Try loading the player's data asynchronously - final Player target = OpenInv.playerLoader.loadPlayer(uuid); + final Player target = OpenInv.getPlayerLoader().loadPlayer(uuid); if (target == null) { player.sendMessage(ChatColor.RED + "Player not found!"); return; } + // Open target's inventory synchronously Bukkit.getScheduler().runTask(plugin, new Runnable() { @Override @@ -129,7 +135,7 @@ public class OpenEnderPluginCommand implements CommandExecutor { } if (target != player && !OpenInv.hasPermission(player, Permissions.PERM_ENDERCHEST_ALL)) { - player.sendMessage(ChatColor.RED + "You do not have permission to access other player's ender chests"); + player.sendMessage(ChatColor.RED + "You do not have permission to access other player's ender chests."); return; } diff --git a/src/main/java/com/lishid/openinv/commands/OpenInvPluginCommand.java b/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java similarity index 93% rename from src/main/java/com/lishid/openinv/commands/OpenInvPluginCommand.java rename to src/main/java/com/lishid/openinv/commands/OpenInvCommand.java index a3e8629..2bfbb8c 100644 --- a/src/main/java/com/lishid/openinv/commands/OpenInvPluginCommand.java +++ b/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java @@ -32,11 +32,11 @@ import com.lishid.openinv.Permissions; import com.lishid.openinv.internal.SpecialPlayerInventory; import com.lishid.openinv.utils.UUIDUtil; -public class OpenInvPluginCommand implements CommandExecutor { +public class OpenInvCommand implements CommandExecutor { private final OpenInv plugin; private final Map openInvHistory = new ConcurrentHashMap(); - public OpenInvPluginCommand(OpenInv plugin) { + public OpenInvCommand(OpenInv plugin) { this.plugin = plugin; } @@ -44,7 +44,7 @@ public class OpenInvPluginCommand implements CommandExecutor { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("openinv")) { if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "You can't use this from the console."); + sender.sendMessage(ChatColor.RED + "You can't use this command from the console."); return true; } if (!OpenInv.hasPermission(sender, Permissions.PERM_OPENINV)) { @@ -75,6 +75,11 @@ public class OpenInvPluginCommand implements CommandExecutor { } else { uuid = UUIDUtil.getUUIDOf(args[0]); + + if (uuid == null) { + player.sendMessage(ChatColor.RED + "Player not found!"); + return true; + } } final UUID playerUUID = player.getUniqueId(); @@ -86,11 +91,12 @@ public class OpenInvPluginCommand implements CommandExecutor { @Override public void run() { // Try loading the player's data asynchronously - final Player target = OpenInv.playerLoader.loadPlayer(uuid); + final Player target = OpenInv.getPlayerLoader().loadPlayer(uuid); if (target == null) { player.sendMessage(ChatColor.RED + "Player not found!"); return; } + // Open target's inventory synchronously Bukkit.getScheduler().runTask(plugin, new Runnable() { @Override diff --git a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java new file mode 100644 index 0000000..658fa94 --- /dev/null +++ b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java @@ -0,0 +1,67 @@ +package com.lishid.openinv.commands; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.lishid.openinv.OpenInv; +import com.lishid.openinv.Permissions; + +public class SearchEnderCommand implements CommandExecutor { + // TODO + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (command.getName().equalsIgnoreCase("searchinv")) { + if (sender instanceof Player) { + if (!OpenInv.hasPermission(sender, Permissions.PERM_SEARCH)) { + sender.sendMessage(ChatColor.RED + "You do not have permission to search player ender chests."); + return true; + } + } + + Material material = null; + int count = 1; + + if (args.length >= 1) { + String[] gData = null; + gData = args[0].split(":"); + material = Material.matchMaterial(gData[0]); + } + if (args.length >= 2) { + try { + count = Integer.parseInt(args[1]); + } + catch (NumberFormatException ex) { + sender.sendMessage(ChatColor.RED + "'" + args[1] + "' is not a number!"); + return false; + } + } + + if (material == null) { + sender.sendMessage(ChatColor.RED + "Unknown item"); + return false; + } + + StringBuilder sb = new StringBuilder(); + + for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()) { + if (onlinePlayer.getInventory().contains(material, count)) { + sb.append(onlinePlayer.getName()); + sb.append(" "); + } + } + + String playerList = sb.toString(); + sender.sendMessage("Players with the item " + material.toString() + ": " + playerList); + + return true; + } + + return false; + } +} diff --git a/src/main/java/com/lishid/openinv/commands/SearchInvPluginCommand.java b/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java similarity index 83% rename from src/main/java/com/lishid/openinv/commands/SearchInvPluginCommand.java rename to src/main/java/com/lishid/openinv/commands/SearchInvCommand.java index 968e1ab..98d1527 100644 --- a/src/main/java/com/lishid/openinv/commands/SearchInvPluginCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java @@ -27,19 +27,17 @@ import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; -public class SearchInvPluginCommand implements CommandExecutor { +public class SearchInvCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("searchinv")) { if (sender instanceof Player) { if (!OpenInv.hasPermission(sender, Permissions.PERM_SEARCH)) { - sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories"); + sender.sendMessage(ChatColor.RED + "You do not have permission to search player inventories."); return true; } } - String playerList = ""; - Material material = null; int count = 1; @@ -63,12 +61,16 @@ public class SearchInvPluginCommand implements CommandExecutor { return false; } - for (Player templayer : Bukkit.getServer().getOnlinePlayers()) { - if (templayer.getInventory().contains(material, count)) { - playerList += templayer.getName() + " "; + StringBuilder sb = new StringBuilder(); + + for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()) { + if (onlinePlayer.getInventory().contains(material, count)) { + sb.append(onlinePlayer.getName()); + sb.append(" "); } } + String playerList = sb.toString(); sender.sendMessage("Players with the item " + material.toString() + ": " + playerList); return true; diff --git a/src/main/java/com/lishid/openinv/commands/SilentChestPluginCommand.java b/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java similarity index 69% rename from src/main/java/com/lishid/openinv/commands/SilentChestPluginCommand.java rename to src/main/java/com/lishid/openinv/commands/SilentChestCommand.java index 90bd3fc..ce94e88 100644 --- a/src/main/java/com/lishid/openinv/commands/SilentChestPluginCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SilentChestCommand.java @@ -25,30 +25,34 @@ import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; -public class SilentChestPluginCommand implements CommandExecutor { +public class SilentChestCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("silentchest")) { if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "You can't use this from the console."); + sender.sendMessage(ChatColor.RED + "You can't use this command from the console."); return true; } + if (!OpenInv.hasPermission(sender, Permissions.PERM_SILENT)) { sender.sendMessage(ChatColor.RED + "You do not have permission to use silent chest."); return true; } + Player player = (Player) sender; + if (args.length > 0) { if (args[0].equalsIgnoreCase("check")) { - if (OpenInv.getPlayerSilentChestStatus(sender.getName())) - sender.sendMessage("SilentChest is ON."); - else - sender.sendMessage("SilentChest is OFF."); + String status = OpenInv.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + OpenInv.sendMessage(player, "Silent Chest is " + status + ChatColor.RESET + "."); + return true; } } - OpenInv.setPlayerSilentChestStatus(sender.getName(), !OpenInv.getPlayerSilentChestStatus(sender.getName())); - sender.sendMessage("SilentChest is now " + (OpenInv.getPlayerSilentChestStatus(sender.getName()) ? "On" : "Off") + "."); + OpenInv.setPlayerSilentChestStatus(player, !OpenInv.getPlayerSilentChestStatus(player)); + + String status = OpenInv.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF"; + OpenInv.sendMessage(player, "Silent Chest is now " + status + ChatColor.RESET + "."); return true; } diff --git a/src/main/java/com/lishid/openinv/commands/ToggleOpenInvPluginCommand.java b/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java similarity index 60% rename from src/main/java/com/lishid/openinv/commands/ToggleOpenInvPluginCommand.java rename to src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java index b59f841..a024c24 100644 --- a/src/main/java/com/lishid/openinv/commands/ToggleOpenInvPluginCommand.java +++ b/src/main/java/com/lishid/openinv/commands/ToggleOpenInvCommand.java @@ -17,7 +17,6 @@ package com.lishid.openinv.commands; import org.bukkit.ChatColor; -import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -26,37 +25,35 @@ import org.bukkit.entity.Player; import com.lishid.openinv.OpenInv; import com.lishid.openinv.Permissions; -@SuppressWarnings("deprecation") -public class ToggleOpenInvPluginCommand implements CommandExecutor { +public class ToggleOpenInvCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("toggleopeninv")) { if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "You can't use this from the console."); + 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"); + sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories."); return true; } Player player = (Player) sender; + if (args.length > 0) { if (args[0].equalsIgnoreCase("check")) { - if (OpenInv.getPlayerItemOpenInvStatus(player.getName())) - player.sendMessage("OpenInv with " + Material.getMaterial(OpenInv.getItemOpenInvItem()).toString() + " is ON."); - else - player.sendMessage("OpenInv with " + Material.getMaterial(OpenInv.getItemOpenInvItem()).toString() + " is OFF."); + 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 + "."); + return true; } } - if (OpenInv.getPlayerItemOpenInvStatus(player.getName())) { - OpenInv.setPlayerItemOpenInvStatus(player.getName(), false); - player.sendMessage("OpenInv with " + Material.getMaterial(OpenInv.getItemOpenInvItem()).toString() + " is OFF."); - } - else { - OpenInv.setPlayerItemOpenInvStatus(player.getName(), true); - player.sendMessage("OpenInv with " + Material.getMaterial(OpenInv.getItemOpenInvItem()).toString() + " is ON."); - } + + OpenInv.setPlayerItemOpenInvStatus(player, !OpenInv.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 + "."); + 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 d668c82..213422e 100644 --- a/src/main/java/com/lishid/openinv/internal/AnySilentChest.java +++ b/src/main/java/com/lishid/openinv/internal/AnySilentChest.java @@ -28,7 +28,7 @@ import net.minecraft.server.v1_8_R3.*; import org.bukkit.craftbukkit.v1_8_R3.entity.*; public class AnySilentChest { - public boolean IsAnyChestNeeded(Player p, int x, int y, int z) { + 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); EntityPlayer player = ((CraftPlayer) p).getHandle(); @@ -81,7 +81,7 @@ public class AnySilentChest { return true; } - public boolean ActivateChest(Player p, boolean anychest, boolean silentchest, int x, int y, int z) { + public boolean activateChest(Player p, boolean anyChest, boolean silentChest, int x, int y, int z) { BlockPosition position = new BlockPosition(x, y, z); EntityPlayer player = ((CraftPlayer) p).getHandle(); World world = player.world; @@ -98,7 +98,7 @@ public class AnySilentChest { } ITileInventory tileInventory = (ITileInventory) tileEntity; - if (!anychest && this.topBlocking(world, position)) { + if (!anyChest && this.topBlocking(world, position)) { return true; } @@ -106,7 +106,7 @@ public class AnySilentChest { BlockPosition side = position.shift(direction); Block block = world.getType(side).getBlock(); if (block == chest) { - if (!anychest && this.topBlocking(world, side)) { + if (!anyChest && this.topBlocking(world, side)) { return true; } @@ -122,7 +122,7 @@ public class AnySilentChest { } boolean returnValue = true; - if (silentchest) { + if (silentChest) { tileInventory = new SilentInventory(tileInventory); if (OpenInv.notifySilentChest()) { p.sendMessage("You are opening a chest silently."); @@ -132,7 +132,7 @@ public class AnySilentChest { player.openContainer(tileInventory); - if (anychest && OpenInv.notifyAnyChest()) { + if (anyChest && OpenInv.notifyAnyChest()) { p.sendMessage("You are opening a blocked chest."); } diff --git a/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java b/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java index f167e28..c9e39e3 100644 --- a/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java +++ b/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java @@ -180,7 +180,7 @@ public class SpecialPlayerInventory extends PlayerInventory { } @Override - public void setItem(int i, ItemStack itemstack) { + public void setItem(int i, ItemStack itemStack) { ItemStack[] is = this.items; if (i >= is.length) { @@ -199,11 +199,11 @@ public class SpecialPlayerInventory extends PlayerInventory { // Effects if (is == this.extra) { - owner.getHandle().drop(itemstack, true); - itemstack = null; + owner.getHandle().drop(itemStack, true); + itemStack = null; } - is[i] = itemstack; + is[i] = itemStack; owner.getHandle().defaultContainer.b(); } diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvEntityListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvEntityListener.java index 5697cfb..3508af5 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvEntityListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvEntityListener.java @@ -19,37 +19,36 @@ package com.lishid.openinv.listeners; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import com.lishid.openinv.OpenInv; +import com.lishid.openinv.Permissions; -@SuppressWarnings("deprecation") public class OpenInvEntityListener implements Listener { @EventHandler(priority = EventPriority.LOWEST) - public void onEntityDamage(EntityDamageEvent event) { - if (event instanceof EntityDamageByEntityEvent) { - EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent) event; - Entity attacker = evt.getDamager(); - Entity defender = evt.getEntity(); + public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { + Entity attacker = event.getDamager(); + Entity defender = event.getEntity(); - if (!(attacker instanceof Player) || !(defender instanceof Player)) { - return; - } + if (!(attacker instanceof Player) || !(defender instanceof Player)) { + return; + } - Player player = (Player) attacker; + Player player = (Player) attacker; - if (!(player.getItemInHand().getType().getId() == OpenInv.getItemOpenInvItem()) || (!OpenInv.getPlayerItemOpenInvStatus(player.getName())) || !OpenInv.hasPermission(player, "OpenInv.openinv")) { + if (player.getItemInHand().getType() == OpenInv.getOpenInvItem()) { + if (!OpenInv.getPlayerItemOpenInvStatus(player) || !OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) { return; } Player target = (Player) defender; - player.performCommand("openinv " + target.getName()); - evt.setDamage(0); - evt.setCancelled(true); + event.setDamage(0); + event.setCancelled(true); + + player.performCommand("openinv " + target.getName()); } } } diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvInventoryListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvInventoryListener.java index bd100c6..6d3250e 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvInventoryListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvInventoryListener.java @@ -16,22 +16,22 @@ package com.lishid.openinv.listeners; +import org.bukkit.entity.HumanEntity; import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; import com.lishid.openinv.OpenInv; public class OpenInvInventoryListener implements Listener { - @EventHandler(priority = EventPriority.NORMAL) + @EventHandler public void onInventoryClick(InventoryClickEvent event) { - // If this is the top inventory - // if (event.getView().convertSlot(event.getRawSlot()) == event.getRawSlot()) - // { - if (!OpenInv.inventoryAccess.check(event.getInventory(), event.getWhoClicked())) { + Inventory inventory = event.getInventory(); + HumanEntity player = event.getWhoClicked(); + + if (!OpenInv.getInventoryAccess().check(inventory, player)) { event.setCancelled(true); } - // } } } \ No newline at end of file diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java index b78e606..07f8fc9 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java @@ -17,6 +17,8 @@ package com.lishid.openinv.listeners; import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.block.Chest; import org.bukkit.block.Sign; import org.bukkit.entity.Player; @@ -34,7 +36,6 @@ import com.lishid.openinv.Permissions; import com.lishid.openinv.internal.SpecialEnderChest; import com.lishid.openinv.internal.SpecialPlayerInventory; -@SuppressWarnings("deprecation") public class OpenInvPlayerListener implements Listener { @EventHandler(priority = EventPriority.LOWEST) public void onPlayerJoin(PlayerJoinEvent event) { @@ -70,35 +71,40 @@ public class OpenInvPlayerListener implements Listener { public void onPlayerInteract(PlayerInteractEvent event) { Player player = event.getPlayer(); - if (event.getPlayer().isSneaking()) { + if (player.isSneaking()) { return; } - if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.useInteractedBlock() == Result.DENY) { + Action action = event.getAction(); + + if (action == Action.RIGHT_CLICK_BLOCK && event.useInteractedBlock() == Result.DENY) { return; } - if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == org.bukkit.Material.ENDER_CHEST) { - if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && OpenInv.getPlayerSilentChestStatus(player.getName())) { + Block block = event.getClickedBlock(); + + if (action == Action.RIGHT_CLICK_BLOCK && block.getType() == Material.ENDER_CHEST) { + if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && OpenInv.getPlayerSilentChestStatus(player)) { event.setCancelled(true); player.openInventory(player.getEnderChest()); } } - if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Chest) { - boolean silentchest = false; - boolean anychest = false; - int x = event.getClickedBlock().getX(); - int y = event.getClickedBlock().getY(); - int z = event.getClickedBlock().getZ(); + if (action == Action.RIGHT_CLICK_BLOCK && block.getState() instanceof Chest) { + boolean silentChest = false; + boolean anyChest = false; - if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && OpenInv.getPlayerSilentChestStatus(player.getName())) { - silentchest = true; + int x = block.getX(); + int y = block.getY(); + int z = block.getZ(); + + if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && OpenInv.getPlayerSilentChestStatus(player)) { + silentChest = true; } - if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && OpenInv.getPlayerAnyChestStatus(player.getName())) { + if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && OpenInv.getPlayerAnyChestStatus(player)) { try { - anychest = OpenInv.anySilentChest.IsAnyChestNeeded(player, x, y, z); + anyChest = OpenInv.getAnySilentChest().isAnyChestNeeded(player, x, y, z); } catch (Exception e) { player.sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit."); @@ -107,16 +113,16 @@ public class OpenInvPlayerListener implements Listener { } // If the anychest or silentchest is active - if (anychest || silentchest) { - if (!OpenInv.anySilentChest.ActivateChest(player, anychest, silentchest, x, y, z)) { + if (anyChest || silentChest) { + if (!OpenInv.getAnySilentChest().activateChest(player, anyChest, silentChest, x, y, z)) { event.setCancelled(true); } } } - if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Sign) { + if (action == Action.RIGHT_CLICK_BLOCK && block.getState() instanceof Sign) { try { - Sign sign = ((Sign) event.getClickedBlock().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); @@ -128,12 +134,12 @@ public class OpenInvPlayerListener implements Listener { } } - if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if (!(player.getItemInHand().getType().getId() == OpenInv.getItemOpenInvItem()) || (!OpenInv.getPlayerItemOpenInvStatus(player.getName())) || !OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) { + if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) { + if (!(player.getItemInHand().getType() == OpenInv.getOpenInvItem()) || (!OpenInv.getPlayerItemOpenInvStatus(player)) || !OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) { return; } player.performCommand("openinv"); } } -} \ No newline at end of file +} diff --git a/src/main/java/com/lishid/openinv/utils/UUIDUtil.java b/src/main/java/com/lishid/openinv/utils/UUIDUtil.java index 76ebb38..22e3928 100644 --- a/src/main/java/com/lishid/openinv/utils/UUIDUtil.java +++ b/src/main/java/com/lishid/openinv/utils/UUIDUtil.java @@ -1,25 +1,58 @@ package com.lishid.openinv.utils; import java.util.Arrays; +import java.util.Collection; import java.util.Map; import java.util.UUID; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; public class UUIDUtil { + private static Player getPlayer(String name) { + Validate.notNull(name, "Name cannot be null"); + + Player found = null; + String lowerName = name.toLowerCase(); + int delta = Integer.MAX_VALUE; + + Collection players = Bukkit.getOnlinePlayers(); + 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; + } + } + + return found; + } + public static UUID getUUIDOf(String name) { UUID uuid = null; + Player player = getPlayer(name); - UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name)); - Map response; - - try { - response = fetcher.call(); - uuid = response.get(name); + if (player != null) { + // Player was found online + uuid = player.getUniqueId(); } - catch (Exception e) { - Bukkit.getServer().getLogger().warning("Exception while running UUIDFetcher"); - e.printStackTrace(); + else { + // Player was not found online. Fetch their UUID instead + UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name)); + Map response; + + try { + response = fetcher.call(); + uuid = response.get(name); + } + catch (Exception e) { + Bukkit.getServer().getLogger().warning("Exception while running UUIDFetcher"); + e.printStackTrace(); + } } return uuid; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..eb76f52 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,7 @@ +config-version: 2 +check-for-updates: true +notify: + any-chest: true + silent-chest: true +items: + open-inv: STICK \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 4cd5127..73a1bf2 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.0 +version: 2.3.1 author: lishid description: > This plugin allows you to open a player's inventory as a chest and interact with it in real time.