Files
PaidSigns/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java

97 lines
3.0 KiB
Java

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 <p>The string/regular expression the line has to match to fulfill this condition</p>
* @param executeRegex <p>Whether to execute the match string as a regular expression</p>
* @param ignoreCase <p>Whether to ignore uppercase/lowercase when comparing against this condition</p>
* @param ignoreColor <p>Whether to ignore color codes when comparing against this condition</p>
*/
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 <p>The string this condition should match</p>
*/
public String getStringToMatch() {
return this.stringToMatch;
}
/**
* Gets whether to execute the match string as RegEx
*
* @return <p>Whether to execute the match string as RegEx</p>
*/
public boolean executeRegex() {
return this.executeRegex;
}
/**
* Gets whether to ignore case when trying to match strings
*
* @return <p>Whether to ignore case when trying to match strings</p>
*/
public boolean ignoreCase() {
return this.ignoreCase;
}
/**
* Gets whether to ignore color when trying to match strings
*
* @return <p>Whether to ignore color when trying to match strings</p>
*/
public boolean ignoreColor() {
return this.ignoreColor;
}
/**
* Tests whether the given line matches this condition
*
* @param line <p>The sign line to test</p>
* @return <p>True if this condition matches the given line</p>
*/
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);
}
}
}
}