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

150 lines
5.3 KiB
Java

package net.knarcraft.paidsigns.container;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.property.OptionState;
import java.util.HashMap;
import java.util.Map;
/**
* A representation of a paid sign
*/
public class PaidSign {
private final String name;
private final double cost;
private final String permission;
private final OptionState ignoreCase;
private final OptionState ignoreColor;
private final Map<Short, PaidSignCondition> conditions = new HashMap<>();
/**
* Instantiates a new paid sign
*
* @param name <p>A recognizable, unique, name used to identify this paid sign</p>
* @param cost <p>The cost of creating this paid sign</p>
* @param permission <p>The permission required to create the sign this paid sign matches</p>
* @param ignoreCase <p>Whether to ignore case when looking for this permission sign</p>
* @param ignoreColor <p>Whether to ignore color when looking for this permission sign</p>
*/
public PaidSign(String name, double cost, String permission, OptionState ignoreCase, OptionState ignoreColor) {
if (name == null || name.trim().isBlank()) {
throw new IllegalArgumentException("Name cannot be empty");
}
if (cost <= 0) {
throw new IllegalArgumentException("Cost must be larger than 0");
}
if (ignoreCase == null || ignoreColor == null) {
throw new IllegalArgumentException("Ignore case and ignore color options cannot be null");
}
this.name = name;
this.cost = cost;
this.permission = permission;
this.ignoreCase = ignoreCase;
this.ignoreColor = ignoreColor;
}
/**
* Gets the name identifying this paid sign
*
* @return <p>The name identifying this paid sign</p>
*/
public String getName() {
return name;
}
/**
* Gets the cost of creating a sign matching this paid sign
*
* @return <p>The cost of creating a sign matching this paid sign</p>
*/
public double getCost() {
return this.cost;
}
/**
* Gets the permission required by a player to create the sign this paid sign matches
*
* @return <p>The permission required by a player to create the sign this paid sign matches</p>
*/
public String getPermission() {
return this.permission;
}
/**
* Gets all conditions registered for this paid sign
*
* @return <p>All conditions registered for this paid sign</p>
*/
public Map<Short, PaidSignCondition> getConditions() {
return new HashMap<>(this.conditions);
}
/**
* Gets whether the text case should be ignored for this paid sign
*
* @return <p>Whether the text case should be ignored for this paid sign</p>
*/
public boolean getIgnoreCase() {
return OptionState.getBooleanValue(this.ignoreCase, PaidSigns.getInstance().ignoreCase());
}
/**
* Gets whether the text color should be ignored for this paid sign
*
* @return <p>Whether the text color should be ignored for this paid sign</p>
*/
public boolean getIgnoreColor() {
return OptionState.getBooleanValue(this.ignoreColor, PaidSigns.getInstance().ignoreColor());
}
/**
* Checks whether this paid sign matches the given set of sign lines
*
* @param lines <p>The sign lines to test against</p>
* @return <p>True if this paid sign matches the given lines</p>
*/
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 <p>The line on the sign the matched text must be on</p>
* @param stringToMatch <p>The string that should be matched for the new condition to be true</p>
* @param executeRegex <p>Whether to execute the string as RegEx</p>
* @param ignoreCase <p>Whether to ignore case when matching against the condition</p>
* @param ignoreColor <p>Whether to ignore color when matching against the condition</p>
*/
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));
}
/**
* Removes a condition from this paid sign
*
* @param line <p>The sign line the condition belongs to</p>
*/
public void removeCondition(short line) {
this.conditions.remove(line);
}
}