diff --git a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java index 807933c..270a851 100644 --- a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java +++ b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java @@ -1,6 +1,8 @@ package net.knarcraft.paidsigns; import net.knarcraft.paidsigns.command.AddCommand; +import net.knarcraft.paidsigns.command.AddConditionCommand; +import net.knarcraft.paidsigns.command.AddConditionTabCompleter; import net.knarcraft.paidsigns.command.AddTabCompleter; import net.knarcraft.paidsigns.command.ReloadTabCommand; import net.knarcraft.paidsigns.command.RemoveTabCommand; @@ -105,6 +107,12 @@ public final class PaidSigns extends JavaPlugin { addCommand.setTabCompleter(new AddTabCompleter()); } + PluginCommand addConditionCommand = this.getCommand("addPaidSignCondition"); + if (addConditionCommand != null) { + addConditionCommand.setExecutor(new AddConditionCommand()); + addConditionCommand.setTabCompleter(new AddConditionTabCompleter()); + } + PluginCommand removeCommand = this.getCommand("removePaidSign"); if (removeCommand != null) { TabExecutor removeTabExecutor = new RemoveTabCommand(); diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java index a2cbf82..3a6c5ef 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java @@ -29,6 +29,7 @@ public class AddCommand implements CommandExecutor { } PaidSignManager manager = PaidSigns.getInstance().getSignManager(); List arguments = Tokenizer.tokenize(String.join(" ", args)); + int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; String signName = arguments.get(0); double cost; @@ -41,20 +42,18 @@ public class AddCommand implements CommandExecutor { String permission = arguments.get(2); OptionState ignoreCase = OptionState.DEFAULT; OptionState ignoreColor = OptionState.DEFAULT; - if (arguments.size() > 3) { + if (argumentSize > 3) { ignoreCase = OptionState.fromString(arguments.get(3)); } - if (arguments.size() > 4) { + if (argumentSize > 4) { ignoreColor = OptionState.fromString(arguments.get(4)); } try { PaidSign sign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor); - for (PaidSign otherSign : manager.getAllPaidSigns()) { - if (sign.getName().equals(otherSign.getName())) { - sender.sendMessage("A paid sign with the same name already exists"); - return false; - } + if (manager.getPaidSign(signName) != null) { + sender.sendMessage("A paid sign with the same name already exists"); + return false; } manager.addPaidSign(sign); sender.sendMessage("Successfully added new paid sign"); diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java index 8c2345d..745123e 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java @@ -1,16 +1,56 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.paidsigns.PaidSigns; +import net.knarcraft.paidsigns.container.PaidSign; +import net.knarcraft.paidsigns.property.OptionState; +import net.knarcraft.paidsigns.utility.Tokenizer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import java.util.List; + +/** + * A representation of the command for adding a new match condition for a sign + */ public class AddConditionCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + List arguments = Tokenizer.tokenize(String.join(" ", args)); + int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; + if (argumentSize < 3) { + return false; + } - + String name = arguments.get(0); + short lineNumber; + try { + lineNumber = (short) (Short.parseShort(arguments.get(1)) - 1); + } catch (NumberFormatException exception) { + sender.sendMessage("Invalid line number given"); + return false; + } + String stringToMatch = arguments.get(2); + boolean executeRegEx = false; + if (argumentSize > 3) { + executeRegEx = Boolean.parseBoolean(arguments.get(3)); + } + OptionState ignoreCase = OptionState.DEFAULT; + if (argumentSize > 4) { + ignoreCase = OptionState.getFromBoolean(Boolean.parseBoolean(arguments.get(4))); + } + OptionState ignoreColor = OptionState.DEFAULT; + if (argumentSize > 5) { + ignoreColor = OptionState.getFromBoolean(Boolean.parseBoolean(arguments.get(5))); + } + PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(name); + if (sign == null) { + sender.sendMessage("No such paid sign exists"); + return false; + } + sign.addCondition(lineNumber, stringToMatch, executeRegEx, ignoreCase, ignoreColor); return false; } diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java new file mode 100644 index 0000000..3e917f5 --- /dev/null +++ b/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java @@ -0,0 +1,57 @@ +package net.knarcraft.paidsigns.command; + +import net.knarcraft.paidsigns.utility.TabCompleteHelper; +import net.knarcraft.paidsigns.utility.Tokenizer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +/** + * The tab completer for the add paid sign condition command + */ +public class AddConditionTabCompleter implements TabCompleter { + + private List lineIndices; + private List stringsToMatch; + private List booleans; + private List optionStates; + + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, + @NotNull String[] args) { + if (lineIndices == null) { + initializeValues(); + } + + List arguments = Tokenizer.tokenize(String.join(" ", args)); + int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; + if (argumentSize < 1) { + return TabCompleteHelper.getPaidSignNames(); + } else if (argumentSize < 2) { + return this.lineIndices; + } else if (argumentSize < 3) { + return stringsToMatch; + } else if (argumentSize < 4) { + return booleans; + } else if (argumentSize < 6) { + return optionStates; + } + return null; + } + + /** + * Initializes the values available for tab completion + */ + private void initializeValues() { + lineIndices = TabCompleteHelper.getSignLines(); + stringsToMatch = TabCompleteHelper.getExampleConditionStrings(); + booleans = TabCompleteHelper.getBooleans(); + optionStates = TabCompleteHelper.getOptionStates(); + } + +} diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java index fa2dea4..7bbc7ce 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java @@ -1,5 +1,6 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.paidsigns.utility.TabCompleteHelper; import net.knarcraft.paidsigns.utility.Tokenizer; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -128,21 +129,9 @@ public class AddTabCompleter implements TabCompleter { * Initializes the values available for tab completion */ private static void initializeValues() { - names = new ArrayList<>(); - names.add("sign1"); - names.add("\"lift up sign\""); - names.add("\"lift down sign\""); - - costs = new ArrayList<>(); - costs.add("1"); - costs.add("5"); - costs.add("10"); - costs.add("15"); - - options = new ArrayList<>(); - options.add("default"); - options.add("true"); - options.add("false"); + names = TabCompleteHelper.getExamplePaidSignNames(); + costs = TabCompleteHelper.getCosts(); + options = TabCompleteHelper.getOptionStates(); } } diff --git a/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCommand.java b/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCommand.java index 6f2cc85..130f2c0 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCommand.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -55,12 +56,12 @@ public class RemoveTabCommand implements TabExecutor { int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; if (argumentSize < 1) { - List allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); - List signIds = new ArrayList<>(); - for (PaidSign sign : allPaidSigns) { - signIds.add("\"" + sign.getName() + "\""); + Map allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); + List signNames = new ArrayList<>(); + for (String name : allPaidSigns.keySet()) { + signNames.add("\"" + name + "\""); } - return signIds; + return signNames; } else { return new ArrayList<>(); } diff --git a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java index 6724f7d..ceed610 100644 --- a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java @@ -9,7 +9,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.SignChangeEvent; -import java.util.List; +import java.util.Map; /** * A listener for listening to registered paid signs @@ -24,8 +24,8 @@ public class SignListener implements Listener { } String[] lines = event.getLines(); - List matchingSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); - for (PaidSign paidSign : matchingSigns) { + Map matchingSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); + for (PaidSign paidSign : matchingSigns.values()) { //If a match is found, just return if (matchSign(paidSign, lines, event)) { return; diff --git a/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java b/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java index fe3cfe8..b475329 100644 --- a/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java +++ b/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java @@ -9,8 +9,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; import java.util.Map; import java.util.logging.Level; @@ -19,7 +18,7 @@ import java.util.logging.Level; */ public final class PaidSignManager { - private final List paidSigns; + private final Map paidSigns; private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml"); /** @@ -27,7 +26,7 @@ public final class PaidSignManager { * * @param paidSigns

The paid signs this manager should manage

*/ - public PaidSignManager(List paidSigns) { + public PaidSignManager(Map paidSigns) { this.paidSigns = paidSigns; } @@ -38,7 +37,7 @@ public final class PaidSignManager { * @throws IOException

If unable to write to the signs file

*/ public void addPaidSign(PaidSign paidSign) throws IOException { - this.paidSigns.add(paidSign); + this.paidSigns.put(paidSign.getName(), paidSign); saveSigns(this.paidSigns); } @@ -50,7 +49,7 @@ public final class PaidSignManager { * @throws IOException

If unable to write to the signs file

*/ public boolean removePaidSign(String name) throws IOException { - boolean removed = this.paidSigns.removeIf((sign) -> sign.getName().equals(name)); + boolean removed = this.paidSigns.remove(name) != null; if (!removed) { return false; } else { @@ -59,13 +58,23 @@ public final class PaidSignManager { } } + /** + * Gets the paid sign with the given name + * + * @param name

The paid sign with the given name

+ * @return

The paid sign with the given name, or null if it does not exist

+ */ + public PaidSign getPaidSign(String name) { + return paidSigns.get(name); + } + /** * Gets a copy of all registered paid signs * * @return

All registered paid signs

*/ - public List getAllPaidSigns() { - return new ArrayList<>(paidSigns); + public Map getAllPaidSigns() { + return new HashMap<>(paidSigns); } /** @@ -73,15 +82,15 @@ public final class PaidSignManager { * * @return

The loaded paid signs

*/ - public static List loadSigns() { + public static Map loadSigns() { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile); ConfigurationSection signSection = configuration.getConfigurationSection("paidSigns"); if (signSection == null) { PaidSigns.getInstance().getLogger().log(Level.WARNING, "Signs section not found in data.yml"); - return new ArrayList<>(); + return new HashMap<>(); } - List paidSigns = new ArrayList<>(); + Map paidSigns = new HashMap<>(); for (String name : signSection.getKeys(false)) { double cost = signSection.getDouble(name + ".cost"); String permission = signSection.getString(name + ".permission"); @@ -89,7 +98,7 @@ public final class PaidSignManager { OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreColor")); PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor); loadConditions(signSection, sign); - paidSigns.add(sign); + paidSigns.put(name, sign); } return paidSigns; } @@ -122,12 +131,12 @@ public final class PaidSignManager { * @param signs

The signs to save

* @throws IOException

If unable to write to the signs file

*/ - public static void saveSigns(List signs) throws IOException { + public static void saveSigns(Map signs) throws IOException { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile); ConfigurationSection signSection = configuration.createSection("paidSigns"); - for (PaidSign sign : signs) { - String name = sign.getName(); + for (PaidSign sign : signs.values()) { + String name = signSection.getName(); signSection.set(name + ".cost", sign.getCost()); signSection.set(name + ".permission", sign.getPermission()); signSection.set(name + ".ignoreCase", sign.getIgnoreCase()); diff --git a/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java b/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java new file mode 100644 index 0000000..a898ca7 --- /dev/null +++ b/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java @@ -0,0 +1,98 @@ +package net.knarcraft.paidsigns.utility; + +import net.knarcraft.paidsigns.PaidSigns; + +import java.util.ArrayList; +import java.util.List; + +/** + * A helper class for providing common tab complete options + */ +public class TabCompleteHelper { + + /** + * Gets the available boolean values for tab completion + * + * @return

The available boolean values

+ */ + public static List getBooleans() { + List booleans = new ArrayList<>(); + booleans.add("true"); + booleans.add("false"); + return booleans; + } + + /** + * Gets the available option states for tab completion + * + * @return

The available option states

+ */ + public static List getOptionStates() { + List optionStates = getBooleans(); + optionStates.add("default"); + return optionStates; + } + + /** + * Gets the names of all registered paid signs + * + * @return

The names of all registered paid signs

+ */ + public static List getPaidSignNames() { + return new ArrayList<>(PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet()); + } + + /** + * Gets all the choices for lines on a sign + * + * @return

All the line indices of a sign

+ */ + public static List getSignLines() { + List lines = new ArrayList<>(); + for (int i = 1; i < 5; i++) { + lines.add(String.valueOf(i)); + } + return lines; + } + + /** + * Gets some valid costs as example values + * + * @return

Some valid costs

+ */ + public static List getCosts() { + List costs = new ArrayList<>(); + costs.add("1"); + costs.add("5"); + costs.add("10"); + costs.add("15"); + return costs; + } + + /** + * Gets some example paid sign names for auto-completion + * + * @return

Some example paid sign names

+ */ + public static List getExamplePaidSignNames() { + List names = new ArrayList<>(); + names.add("sign1"); + names.add("\"lift up sign\""); + names.add("\"lift down sign\""); + return names; + } + + /** + * Gets some example condition matching strings for tab completion + * + * @return

Some example "stringToMatch" values

+ */ + public static List getExampleConditionStrings() { + List conditionStrings = new ArrayList<>(); + conditionStrings.add("[Gate]"); + conditionStrings.add("\"[Lift Up]\""); + conditionStrings.add("\"[Lift Down]\""); + return conditionStrings; + } + +}