Changed command system for easier integration of "addons"

This commit is contained in:
Eric 2017-06-08 13:42:21 +02:00
parent 3e6207182a
commit 6aa0c0b07c
5 changed files with 461 additions and 233 deletions

View File

@ -3,6 +3,7 @@ package de.epiceric.shopchest;
import com.palmergames.bukkit.towny.Towny; import com.palmergames.bukkit.towny.Towny;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.wasteofplastic.askyblock.ASkyBlock; import com.wasteofplastic.askyblock.ASkyBlock;
import de.epiceric.shopchest.command.ShopCommand;
import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.config.HologramFormat; import de.epiceric.shopchest.config.HologramFormat;
import de.epiceric.shopchest.config.Placeholder; import de.epiceric.shopchest.config.Placeholder;

View File

@ -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<ShopSubCommand> 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<PluginCommand> 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<ShopSubCommand> 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<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
List<String> subCommandNames = new ArrayList<>();
List<String> 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<>();
}
}
}

View File

@ -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.config.Placeholder;
import de.epiceric.shopchest.event.ShopPreCreateEvent; import de.epiceric.shopchest.event.ShopPreCreateEvent;
import de.epiceric.shopchest.event.ShopPreInfoEvent; 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.language.LocalizedMessage;
import de.epiceric.shopchest.nms.JsonBuilder; import de.epiceric.shopchest.nms.JsonBuilder;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.utils.Callback; import de.epiceric.shopchest.utils.Callback;
import de.epiceric.shopchest.utils.ClickType; import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.ClickType.EnumClickType;
import de.epiceric.shopchest.utils.ItemUtils; import de.epiceric.shopchest.utils.ItemUtils;
import de.epiceric.shopchest.utils.Permissions; import de.epiceric.shopchest.utils.Permissions;
import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.UpdateChecker; import de.epiceric.shopchest.utils.UpdateChecker;
import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult;
import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
class ShopCommand implements CommandExecutor { class ShopCommandExecutor implements CommandExecutor {
private ShopChest plugin; private ShopChest plugin;
private String name;
private ShopUtils shopUtils; private ShopUtils shopUtils;
private PluginCommand pluginCommand;
ShopCommand(ShopChest plugin) { ShopCommandExecutor(ShopChest plugin) {
this.plugin = plugin; this.plugin = plugin;
this.name = plugin.getShopChestConfig().main_command_name;
this.shopUtils = plugin.getShopUtils(); this.shopUtils = plugin.getShopUtils();
this.pluginCommand = createPluginCommand();
register();
}
public PluginCommand getCommand() {
return pluginCommand;
}
private PluginCommand createPluginCommand() {
plugin.debug("Creating plugin command");
try {
Constructor<PluginCommand> 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 @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
boolean needsHelp = true; List<ShopSubCommand> subCommands = plugin.getShopCommand().getSubCommands();
if (args.length > 0) { if (args.length > 0) {
if (!(sender instanceof Player)) { String _subCommand = args[0];
switch (args[0].toUpperCase(Locale.US)) { ShopSubCommand subCommand = null;
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;
if (args[0].equalsIgnoreCase("create")) { for (ShopSubCommand shopSubCommand : subCommands) {
if (args.length == 4) { if (shopSubCommand.getName().equalsIgnoreCase(_subCommand)) {
needsHelp = false; subCommand = shopSubCommand;
create(args, ShopType.NORMAL, p); break;
} 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);
} }
} }
if (args[0].equalsIgnoreCase("reload")) { if (subCommand == null) {
needsHelp = false; return false;
}
if (subCommand.getName().equalsIgnoreCase("reload")) {
if (sender.hasPermission(Permissions.RELOAD)) { if (sender.hasPermission(Permissions.RELOAD)) {
reload(sender); reload(sender);
} else { } else {
sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_RELOAD)); sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_RELOAD));
} }
} else if (args[0].equalsIgnoreCase("update")) { } else if (subCommand.getName().equalsIgnoreCase("update")) {
needsHelp = false;
if (sender.hasPermission(Permissions.UPDATE)) { if (sender.hasPermission(Permissions.UPDATE)) {
checkUpdates(sender); checkUpdates(sender);
} else { } else {
sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_UPDATE)); 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 (sender.hasPermission(Permissions.CONFIG)) {
if (args.length >= 4) { return args.length >= 4 && changeConfig(sender, args);
needsHelp = false;
changeConfig(sender, args);
}
} else { } else {
needsHelp = false;
sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_CONFIG)); 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 (sender.hasPermission(Permissions.REMOVE_OTHER)) {
if (args.length >= 2) { if (args.length >= 2) {
needsHelp = false;
removeAll(sender, args); removeAll(sender, args);
} else {
return false;
} }
} else { } else {
needsHelp = false;
sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_REMOVE_OTHERS)); 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;
}
} }
} }
if (needsHelp) sendBasicHelpMessage(sender);
return true; return true;
} }
private void changeConfig(CommandSender sender, String[] args) { return false;
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);
}
} }
/** /**
@ -231,9 +145,9 @@ class ShopCommand implements CommandExecutor {
sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_CHECKING)); sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_CHECKING));
UpdateChecker uc = new UpdateChecker(ShopChest.getInstance()); 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.setLatestVersion(uc.getVersion());
plugin.setDownloadLink(uc.getLink()); plugin.setDownloadLink(uc.getLink());
plugin.setUpdateNeeded(true); 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()))); 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.setLatestVersion("");
plugin.setDownloadLink(""); plugin.setDownloadLink("");
plugin.setUpdateNeeded(false); plugin.setUpdateNeeded(false);
@ -282,16 +196,15 @@ class ShopCommand implements CommandExecutor {
} }
} }
}); });
} }
/** /**
* A given player creates a shop * A given player creates a shop
* @param args Arguments of the entered command * @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 * @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"); plugin.debug(p.getName() + " wants to create a shop");
int amount; int amount;
@ -318,7 +231,7 @@ class ShopCommand implements CommandExecutor {
int limit = shopUtils.getShopLimit(p); int limit = shopUtils.getShopLimit(p);
if (limit != -1) { if (limit != -1) {
if (shopUtils.getShopAmount(p) >= limit) { 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)))); 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"); plugin.debug(p.getName() + " has reached the limit");
return; 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 (creationPrice > 0) {
if (plugin.getEconomy().getBalance(p, p.getWorld().getName()) < creationPrice) { 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)))); 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); Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) { 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"); plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_CREATE)); 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"); plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_REMOVE)); 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 // Remove ClickType after 15 seconds if player has not clicked a chest
new BukkitRunnable() { new BukkitRunnable() {
@ -527,7 +440,7 @@ class ShopCommand implements CommandExecutor {
plugin.debug(p.getName() + " can now click a chest"); plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_INFO)); 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 // Remove ClickType after 15 seconds if player has not clicked a chest
new BukkitRunnable() { new BukkitRunnable() {
@ -554,7 +467,7 @@ class ShopCommand implements CommandExecutor {
plugin.debug(p.getName() + " can now click a chest"); plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_OPEN)); 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 // Remove ClickType after 15 seconds if player has not clicked a chest
new BukkitRunnable() { new BukkitRunnable() {
@ -565,6 +478,28 @@ class ShopCommand implements CommandExecutor {
}.runTaskLater(plugin, 300); }.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) { private void removeAll(CommandSender sender, String[] args) {
OfflinePlayer vendor = Bukkit.getOfflinePlayer(args[1]); 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.AMOUNT, String.valueOf(shops.size())),
new LocalizedMessage.ReplacedPlaceholder(Placeholder.VENDOR, vendor.getName()))); 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(" ");
}
} }

View File

@ -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);
}
/**
* <p>Returns a list of tab completions for the sub command</p>
* 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<String> 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);
}

View File

@ -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.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
@ -22,7 +22,6 @@ class ShopTabCompleter implements TabCompleter {
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) { public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase(plugin.getShopChestConfig().main_command_name)) { if (command.getName().equalsIgnoreCase(plugin.getShopChestConfig().main_command_name)) {
List<String> subCommands = Arrays.asList("config", "create", "info", "limits", "open", "reload", "remove", "update");
List<String> createSubCommands = Arrays.asList("admin", "normal"); List<String> createSubCommands = Arrays.asList("admin", "normal");
List<String> configSubCommands = Arrays.asList("add", "remove", "set"); List<String> configSubCommands = Arrays.asList("add", "remove", "set");
List<String> areaShopRemoveEvents = Arrays.asList("DELETE", "RESELL", "SELL", "UNRENT"); List<String> areaShopRemoveEvents = Arrays.asList("DELETE", "RESELL", "SELL", "UNRENT");
@ -32,19 +31,7 @@ class ShopTabCompleter implements TabCompleter {
ArrayList<String> returnCompletions = new ArrayList<>(); ArrayList<String> returnCompletions = new ArrayList<>();
if (args.length == 1) { if (args.length == 2) {
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[0].equals("config")) { if (args[0].equals("config")) {
if (!args[1].equals("")) { if (!args[1].equals("")) {
for (String s : configSubCommands) { for (String s : configSubCommands) {