This commit is contained in:
@ -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<Short, PaidSignCondition> conditions = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Instantiates a new paid sign
|
||||
*
|
||||
* @param id <p>The string that identifies this type of paid sign</p>
|
||||
* @param lineIndex <p>The line the id has to be on to trigger payment</p>
|
||||
* @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 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 <p>The id string of this paid sign</p>
|
||||
* @return <p>The name identifying this paid sign</p>
|
||||
*/
|
||||
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 <p>The sign line to search for the id</p>
|
||||
* @return <p>The permission required by a player to create the sign this paid sign matches</p>
|
||||
*/
|
||||
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 <p>The clean id of this paid sign</p>
|
||||
* @return <p>All conditions registered for this paid sign</p>
|
||||
*/
|
||||
public String getCleanId() {
|
||||
return cleanId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the "clean" version of the given string
|
||||
*
|
||||
* <p>The "cleaning" removes color codes and lower-cases the string.</p>
|
||||
*
|
||||
* @param string <p>The string to clean</p>
|
||||
* @return <p>The cleaned string</p>
|
||||
*/
|
||||
public static String getCleanString(String string) {
|
||||
return ChatColor.stripColor(ColorHelper.translateAllColorCodes(string.toLowerCase()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this paid sign matches the given line
|
||||
*
|
||||
* @param lineNumber <p>The line number of the given line</p>
|
||||
* @param line <p>The line to compare against this paid sign's id</p>
|
||||
* @return <p>True if the line matches this sign</p>
|
||||
*/
|
||||
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 <p>The other paid sign to compare to</p>
|
||||
* @return <p>True if the signs match</p>
|
||||
*/
|
||||
public boolean matches(PaidSign paidSign) {
|
||||
return matches(paidSign.lineIndex, paidSign.id);
|
||||
public Map<Short, PaidSignCondition> getConditions() {
|
||||
return new HashMap<>(this.conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,11 +86,7 @@ public class PaidSign {
|
||||
* @return <p>Whether the text case should be ignored for this paid sign</p>
|
||||
*/
|
||||
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 <p>Whether the text color should be ignored for this paid sign</p>
|
||||
*/
|
||||
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 <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));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user