From 6aa0c0b07c4f39050a1961ff727b769e1020710a Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 8 Jun 2017 13:42:21 +0200 Subject: [PATCH] Changed command system for easier integration of "addons" --- .../java/de/epiceric/shopchest/ShopChest.java | 1 + .../shopchest/command/ShopCommand.java | 295 ++++++++++++++++ .../ShopCommandExecutor.java} | 314 ++++++------------ .../shopchest/command/ShopSubCommand.java | 65 ++++ .../{ => command}/ShopTabCompleter.java | 19 +- 5 files changed, 461 insertions(+), 233 deletions(-) create mode 100644 src/main/java/de/epiceric/shopchest/command/ShopCommand.java rename src/main/java/de/epiceric/shopchest/{ShopCommand.java => command/ShopCommandExecutor.java} (67%) create mode 100644 src/main/java/de/epiceric/shopchest/command/ShopSubCommand.java rename src/main/java/de/epiceric/shopchest/{ => command}/ShopTabCompleter.java (86%) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index ce30e58..bce5fa7 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -3,6 +3,7 @@ package de.epiceric.shopchest; import com.palmergames.bukkit.towny.Towny; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.wasteofplastic.askyblock.ASkyBlock; +import de.epiceric.shopchest.command.ShopCommand; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.HologramFormat; import de.epiceric.shopchest.config.Placeholder; diff --git a/src/main/java/de/epiceric/shopchest/command/ShopCommand.java b/src/main/java/de/epiceric/shopchest/command/ShopCommand.java new file mode 100644 index 0000000..5cc0459 --- /dev/null +++ b/src/main/java/de/epiceric/shopchest/command/ShopCommand.java @@ -0,0 +1,295 @@ +package de.epiceric.shopchest.command; + +import de.epiceric.shopchest.ShopChest; +import de.epiceric.shopchest.config.Placeholder; +import de.epiceric.shopchest.language.LanguageUtils; +import de.epiceric.shopchest.language.LocalizedMessage; +import de.epiceric.shopchest.utils.Permissions; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandMap; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; +import org.bukkit.permissions.PermissionAttachmentInfo; +import org.bukkit.plugin.Plugin; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; + +public class ShopCommand { + + private static boolean commandCreated = false; + + private ShopChest plugin; + private String name; + private PluginCommand pluginCommand; + + private List subCommands = new ArrayList<>(); + + public ShopCommand(final ShopChest plugin) { + if (commandCreated) { + IllegalStateException e = new IllegalStateException("Command has already been registered"); + plugin.debug(e); + throw e; + } + + this.plugin = plugin; + this.name = plugin.getShopChestConfig().main_command_name; + this.pluginCommand = createPluginCommand(); + + ShopCommandExecutor executor = new ShopCommandExecutor(plugin); + ShopTabCompleter tabCompleter = new ShopTabCompleter(plugin); + + final LocalizedMessage.ReplacedPlaceholder cmdPlaceholder = new LocalizedMessage.ReplacedPlaceholder( + Placeholder.COMMAND, plugin.getShopChestConfig().main_command_name); + + addSubCommand(new ShopSubCommand("create", true, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + boolean receiveCreateMessage = sender.hasPermission(Permissions.CREATE); + if (!receiveCreateMessage) { + for (PermissionAttachmentInfo permInfo : sender.getEffectivePermissions()) { + String perm = permInfo.getPermission(); + if (perm.startsWith(Permissions.CREATE) && sender.hasPermission(perm)) { + receiveCreateMessage = true; + break; + } + } + } + + if (sender.hasPermission(Permissions.CREATE_ADMIN)) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE_ADMIN, cmdPlaceholder); + } else if (receiveCreateMessage) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE, cmdPlaceholder); + } + + return ""; + } + }); + + addSubCommand(new ShopSubCommand("remove", true, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVE, cmdPlaceholder); + } + }); + + addSubCommand(new ShopSubCommand("info", true, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_INFO, cmdPlaceholder); + } + }); + + addSubCommand(new ShopSubCommand("limits", true, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_LIMITS, cmdPlaceholder); + } + }); + + addSubCommand(new ShopSubCommand("open", true, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_OPEN, cmdPlaceholder); + } + }); + + addSubCommand(new ShopSubCommand("removeall", false, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + if (sender.hasPermission(Permissions.REMOVE_OTHER)) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVEALL, cmdPlaceholder); + } else { + return ""; + } + } + }); + + addSubCommand(new ShopSubCommand("reload", false, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + if (sender.hasPermission(Permissions.RELOAD)) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_RELOAD, cmdPlaceholder); + } else { + return ""; + } + } + }); + + addSubCommand(new ShopSubCommand("update", false, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + if (sender.hasPermission(Permissions.UPDATE)) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_UPDATE, cmdPlaceholder); + } else { + return ""; + } + } + }); + + addSubCommand(new ShopSubCommand("config", false, executor, tabCompleter) { + @Override + public String getHelpMessage(CommandSender sender) { + if (sender.hasPermission(Permissions.CONFIG)) { + return LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CONFIG, cmdPlaceholder); + } else { + return ""; + } + } + }); + + register(); + commandCreated = true; + } + + public PluginCommand getCommand() { + return pluginCommand; + } + + private PluginCommand createPluginCommand() { + plugin.debug("Creating plugin command"); + try { + Constructor c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); + c.setAccessible(true); + + PluginCommand cmd = c.newInstance(name, plugin); + cmd.setDescription("Manage players' shops or this plugin."); + cmd.setUsage("/" + name); + cmd.setExecutor(new ShopBaseCommandExecutor()); + cmd.setTabCompleter(new ShopBaseTabCompleter()); + + return cmd; + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { + plugin.getLogger().severe("Failed to create command"); + plugin.debug("Failed to create plugin command"); + plugin.debug(e); + } + + return null; + } + + public void addSubCommand(ShopSubCommand subCommand) { + plugin.debug("Adding sub command \"" + subCommand.getName() + "\""); + this.subCommands.add(subCommand); + } + + public List getSubCommands() { + return new ArrayList<>(subCommands); + } + + private void register() { + if (pluginCommand == null) return; + + plugin.debug("Registering command " + name); + + try { + Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); + f.setAccessible(true); + + Object commandMapObject = f.get(Bukkit.getPluginManager()); + if (commandMapObject instanceof CommandMap) { + CommandMap commandMap = (CommandMap) commandMapObject; + commandMap.register(plugin.getName(), pluginCommand); + } + } catch (NoSuchFieldException | IllegalAccessException e) { + plugin.getLogger().severe("Failed to register command"); + plugin.debug("Failed to register plugin command"); + plugin.debug(e); + } + } + + /** + * Sends the basic help message + * @param sender {@link CommandSender} who will receive the message + */ + private void sendBasicHelpMessage(CommandSender sender) { + plugin.debug("Sending basic help message to " + sender.getName()); + + sender.sendMessage(" "); + + for (ShopSubCommand subCommand : subCommands) { + String msg = subCommand.getHelpMessage(sender); + if (msg == null || msg.isEmpty()) { + continue; + } + + sender.sendMessage(msg); + } + + sender.sendMessage(" "); + } + + private class ShopBaseCommandExecutor implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length > 0) { + for (ShopSubCommand subCommand : subCommands) { + if (subCommand.getName().equalsIgnoreCase(args[0])) { + if (!(sender instanceof Player) && subCommand.isPlayerCommand()) { + sender.sendMessage(ChatColor.RED + "Only players can use this command."); + return true; + } + + if (!subCommand.execute(sender, command, label, args)) { + sendBasicHelpMessage(sender); + } + + return true; + } + } + + sendBasicHelpMessage(sender); + } else { + sendBasicHelpMessage(sender); + } + + return true; + } + } + + private class ShopBaseTabCompleter implements TabCompleter { + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + List subCommandNames = new ArrayList<>(); + + List tabCompletions = new ArrayList<>(); + + for (ShopSubCommand subCommand : subCommands) { + subCommandNames.add(subCommand.getName()); + } + + if (args.length == 1) { + if (!args[0].isEmpty()) { + for (String s : subCommandNames) { + if (s.startsWith(args[0])) { + tabCompletions.add(s); + } + } + return tabCompletions; + } else { + return subCommandNames; + } + } else if (args.length > 1) { + for (ShopSubCommand subCmd : subCommands) { + if (subCmd.getName().equalsIgnoreCase(args[0])) { + return subCmd.getTabCompletions(sender, command, label, args); + } + } + } + + return new ArrayList<>(); + } + + } + +} diff --git a/src/main/java/de/epiceric/shopchest/ShopCommand.java b/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java similarity index 67% rename from src/main/java/de/epiceric/shopchest/ShopCommand.java rename to src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java index a8df220..21e7c67 100644 --- a/src/main/java/de/epiceric/shopchest/ShopCommand.java +++ b/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java @@ -1,5 +1,6 @@ -package de.epiceric.shopchest; +package de.epiceric.shopchest.command; +import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Placeholder; import de.epiceric.shopchest.event.ShopPreCreateEvent; import de.epiceric.shopchest.event.ShopPreInfoEvent; @@ -11,214 +12,127 @@ import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.nms.JsonBuilder; import de.epiceric.shopchest.shop.Shop; -import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.utils.Callback; import de.epiceric.shopchest.utils.ClickType; -import de.epiceric.shopchest.utils.ClickType.EnumClickType; import de.epiceric.shopchest.utils.ItemUtils; import de.epiceric.shopchest.utils.Permissions; import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.UpdateChecker; -import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult; import de.epiceric.shopchest.utils.Utils; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; -import org.bukkit.command.PluginCommand; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.permissions.PermissionAttachmentInfo; -import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; -import java.util.Locale; -class ShopCommand implements CommandExecutor { +class ShopCommandExecutor implements CommandExecutor { private ShopChest plugin; - private String name; private ShopUtils shopUtils; - private PluginCommand pluginCommand; - ShopCommand(ShopChest plugin) { + ShopCommandExecutor(ShopChest plugin) { this.plugin = plugin; - this.name = plugin.getShopChestConfig().main_command_name; this.shopUtils = plugin.getShopUtils(); - this.pluginCommand = createPluginCommand(); - - register(); - } - - public PluginCommand getCommand() { - return pluginCommand; - } - - private PluginCommand createPluginCommand() { - plugin.debug("Creating plugin command"); - try { - Constructor c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); - c.setAccessible(true); - - PluginCommand cmd = c.newInstance(name, plugin); - cmd.setDescription("Manage players' shops or this plugin."); - cmd.setUsage("/" + name); - cmd.setExecutor(this); - cmd.setTabCompleter(new ShopTabCompleter(plugin)); - - return cmd; - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { - plugin.getLogger().severe("Failed to create command"); - plugin.debug("Failed to create plugin command"); - plugin.debug(e); - } - - return null; - } - - private void register() { - if (pluginCommand == null) return; - - plugin.debug("Registering command " + name); - - try { - Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); - f.setAccessible(true); - - Object commandMapObject = f.get(Bukkit.getPluginManager()); - if (commandMapObject instanceof CommandMap) { - CommandMap commandMap = (CommandMap) commandMapObject; - commandMap.register(plugin.getName(), pluginCommand); - } - } catch (NoSuchFieldException | IllegalAccessException e) { - plugin.getLogger().severe("Failed to register command"); - plugin.debug("Failed to register plugin command"); - plugin.debug(e); - } } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - boolean needsHelp = true; + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + List subCommands = plugin.getShopCommand().getSubCommands(); if (args.length > 0) { - if (!(sender instanceof Player)) { - switch (args[0].toUpperCase(Locale.US)) { - case "CREATE": - case "REMOVE": - case "INFO": - case "LIMITS": - case "OPEN": - sender.sendMessage(ChatColor.RED + "Only players can use this command."); - return true; - } - } else { - Player p = (Player) sender; + String _subCommand = args[0]; + ShopSubCommand subCommand = null; - if (args[0].equalsIgnoreCase("create")) { - if (args.length == 4) { - needsHelp = false; - create(args, ShopType.NORMAL, p); - } else if (args.length == 5) { - if (args[4].equalsIgnoreCase("normal")) { - needsHelp = false; - create(args, ShopType.NORMAL, p); - } else if (args[4].equalsIgnoreCase("admin")) { - needsHelp = false; - if (p.hasPermission(Permissions.CREATE_ADMIN)) { - create(args, ShopType.ADMIN, p); - } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_CREATE_ADMIN)); - } - } - } - } else if (args[0].equalsIgnoreCase("remove")) { - needsHelp = false; - remove(p); - } else if (args[0].equalsIgnoreCase("info")) { - needsHelp = false; - info(p); - } else if (args[0].equalsIgnoreCase("limits")) { - needsHelp = false; - plugin.debug(p.getName() + " is viewing his shop limits: " + shopUtils.getShopAmount(p) + "/" + shopUtils.getShopLimit(p)); - int limit = shopUtils.getShopLimit(p); - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OCCUPIED_SHOP_SLOTS, - new LocalizedMessage.ReplacedPlaceholder(Placeholder.LIMIT, (limit < 0 ? "∞" : String.valueOf(limit))), - new LocalizedMessage.ReplacedPlaceholder(Placeholder.AMOUNT, String.valueOf(shopUtils.getShopAmount(p))))); - } else if (args[0].equalsIgnoreCase("open")) { - needsHelp = false; - open(p); + for (ShopSubCommand shopSubCommand : subCommands) { + if (shopSubCommand.getName().equalsIgnoreCase(_subCommand)) { + subCommand = shopSubCommand; + break; } } - if (args[0].equalsIgnoreCase("reload")) { - needsHelp = false; + if (subCommand == null) { + return false; + } + + if (subCommand.getName().equalsIgnoreCase("reload")) { if (sender.hasPermission(Permissions.RELOAD)) { reload(sender); } else { sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_RELOAD)); } - } else if (args[0].equalsIgnoreCase("update")) { - needsHelp = false; + } else if (subCommand.getName().equalsIgnoreCase("update")) { if (sender.hasPermission(Permissions.UPDATE)) { checkUpdates(sender); } else { sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_UPDATE)); } - } else if (args[0].equalsIgnoreCase("config")) { + } else if (subCommand.getName().equalsIgnoreCase("config")) { if (sender.hasPermission(Permissions.CONFIG)) { - if (args.length >= 4) { - needsHelp = false; - changeConfig(sender, args); - } + return args.length >= 4 && changeConfig(sender, args); } else { - needsHelp = false; sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_CONFIG)); } - } else if (args[0].equalsIgnoreCase("removeall")) { + } else if (subCommand.getName().equalsIgnoreCase("removeall")) { if (sender.hasPermission(Permissions.REMOVE_OTHER)) { if (args.length >= 2) { - needsHelp = false; removeAll(sender, args); + } else { + return false; } } else { - needsHelp = false; sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_REMOVE_OTHERS)); } + } else { + if (sender instanceof Player) { + Player p = (Player) sender; + + if (subCommand.getName().equalsIgnoreCase("create")) { + if (args.length == 4) { + create(args, Shop.ShopType.NORMAL, p); + } else if (args.length == 5) { + if (args[4].equalsIgnoreCase("normal")) { + create(args, Shop.ShopType.NORMAL, p); + } else if (args[4].equalsIgnoreCase("admin")) { + if (p.hasPermission(Permissions.CREATE_ADMIN)) { + create(args, Shop.ShopType.ADMIN, p); + } else { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_CREATE_ADMIN)); + } + } else { + return false; + } + } else { + return false; + } + } else if (subCommand.getName().equalsIgnoreCase("remove")) { + remove(p); + } else if (subCommand.getName().equalsIgnoreCase("info")) { + info(p); + } else if (subCommand.getName().equalsIgnoreCase("limits")) { + plugin.debug(p.getName() + " is viewing his shop limits: " + shopUtils.getShopAmount(p) + "/" + shopUtils.getShopLimit(p)); + int limit = shopUtils.getShopLimit(p); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OCCUPIED_SHOP_SLOTS, + new LocalizedMessage.ReplacedPlaceholder(Placeholder.LIMIT, (limit < 0 ? "∞" : String.valueOf(limit))), + new LocalizedMessage.ReplacedPlaceholder(Placeholder.AMOUNT, String.valueOf(shopUtils.getShopAmount(p))))); + } else if (subCommand.getName().equalsIgnoreCase("open")) { + open(p); + } else { + return false; + } + } } + + return true; } - if (needsHelp) sendBasicHelpMessage(sender); - return true; - } - - private void changeConfig(CommandSender sender, String[] args) { - plugin.debug(sender.getName() + " is changing the configuration"); - - String property = args[2]; - String value = args[3]; - - if (args[1].equalsIgnoreCase("set")) { - plugin.getShopChestConfig().set(property, value); - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHANGED_CONFIG_SET, new LocalizedMessage.ReplacedPlaceholder(Placeholder.PROPERTY, property), new LocalizedMessage.ReplacedPlaceholder(Placeholder.VALUE, value))); - } else if (args[1].equalsIgnoreCase("add")) { - plugin.getShopChestConfig().add(property, value); - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHANGED_CONFIG_ADDED, new LocalizedMessage.ReplacedPlaceholder(Placeholder.PROPERTY, property), new LocalizedMessage.ReplacedPlaceholder(Placeholder.VALUE, value))); - } else if (args[1].equalsIgnoreCase("remove")) { - plugin.getShopChestConfig().remove(property, value); - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHANGED_CONFIG_REMOVED, new LocalizedMessage.ReplacedPlaceholder(Placeholder.PROPERTY, property), new LocalizedMessage.ReplacedPlaceholder(Placeholder.VALUE, value))); - } else { - sendBasicHelpMessage(sender); - } + return false; } /** @@ -231,9 +145,9 @@ class ShopCommand implements CommandExecutor { sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_CHECKING)); UpdateChecker uc = new UpdateChecker(ShopChest.getInstance()); - UpdateCheckerResult result = uc.check(); + UpdateChecker.UpdateCheckerResult result = uc.check(); - if (result == UpdateCheckerResult.TRUE) { + if (result == UpdateChecker.UpdateCheckerResult.TRUE) { plugin.setLatestVersion(uc.getVersion()); plugin.setDownloadLink(uc.getLink()); plugin.setUpdateNeeded(true); @@ -245,7 +159,7 @@ class ShopCommand implements CommandExecutor { sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_AVAILABLE, new LocalizedMessage.ReplacedPlaceholder(Placeholder.VERSION, uc.getVersion()))); } - } else if (result == UpdateCheckerResult.FALSE) { + } else if (result == UpdateChecker.UpdateCheckerResult.FALSE) { plugin.setLatestVersion(""); plugin.setDownloadLink(""); plugin.setUpdateNeeded(false); @@ -282,16 +196,15 @@ class ShopCommand implements CommandExecutor { } } }); - } /** * A given player creates a shop * @param args Arguments of the entered command - * @param shopType The {@link ShopType}, the shop will have + * @param shopType The {@link Shop.ShopType}, the shop will have * @param p The command executor */ - private void create(String[] args, ShopType shopType, final Player p) { + private void create(String[] args, Shop.ShopType shopType, final Player p) { plugin.debug(p.getName() + " wants to create a shop"); int amount; @@ -318,7 +231,7 @@ class ShopCommand implements CommandExecutor { int limit = shopUtils.getShopLimit(p); if (limit != -1) { if (shopUtils.getShopAmount(p) >= limit) { - if (shopType != ShopType.ADMIN || !plugin.getShopChestConfig().exclude_admin_shops) { + if (shopType != Shop.ShopType.ADMIN || !plugin.getShopChestConfig().exclude_admin_shops) { p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_LIMIT_REACHED, new LocalizedMessage.ReplacedPlaceholder(Placeholder.LIMIT, String.valueOf(limit)))); plugin.debug(p.getName() + " has reached the limit"); return; @@ -455,7 +368,7 @@ class ShopCommand implements CommandExecutor { } } - double creationPrice = (shopType == ShopType.NORMAL) ? plugin.getShopChestConfig().shop_creation_price_normal : plugin.getShopChestConfig().shop_creation_price_admin; + double creationPrice = (shopType == Shop.ShopType.NORMAL) ? plugin.getShopChestConfig().shop_creation_price_normal : plugin.getShopChestConfig().shop_creation_price_admin; if (creationPrice > 0) { if (plugin.getEconomy().getBalance(p, p.getWorld().getName()) < creationPrice) { p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_CREATE_NOT_ENOUGH_MONEY, new LocalizedMessage.ReplacedPlaceholder(Placeholder.CREATION_PRICE, String.valueOf(creationPrice)))); @@ -468,7 +381,7 @@ class ShopCommand implements CommandExecutor { Bukkit.getPluginManager().callEvent(event); if (!event.isCancelled()) { - ClickType.setPlayerClickType(p, new ClickType(EnumClickType.CREATE, product, buyPrice, sellPrice, shopType)); + ClickType.setPlayerClickType(p, new ClickType(ClickType.EnumClickType.CREATE, product, buyPrice, sellPrice, shopType)); plugin.debug(p.getName() + " can now click a chest"); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_CREATE)); @@ -500,7 +413,7 @@ class ShopCommand implements CommandExecutor { plugin.debug(p.getName() + " can now click a chest"); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_REMOVE)); - ClickType.setPlayerClickType(p, new ClickType(EnumClickType.REMOVE)); + ClickType.setPlayerClickType(p, new ClickType(ClickType.EnumClickType.REMOVE)); // Remove ClickType after 15 seconds if player has not clicked a chest new BukkitRunnable() { @@ -527,7 +440,7 @@ class ShopCommand implements CommandExecutor { plugin.debug(p.getName() + " can now click a chest"); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_INFO)); - ClickType.setPlayerClickType(p, new ClickType(EnumClickType.INFO)); + ClickType.setPlayerClickType(p, new ClickType(ClickType.EnumClickType.INFO)); // Remove ClickType after 15 seconds if player has not clicked a chest new BukkitRunnable() { @@ -554,7 +467,7 @@ class ShopCommand implements CommandExecutor { plugin.debug(p.getName() + " can now click a chest"); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_OPEN)); - ClickType.setPlayerClickType(p, new ClickType(EnumClickType.OPEN)); + ClickType.setPlayerClickType(p, new ClickType(ClickType.EnumClickType.OPEN)); // Remove ClickType after 15 seconds if player has not clicked a chest new BukkitRunnable() { @@ -565,6 +478,28 @@ class ShopCommand implements CommandExecutor { }.runTaskLater(plugin, 300); } + private boolean changeConfig(CommandSender sender, String[] args) { + plugin.debug(sender.getName() + " is changing the configuration"); + + String property = args[2]; + String value = args[3]; + + if (args[1].equalsIgnoreCase("set")) { + plugin.getShopChestConfig().set(property, value); + sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHANGED_CONFIG_SET, new LocalizedMessage.ReplacedPlaceholder(Placeholder.PROPERTY, property), new LocalizedMessage.ReplacedPlaceholder(Placeholder.VALUE, value))); + } else if (args[1].equalsIgnoreCase("add")) { + plugin.getShopChestConfig().add(property, value); + sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHANGED_CONFIG_ADDED, new LocalizedMessage.ReplacedPlaceholder(Placeholder.PROPERTY, property), new LocalizedMessage.ReplacedPlaceholder(Placeholder.VALUE, value))); + } else if (args[1].equalsIgnoreCase("remove")) { + plugin.getShopChestConfig().remove(property, value); + sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHANGED_CONFIG_REMOVED, new LocalizedMessage.ReplacedPlaceholder(Placeholder.PROPERTY, property), new LocalizedMessage.ReplacedPlaceholder(Placeholder.VALUE, value))); + } else { + return false; + } + + return true; + } + private void removeAll(CommandSender sender, String[] args) { OfflinePlayer vendor = Bukkit.getOfflinePlayer(args[1]); @@ -593,59 +528,4 @@ class ShopCommand implements CommandExecutor { new LocalizedMessage.ReplacedPlaceholder(Placeholder.AMOUNT, String.valueOf(shops.size())), new LocalizedMessage.ReplacedPlaceholder(Placeholder.VENDOR, vendor.getName()))); } - - /** - * Sends the basic help message - * @param sender {@link CommandSender} who will receive the message - */ - private void sendBasicHelpMessage(CommandSender sender) { - plugin.debug("Sending basic help message to " + sender.getName()); - - boolean receiveCreateMessage = sender.hasPermission(Permissions.CREATE); - if (!receiveCreateMessage) { - for (PermissionAttachmentInfo permInfo : sender.getEffectivePermissions()) { - String perm = permInfo.getPermission(); - if (perm.startsWith(Permissions.CREATE) && sender.hasPermission(perm)) { - receiveCreateMessage = true; - break; - } - } - } - - sender.sendMessage(" "); - - LocalizedMessage.ReplacedPlaceholder cmdPlaceholder = new LocalizedMessage.ReplacedPlaceholder(Placeholder.COMMAND, plugin.getShopChestConfig().main_command_name); - - if (sender instanceof Player) { - if (sender.hasPermission(Permissions.CREATE_ADMIN)) { - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE_ADMIN, cmdPlaceholder)); - } else if (receiveCreateMessage) { - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE, cmdPlaceholder)); - } - - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVE, cmdPlaceholder)); - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_INFO, cmdPlaceholder)); - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_LIMITS, cmdPlaceholder)); - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_OPEN, cmdPlaceholder)); - } - - if (sender.hasPermission(Permissions.REMOVE_OTHER)) { - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVEALL, cmdPlaceholder)); - } - - if (sender.hasPermission(Permissions.RELOAD)) { - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_RELOAD, cmdPlaceholder)); - } - - if (sender.hasPermission(Permissions.UPDATE)) { - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_UPDATE, cmdPlaceholder)); - } - - if (sender.hasPermission(Permissions.CONFIG)) { - sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CONFIG, cmdPlaceholder)); - } - - sender.sendMessage(" "); - } - } diff --git a/src/main/java/de/epiceric/shopchest/command/ShopSubCommand.java b/src/main/java/de/epiceric/shopchest/command/ShopSubCommand.java new file mode 100644 index 0000000..d9da08d --- /dev/null +++ b/src/main/java/de/epiceric/shopchest/command/ShopSubCommand.java @@ -0,0 +1,65 @@ +package de.epiceric.shopchest.command; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +import java.util.ArrayList; +import java.util.List; + +public abstract class ShopSubCommand { + private String name; + private boolean playerCommand; + private CommandExecutor executor; + private TabCompleter tabCompleter; + + public ShopSubCommand(String name, boolean playerCommand, CommandExecutor executor, TabCompleter tabCompleter) { + this.name = name; + this.playerCommand = playerCommand; + this.executor = executor; + this.tabCompleter = tabCompleter; + } + + public String getName() { + return name; + } + + /** + * Returns whether the command can only be used by players, not by the console + */ + public boolean isPlayerCommand() { + return playerCommand; + } + + /** + * Execute the sub command + * @param sender Sender of the command + * @param args Arguments of the command ({@code args[0]} is the sub command's name) + * @return Whether the sender should be sent the help message + */ + public boolean execute(CommandSender sender, Command command, String label, String[] args) { + return executor.onCommand(sender, command, label, args); + } + + /** + *

Returns a list of tab completions for the sub command

+ * The main sub command will be tab completed by ShopChest + * @param sender Sender of the command + * @param args Arguments of the command ({@code args[0]} is the sub command's name) + * @return A list of tab completions (may be an empty list) + */ + public List getTabCompletions(CommandSender sender, Command command, String label, String[] args) { + if (tabCompleter == null) { + return new ArrayList<>(); + } + + return tabCompleter.onTabComplete(sender, command, label, args); + } + + /** + * Returns the help message for the command. + * @param sender Sender to receive the help message + */ + public abstract String getHelpMessage(CommandSender sender); +} diff --git a/src/main/java/de/epiceric/shopchest/ShopTabCompleter.java b/src/main/java/de/epiceric/shopchest/command/ShopTabCompleter.java similarity index 86% rename from src/main/java/de/epiceric/shopchest/ShopTabCompleter.java rename to src/main/java/de/epiceric/shopchest/command/ShopTabCompleter.java index b2594ff..4d0fbdb 100644 --- a/src/main/java/de/epiceric/shopchest/ShopTabCompleter.java +++ b/src/main/java/de/epiceric/shopchest/command/ShopTabCompleter.java @@ -1,6 +1,6 @@ -package de.epiceric.shopchest; - +package de.epiceric.shopchest.command; +import de.epiceric.shopchest.ShopChest; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -22,7 +22,6 @@ class ShopTabCompleter implements TabCompleter { public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase(plugin.getShopChestConfig().main_command_name)) { - List subCommands = Arrays.asList("config", "create", "info", "limits", "open", "reload", "remove", "update"); List createSubCommands = Arrays.asList("admin", "normal"); List configSubCommands = Arrays.asList("add", "remove", "set"); List areaShopRemoveEvents = Arrays.asList("DELETE", "RESELL", "SELL", "UNRENT"); @@ -32,19 +31,7 @@ class ShopTabCompleter implements TabCompleter { ArrayList returnCompletions = new ArrayList<>(); - if (args.length == 1) { - if (!args[0].equals("")) { - for (String s : subCommands) { - if (s.startsWith(args[0])) { - returnCompletions.add(s); - } - } - - return returnCompletions; - } else { - return subCommands; - } - } else if (args.length == 2) { + if (args.length == 2) { if (args[0].equals("config")) { if (!args[1].equals("")) { for (String s : configSubCommands) {