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 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; } /** * Tests whether the given line matches this condition * * @param lineThe sign line to test
* @returnTrue 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); } } } }