From 16faa1ddb262916caff166b67d7aa71597ba32eb Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 18 Feb 2022 19:39:20 +0100 Subject: [PATCH] Rewrites a bunch of code to improve the structure as specified in #5, #4, #2, #1 --- .../paidsigns/command/AddCommand.java | 8 +- .../paidsigns/command/RemoveCommand.java | 14 +- .../paidsigns/command/RemoveTabCompleter.java | 2 +- .../paidsigns/container/PaidSign.java | 145 ++++++++---------- .../container/PaidSignCondition.java | 96 ++++++++++++ .../paidsigns/listener/SignListener.java | 59 ++++--- .../paidsigns/manager/PaidSignManager.java | 116 +++++++------- .../paidsigns/property/OptionState.java | 9 +- 8 files changed, 254 insertions(+), 195 deletions(-) create mode 100644 src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java index a5fe838..f367e74 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java @@ -31,15 +31,14 @@ public class AddCommand implements CommandExecutor { List arguments = Tokenizer.tokenize(String.join(" ", args)); String id = arguments.get(0); - short line; double cost; try { - line = (short) (Short.parseShort(arguments.get(1)) - 1); cost = Double.parseDouble(arguments.get(2)); } catch (NumberFormatException exception) { sender.sendMessage("You provided an invalid number"); return false; } + String permission = arguments.get(2); OptionState ignoreCase = OptionState.DEFAULT; OptionState ignoreColor = OptionState.DEFAULT; if (arguments.size() > 3) { @@ -50,13 +49,14 @@ public class AddCommand implements CommandExecutor { } try { - PaidSign sign = new PaidSign(id, line, cost, ignoreCase, ignoreColor); + PaidSign sign = new PaidSign(id, cost, permission, ignoreCase, ignoreColor); + /* TODO: Rewrite the method for comparing if paid signs are duplicated for (PaidSign similarSign : manager.getPaidSigns(sign.getCleanId(), sign.getLineIndex())) { if (sign.matches(similarSign)) { sender.sendMessage("A paid sign with the same id and line already exists"); return false; } - } + }*/ manager.addPaidSign(sign); sender.sendMessage("Successfully added new paid sign"); return true; diff --git a/src/main/java/net/knarcraft/paidsigns/command/RemoveCommand.java b/src/main/java/net/knarcraft/paidsigns/command/RemoveCommand.java index f77c007..2e3eef9 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/RemoveCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/RemoveCommand.java @@ -2,7 +2,6 @@ package net.knarcraft.paidsigns.command; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.utility.Tokenizer; -import org.apache.commons.lang.ArrayUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -26,17 +25,10 @@ public class RemoveCommand implements CommandExecutor { return false; } List arguments = Tokenizer.tokenize(String.join(" ", args)); - String[] input = arguments.get(0).split("\\|"); - short line; + String name = arguments.get(0); + try { - line = (short) (Short.parseShort(input[0]) - 1); - } catch (NumberFormatException exception) { - sender.sendMessage("Invalid line number given"); - return false; - } - String id = String.join("|", (String[]) ArrayUtils.remove(input, 0)); - try { - if (PaidSigns.getInstance().getSignManager().removePaidSign(id, line)) { + if (PaidSigns.getInstance().getSignManager().removePaidSign(name)) { sender.sendMessage("Successfully removed paid sign"); } else { sender.sendMessage("No matching paid sign was found"); diff --git a/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCompleter.java index aaa48c0..187e866 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCompleter.java @@ -23,7 +23,7 @@ public class RemoveTabCompleter implements TabCompleter { List allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); List signIds = new ArrayList<>(); for (PaidSign sign : allPaidSigns) { - signIds.add("\"" + (sign.getLineIndex() + 1) + "|" + sign.getId() + "\""); + signIds.add("\"" + sign.getName() + "\""); } return signIds; } diff --git a/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java b/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java index 3e9e6ff..e2c74a1 100644 --- a/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java +++ b/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java @@ -2,58 +2,55 @@ package net.knarcraft.paidsigns.container; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.property.OptionState; -import net.knarcraft.paidsigns.utility.ColorHelper; -import net.md_5.bungee.api.ChatColor; + +import java.util.HashMap; +import java.util.Map; /** * A representation of a paid sign */ public class PaidSign { - private final String id; - private final String cleanId; - private final short lineIndex; + private final String name; private final double cost; + private final String permission; private final OptionState ignoreCase; private final OptionState ignoreColor; + private final Map conditions = new HashMap<>(); /** * Instantiates a new paid sign * - * @param id

The string that identifies this type of paid sign

- * @param lineIndex

The line the id has to be on to trigger payment

+ * @param name

A recognizable, unique, name used to identify this paid sign

* @param cost

The cost of creating this paid sign

+ * @param permission

The permission required to create the sign this paid sign matches

* @param ignoreCase

Whether to ignore case when looking for this permission sign

* @param ignoreColor

Whether to ignore color when looking for this permission sign

*/ - public PaidSign(String id, short lineIndex, double cost, OptionState ignoreCase, OptionState ignoreColor) { - if (id == null || id.trim().isBlank()) { + public PaidSign(String name, double cost, String permission, OptionState ignoreCase, OptionState ignoreColor) { + if (name == null || name.trim().isBlank()) { throw new IllegalArgumentException("Id cannot be empty"); } if (cost <= 0) { throw new IllegalArgumentException("Cost must be larger than 0"); } - if (lineIndex < 0 || lineIndex > 3) { - throw new IllegalArgumentException("Sign line must be between 0 and 3"); - } if (ignoreCase == null || ignoreColor == null) { throw new IllegalArgumentException("Ignore case and ignore color options cannot be null"); } - this.id = id; - this.lineIndex = lineIndex; + this.name = name; this.cost = cost; + this.permission = permission; this.ignoreCase = ignoreCase; this.ignoreColor = ignoreColor; - this.cleanId = getCleanString(id); } /** - * Gets the id string of this paid sign + * Gets the name identifying this paid sign * - * @return

The id string of this paid sign

+ * @return

The name identifying this paid sign

*/ - public String getId() { - return id; + public String getName() { + return name; } /** @@ -66,66 +63,21 @@ public class PaidSign { } /** - * Gets the line on the sign the id must be on to trigger payment + * Gets the permission required by a player to create the sign this paid sign matches * - * @return

The sign line to search for the id

+ * @return

The permission required by a player to create the sign this paid sign matches

*/ - public short getLineIndex() { - return lineIndex; + public String getPermission() { + return this.permission; } /** - * Gets the clean id of this paid sign + * Gets all conditions registered for this paid sign * - * @return

The clean id of this paid sign

+ * @return

All conditions registered for this paid sign

*/ - public String getCleanId() { - return cleanId; - } - - /** - * Gets the "clean" version of the given string - * - *

The "cleaning" removes color codes and lower-cases the string.

- * - * @param string

The string to clean

- * @return

The cleaned string

- */ - public static String getCleanString(String string) { - return ChatColor.stripColor(ColorHelper.translateAllColorCodes(string.toLowerCase())); - } - - /** - * Checks whether this paid sign matches the given line - * - * @param lineNumber

The line number of the given line

- * @param line

The line to compare against this paid sign's id

- * @return

True if the line matches this sign

- */ - public boolean matches(short lineNumber, String line) { - if (lineNumber != this.lineIndex) { - return false; - } - String idCopy = id; - if (getIgnoreCase()) { - idCopy = idCopy.toLowerCase(); - line = line.toLowerCase(); - } - if (getIgnoreColor()) { - idCopy = ColorHelper.stripColorCodes(idCopy); - line = ColorHelper.stripColorCodes(line); - } - return idCopy.equals(line); - } - - /** - * Checks whether this paid sign matches another paid sign - * - * @param paidSign

The other paid sign to compare to

- * @return

True if the signs match

- */ - public boolean matches(PaidSign paidSign) { - return matches(paidSign.lineIndex, paidSign.id); + public Map getConditions() { + return new HashMap<>(this.conditions); } /** @@ -134,11 +86,7 @@ public class PaidSign { * @return

Whether the text case should be ignored for this paid sign

*/ public boolean getIgnoreCase() { - if (this.ignoreCase == OptionState.DEFAULT) { - return PaidSigns.getInstance().ignoreCase(); - } else { - return OptionState.getBooleanValue(this.ignoreCase); - } + return OptionState.getBooleanValue(this.ignoreCase, PaidSigns.getInstance().ignoreCase()); } /** @@ -147,11 +95,46 @@ public class PaidSign { * @return

Whether the text color should be ignored for this paid sign

*/ public boolean getIgnoreColor() { - if (this.ignoreColor == OptionState.DEFAULT) { - return PaidSigns.getInstance().ignoreColor(); - } else { - return OptionState.getBooleanValue(this.ignoreColor); + return OptionState.getBooleanValue(this.ignoreColor, PaidSigns.getInstance().ignoreColor()); + } + + /** + * Checks whether this paid sign matches the given set of sign lines + * + * @param lines

The sign lines to test against

+ * @return

True if this paid sign matches the given lines

+ */ + public boolean matches(String[] lines) { + //Make sure a paid sign without a condition never matches anything + if (this.conditions.isEmpty()) { + return false; } + boolean success = true; + for (short i = 0; i < 4; i++) { + PaidSignCondition condition = this.conditions.get(i); + if (condition != null) { + success = success && condition.test(lines[i]); + } + } + return success; + } + + /** + * Adds a condition to this paid sign + * + * @param line

The line on the sign the matched text must be on

+ * @param stringToMatch

The string that should be matched for the new condition to be true

+ * @param executeRegex

Whether to execute the string as RegEx

+ * @param ignoreCase

Whether to ignore case when matching against the condition

+ * @param ignoreColor

Whether to ignore color when matching against the condition

+ */ + public void addCondition(short line, String stringToMatch, boolean executeRegex, OptionState ignoreCase, OptionState ignoreColor) { + if (line < 0 || line > 3) { + throw new IllegalArgumentException("Invalid sign line given for new paid sign condition"); + } + boolean ignoreCaseBoolean = OptionState.getBooleanValue(ignoreCase, this.getIgnoreCase()); + boolean ignoreColorBoolean = OptionState.getBooleanValue(ignoreColor, this.getIgnoreColor()); + this.conditions.put(line, new PaidSignCondition(stringToMatch, executeRegex, ignoreCaseBoolean, ignoreColorBoolean)); } } diff --git a/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java b/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java new file mode 100644 index 0000000..7cf7df5 --- /dev/null +++ b/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java @@ -0,0 +1,96 @@ +package net.knarcraft.paidsigns.container; + +import net.knarcraft.paidsigns.utility.ColorHelper; + +/** + * A condition for deciding if a paid sign matches a sign line + */ +public class PaidSignCondition { + + final String stringToMatch; + final boolean executeRegex; + final boolean ignoreCase; + final boolean ignoreColor; + + /** + * Instantiates a new paid sign condition + * + * @param stringToMatch

The string/regular expression the line has to match to fulfill this condition

+ * @param executeRegex

Whether to execute the match string as a regular expression

+ * @param ignoreCase

Whether to ignore uppercase/lowercase when comparing against this condition

+ * @param ignoreColor

Whether to ignore color codes when comparing against this condition

+ */ + public PaidSignCondition(String stringToMatch, boolean executeRegex, boolean ignoreCase, boolean ignoreColor) { + this.stringToMatch = stringToMatch; + this.executeRegex = executeRegex; + this.ignoreCase = ignoreCase; + this.ignoreColor = ignoreColor; + } + + /** + * Gets the string this condition should match + * + * @return

The string this condition should match

+ */ + public String getStringToMatch() { + return this.stringToMatch; + } + + /** + * Gets whether to execute the match string as RegEx + * + * @return

Whether to execute the match string as RegEx

+ */ + public boolean executeRegex() { + return this.executeRegex; + } + + /** + * Gets whether to ignore case when trying to match strings + * + * @return

Whether to ignore case when trying to match strings

+ */ + public boolean ignoreCase() { + return this.ignoreCase; + } + + /** + * Gets whether to ignore color when trying to match strings + * + * @return

Whether to ignore color when trying to match strings

+ */ + public boolean ignoreColor() { + return this.ignoreColor; + } + + /** + * Tests whether the given line matches this condition + * + * @param line

The sign line to test

+ * @return

True if this condition matches the given line

+ */ + public boolean test(String line) { + String stringToMatch = this.stringToMatch; + //Strip color codes if they shouldn't matter + if (this.ignoreColor) { + stringToMatch = ColorHelper.stripColorCodes(stringToMatch); + line = ColorHelper.stripColorCodes(line); + } + if (this.executeRegex) { + //Match using RegEx + if (this.ignoreCase) { + return line.matches("(?i)" + stringToMatch); + } else { + return line.matches(stringToMatch); + } + } else { + //Match regularly + if (this.ignoreCase) { + return stringToMatch.equalsIgnoreCase(line); + } else { + return stringToMatch.equals(line); + } + } + } + +} diff --git a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java index ddec22c..6724f7d 100644 --- a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java @@ -3,7 +3,6 @@ package net.knarcraft.paidsigns.listener; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.manager.EconomyManager; -import net.knarcraft.paidsigns.manager.PaidSignManager; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -17,51 +16,51 @@ import java.util.List; */ public class SignListener implements Listener { - @EventHandler(priority = EventPriority.MONITOR) + @EventHandler(priority = EventPriority.LOW) + @SuppressWarnings("unused") public void onSignChange(SignChangeEvent event) { if (event.isCancelled() || event.getPlayer().hasPermission("paidsigns.paymentexempt")) { return; } String[] lines = event.getLines(); - PaidSignManager signManager = PaidSigns.getInstance().getSignManager(); - for (short lineIndex = 0; lineIndex < lines.length; lineIndex++) { - //Get all "weak" matches (any paid sign with a clean id matching the clean line) - List matchingSigns = signManager.getPaidSigns(PaidSign.getCleanString(lines[lineIndex]), lineIndex); - if (matchingSigns.isEmpty()) { - continue; - } - if (testMatchingSigns(lineIndex, lines[lineIndex], matchingSigns, event)) { + List matchingSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); + for (PaidSign paidSign : matchingSigns) { + //If a match is found, just return + if (matchSign(paidSign, lines, event)) { return; } } } /** - * Tests all weak matches of paid signs to check if a strong match is found + * Checks if the given paid sign matches the given sign lines * - * @param lineIndex

The index of the currently managed line

- * @param line

The text on the currently managed line

- * @param matchingSigns

The signs that weakly match the

- * @param event

The triggered sign change event

- * @return

True if a match was found and thw work is finished

+ * @param paidSign

The paid sign to test against the sign lines

+ * @param lines

The lines of a sign

+ * @param event

The triggered sign change event to cancel if necessary

+ * @return

True if a match was found and actions have been taken

*/ - private boolean testMatchingSigns(short lineIndex, String line, List matchingSigns, SignChangeEvent event) { - for (PaidSign paidSign : matchingSigns) { - if (paidSign.matches(lineIndex, line)) { - Player player = event.getPlayer(); - double cost = paidSign.getCost(); - boolean canAfford = EconomyManager.canAfford(player, cost); - if (!canAfford) { - player.sendMessage("[PaidSigns] You cannot afford to create this sign"); - event.setCancelled(true); - } else { - String unit = EconomyManager.getCurrency(cost != 1); - player.sendMessage(String.format("[PaidSigns] You paid %.2f %s to create the sign", cost, unit)); - EconomyManager.withdraw(player, cost); - } + private boolean matchSign(PaidSign paidSign, String[] lines, SignChangeEvent event) { + if (paidSign.matches(lines)) { + Player player = event.getPlayer(); + String permission = paidSign.getPermission(); + //If a match is found, but the player is missing the permission, assume no plugin sign was created + if (permission != null && !permission.trim().isEmpty() && !player.hasPermission(permission)) { return true; } + + double cost = paidSign.getCost(); + boolean canAfford = EconomyManager.canAfford(player, cost); + if (!canAfford) { + player.sendMessage("[PaidSigns] You cannot afford to create this sign"); + event.setCancelled(true); + } else { + String unit = EconomyManager.getCurrency(cost != 1); + player.sendMessage(String.format("[PaidSigns] You paid %.2f %s to create the sign", cost, unit)); + EconomyManager.withdraw(player, cost); + } + return true; } return false; } diff --git a/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java b/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java index 4fbbe70..fe3cfe8 100644 --- a/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java +++ b/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java @@ -2,6 +2,7 @@ package net.knarcraft.paidsigns.manager; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; +import net.knarcraft.paidsigns.container.PaidSignCondition; import net.knarcraft.paidsigns.property.OptionState; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -10,7 +11,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.function.Predicate; +import java.util.Map; import java.util.logging.Level; /** @@ -20,7 +21,6 @@ public final class PaidSignManager { private final List paidSigns; private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml"); - private static final String signLineIdSeparator = "-,_,-"; /** * Instantiate a new paid sign manager @@ -45,29 +45,18 @@ public final class PaidSignManager { /** * Removes a paid sign from this paid sign manager * - * @param id

The identifier for the paid sign to remove

- * @param line

The line the identifier has to match to be valid

+ * @param name

The name of the paid sign to remove

* @return

True if a sign was removed

* @throws IOException

If unable to write to the signs file

*/ - public boolean removePaidSign(String id, short line) throws IOException { - boolean removed = this.paidSigns.removeIf((sign) -> sign.getId().equals(id) && sign.getLineIndex() == line); + public boolean removePaidSign(String name) throws IOException { + boolean removed = this.paidSigns.removeIf((sign) -> sign.getName().equals(name)); if (!removed) { return false; + } else { + saveSigns(this.paidSigns); + return true; } - saveSigns(this.paidSigns); - return true; - } - - /** - * Gets the paid signs that match the given properties - * - * @param cleanId

The clean id to search for

- * @param line

The line number to search for

- * @return

The paid signs that matched the given properties

- */ - public List getPaidSigns(String cleanId, short line) { - return filterPaidSigns(filterPaidSigns(paidSigns, line), cleanId); } /** @@ -79,17 +68,6 @@ public final class PaidSignManager { return new ArrayList<>(paidSigns); } - /** - * Filters a list of paid signs to match the given line number - * - * @param paidSigns

The list of paid signs to start with

- * @param line

The line number to filter by

- * @return

The filtered list of paid signs

- */ - private static List filterPaidSigns(List paidSigns, short line) { - return filterPaidSigns(paidSigns, (paidSign) -> paidSign.getLineIndex() == line); - } - /** * Loads paid signs from the signs file * @@ -104,18 +82,40 @@ public final class PaidSignManager { } List paidSigns = new ArrayList<>(); - for (String combinedId : signSection.getKeys(false)) { - String[] idParts = combinedId.split(signLineIdSeparator); - short lineNumber = Short.parseShort(idParts[0]); - String id = idParts[1]; - double cost = signSection.getDouble(combinedId + ".cost"); - OptionState ignoreCase = OptionState.getFromBoolean(signSection.getBoolean(combinedId + ".ignoreCase")); - OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(combinedId + ".ignoreColor")); - paidSigns.add(new PaidSign(id, lineNumber, cost, ignoreCase, ignoreColor)); + for (String name : signSection.getKeys(false)) { + double cost = signSection.getDouble(name + ".cost"); + String permission = signSection.getString(name + ".permission"); + OptionState ignoreCase = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreCase")); + OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreColor")); + PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor); + loadConditions(signSection, sign); + paidSigns.add(sign); } return paidSigns; } + /** + * Loads any saved paid sign conditions and applies them to the given sign + * + * @param signSection

The configuration section containing sign information

+ * @param sign

The sign to load conditions for

+ */ + private static void loadConditions(ConfigurationSection signSection, PaidSign sign) { + ConfigurationSection conditionSection = signSection.getConfigurationSection(sign.getName() + ".conditions"); + if (conditionSection != null) { + for (String lineIndex : conditionSection.getKeys(false)) { + short lineNumber = Short.parseShort(lineIndex); + String stringToMatch = conditionSection.getString(lineIndex + ".stringToMatch"); + boolean executeRegEx = conditionSection.getBoolean(lineIndex + ".executeRegEx"); + boolean ignoreConditionCase = conditionSection.getBoolean(lineIndex + ".ignoreCase"); + boolean ignoreConditionColor = conditionSection.getBoolean(lineIndex + ".ignoreColor"); + sign.addCondition(lineNumber, stringToMatch, executeRegEx, + OptionState.getFromBoolean(ignoreConditionCase), + OptionState.getFromBoolean(ignoreConditionColor)); + } + } + } + /** * Saves the given paid signs to the signs file * @@ -127,34 +127,22 @@ public final class PaidSignManager { ConfigurationSection signSection = configuration.createSection("paidSigns"); for (PaidSign sign : signs) { - String signId = sign.getLineIndex() + signLineIdSeparator + sign.getId(); - signSection.set(signId + ".cost", sign.getCost()); - signSection.set(signId + ".ignoreCase", sign.getIgnoreCase()); - signSection.set(signId + ".ignoreColor", sign.getIgnoreColor()); + String name = sign.getName(); + signSection.set(name + ".cost", sign.getCost()); + signSection.set(name + ".permission", sign.getPermission()); + signSection.set(name + ".ignoreCase", sign.getIgnoreCase()); + signSection.set(name + ".ignoreColor", sign.getIgnoreColor()); + ConfigurationSection conditionsSection = signSection.createSection(name + ".conditions"); + Map signConditions = sign.getConditions(); + for (short lineIndex : signConditions.keySet()) { + PaidSignCondition condition = signConditions.get(lineIndex); + conditionsSection.set(lineIndex + ".stringToMatch", condition.getStringToMatch()); + conditionsSection.set(lineIndex + ".executeRegEx", condition.executeRegex()); + conditionsSection.set(lineIndex + ".ignoreCase", condition.ignoreCase()); + conditionsSection.set(lineIndex + ".ignoreColor", condition.ignoreColor()); + } } configuration.save(signsFile); } - /** - * Filters a list of paid signs to match the given clean id - * - * @param paidSigns

The list of paid signs to start with

- * @param cleanId

The clean id to filter by

- * @return

The filtered list of paid signs

- */ - private static List filterPaidSigns(List paidSigns, String cleanId) { - return filterPaidSigns(paidSigns, (paidSign) -> paidSign.getCleanId().equals(cleanId)); - } - - /** - * Filters a list of paid signs using the given predicate - * - * @param paidSigns

The list of paid signs to start with

- * @param predicate

The predicate used to filter paid signs

- * @return

The filtered list of paid signs

- */ - private static List filterPaidSigns(List paidSigns, Predicate predicate) { - return new ArrayList<>(paidSigns).stream().filter(predicate).toList(); - } - } diff --git a/src/main/java/net/knarcraft/paidsigns/property/OptionState.java b/src/main/java/net/knarcraft/paidsigns/property/OptionState.java index e39f5aa..b0e8f61 100644 --- a/src/main/java/net/knarcraft/paidsigns/property/OptionState.java +++ b/src/main/java/net/knarcraft/paidsigns/property/OptionState.java @@ -23,14 +23,15 @@ public enum OptionState { /** * Gets the boolean value of the given option state if it's boolean compatible * - * @param optionState

The option state to get the boolean from

- * @return

The boolean value, or an illegal argument exception if called on DEFAULT

+ * @param optionState

The option state to get the boolean from

+ * @param defaultValue

The default value to use if the option state is DEFAULT

+ * @return

The boolean value

*/ - public static boolean getBooleanValue(OptionState optionState) { + public static boolean getBooleanValue(OptionState optionState, boolean defaultValue) { return switch (optionState) { case TRUE -> true; case FALSE -> false; - case DEFAULT -> throw new IllegalArgumentException("No boolean value available for DEFAULT"); + case DEFAULT -> defaultValue; }; }