diff --git a/src/main/java/net/knarcraft/paidsigns/command/EditCommand.java b/src/main/java/net/knarcraft/paidsigns/command/EditCommand.java index c5a8d67..ca7005e 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/EditCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/EditCommand.java @@ -130,7 +130,7 @@ public class EditCommand extends TokenizedCommand { PaidSign updatedSign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor, matchAnyCondition); for (short line : conditions.keySet()) { PaidSignCondition condition = conditions.get(line); - updatedSign.addCondition(line, condition.getStringToMatch(), condition.executeRegex(), + updatedSign.addCondition(line, condition.stringToMatch(), condition.executeRegex(), OptionState.getFromBoolean(condition.ignoreCase()), OptionState.getFromBoolean(condition.ignoreColor())); } @@ -154,7 +154,7 @@ public class EditCommand extends TokenizedCommand { PaidSignConditionProperty property, String newValue) { PaidSignCondition condition = sign.getConditions().get(conditionIndex); String stringToMatch = property == PaidSignConditionProperty.STRING_TO_MATCH ? newValue : - condition.getStringToMatch(); + condition.stringToMatch(); boolean executeRegEx = property == PaidSignConditionProperty.EXECUTE_REG_EX ? Boolean.parseBoolean(newValue) : condition.executeRegex(); boolean ignoreCase = property == PaidSignConditionProperty.IGNORE_CASE ? OptionState.getBooleanValue( diff --git a/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java b/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java index 15d3361..56717f6 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java @@ -107,7 +107,7 @@ public class ListCommand extends TokenizedCommand { PaidSignCondition condition) { sender.sendMessage(StringFormatter.replacePlaceholders(Translator.getTranslatedMessage( TranslatableMessage.PAID_SIGN_CONDITION_INFO), new String[]{"{name}", "{line}", "{match}", "{regex}", - "{case}", "{color}"}, new String[]{signName, String.valueOf(signLine + 1), condition.getStringToMatch(), + "{case}", "{color}"}, new String[]{signName, String.valueOf(signLine + 1), condition.stringToMatch(), translateBoolean(condition.executeRegex()), translateBoolean(condition.ignoreCase()), translateBoolean(condition.ignoreColor())})); } @@ -124,7 +124,7 @@ public class ListCommand extends TokenizedCommand { for (short lineIndex : signConditions.keySet()) { String format = Translator.getTranslatedMessage(TranslatableMessage.PAID_SIGN_INFO_CONDITION_FORMAT); conditions.append(StringFormatter.replacePlaceholders(format, new String[]{"{line}", "{condition}"}, - new String[]{String.valueOf((lineIndex + 1)), signConditions.get(lineIndex).getStringToMatch()})); + new String[]{String.valueOf((lineIndex + 1)), signConditions.get(lineIndex).stringToMatch()})); } sender.sendMessage(replacePlaceholders(Translator.getTranslatedMessage( diff --git a/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java b/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java index 18a3223..e16e4d8 100644 --- a/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java +++ b/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java @@ -148,13 +148,15 @@ public class PaidSign { * @param ignoreCase
Whether to ignore case when matching against the condition
* @param ignoreColorWhether to ignore color when matching against the condition
*/ - public void addCondition(short line, String stringToMatch, boolean executeRegex, OptionState ignoreCase, OptionState ignoreColor) { + 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)); + 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 index 7cf7df5..9ac4b7b 100644 --- a/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java +++ b/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java @@ -4,64 +4,14 @@ import net.knarcraft.paidsigns.utility.ColorHelper; /** * A condition for deciding if a paid sign matches a sign line + * + * @param stringToMatchThe string/regular expression the line has to match to fulfill this condition
+ * @param executeRegexWhether to execute the match string as a regular expression
+ * @param ignoreCaseWhether to ignore uppercase/lowercase when comparing against this condition
+ * @param ignoreColorWhether to ignore color codes when comparing against this condition
*/ -public class PaidSignCondition { - - final String stringToMatch; - final boolean executeRegex; - final boolean ignoreCase; - final boolean ignoreColor; - - /** - * Instantiates a new paid sign condition - * - * @param stringToMatchThe string/regular expression the line has to match to fulfill this condition
- * @param executeRegexWhether to execute the match string as a regular expression
- * @param ignoreCaseWhether to ignore uppercase/lowercase when comparing against this condition
- * @param ignoreColorWhether 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 - * - * @returnThe string this condition should match
- */ - public String getStringToMatch() { - return this.stringToMatch; - } - - /** - * Gets whether to execute the match string as RegEx - * - * @returnWhether to execute the match string as RegEx
- */ - public boolean executeRegex() { - return this.executeRegex; - } - - /** - * Gets whether to ignore case when trying to match strings - * - * @returnWhether to ignore case when trying to match strings
- */ - public boolean ignoreCase() { - return this.ignoreCase; - } - - /** - * Gets whether to ignore color when trying to match strings - * - * @returnWhether to ignore color when trying to match strings
- */ - public boolean ignoreColor() { - return this.ignoreColor; - } +public record PaidSignCondition(String stringToMatch, boolean executeRegex, boolean ignoreCase, + boolean ignoreColor) { /** * Tests whether the given line matches this condition diff --git a/src/main/java/net/knarcraft/paidsigns/container/PaidSignConditionMatch.java b/src/main/java/net/knarcraft/paidsigns/container/PaidSignConditionMatch.java new file mode 100644 index 0000000..8a28c0d --- /dev/null +++ b/src/main/java/net/knarcraft/paidsigns/container/PaidSignConditionMatch.java @@ -0,0 +1,19 @@ +package net.knarcraft.paidsigns.container; + +import org.jetbrains.annotations.NotNull; + +/** + * A container class for number of condition matches for a paid sign + * + * @param paidSignThe paid sign this class keeps track of matches for
+ * @param conditionMatchesThe number of conditions matched for the paid sign
+ */ +public record PaidSignConditionMatch(PaidSign paidSign, + int conditionMatches) implements ComparableThe unique id of the player that created the sign
+ * @param costThe cost the player paid for creating the sign
*/ -public class TrackedSign { - - private final UUID playerId; - private final double cost; - - /** - * Instantiates a new tracked sign - * - * @param playerIdThe unique id of the player that created the sign
- * @param costThe cost the player paid for creating the sign
- */ - public TrackedSign(UUID playerId, double cost) { - this.playerId = playerId; - this.cost = cost; - } - - /** - * Gets the id of the player that created this tracked sign - * - * @returnThe player that created this tracked sign
- */ - public UUID getPlayerId() { - return this.playerId; - } - - /** - * Gets the cost the player paid for creating this paid sign - * - * @returnThe cost paid for creating this sign
- */ - public double getCost() { - return this.cost; - } +public record TrackedSign(UUID playerId, double cost) { } diff --git a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java index d469966..a3db905 100644 --- a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java @@ -2,6 +2,7 @@ package net.knarcraft.paidsigns.listener; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; +import net.knarcraft.paidsigns.container.PaidSignConditionMatch; import net.knarcraft.paidsigns.formatting.StringFormatter; import net.knarcraft.paidsigns.formatting.TranslatableMessage; import net.knarcraft.paidsigns.manager.EconomyManager; @@ -13,6 +14,9 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.SignChangeEvent; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Map; /** @@ -28,36 +32,90 @@ public class SignListener implements Listener { } String[] lines = event.getLines(); - MapThis method chooses the most specific match, if any. If two matches have the same number of matched + * conditions, it will choose the most expensive one.
+ * + * @param allConditionMatchesA sorted list of all paid sign condition matches
+ * @returnThe best matching paid sign, or null if no paid sign matches
+ */ + private PaidSign getMatchingSign(ListThis calculates the number of matches, allowing the most specific paid sign to be chosen
+ * + * @param paidSignThe paid sign to calculate matches for
+ * @param linesThe lines of a sign
+ * @param allConditionMatchesThe number of conditions matched
+ */ + private void calculateConditionMatches(PaidSign paidSign, String[] lines, ListThe paid sign to test against the sign lines
- * @param linesThe lines of a sign
* @param eventThe triggered sign change event to cancel if necessary
- * @returnTrue if a match was found and actions have been taken
*/ - 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; - } - - performPaidSignTransaction(paidSign, player, event); - return true; + private void performPaidSignCheck(PaidSign paidSign, SignChangeEvent event) { + 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; } - return false; + + performPaidSignTransaction(paidSign, player, event); } /** diff --git a/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java b/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java index 0fe4086..164c9bb 100644 --- a/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java +++ b/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java @@ -156,7 +156,7 @@ public final class PaidSignManager { Map