Initial commit
This commit is contained in:
157
src/main/java/net/knarcraft/paidsigns/container/PaidSign.java
Normal file
157
src/main/java/net/knarcraft/paidsigns/container/PaidSign.java
Normal file
@ -0,0 +1,157 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* A representation of a paid sign
|
||||
*/
|
||||
public class PaidSign {
|
||||
|
||||
private final String id;
|
||||
private final String cleanId;
|
||||
private final short lineIndex;
|
||||
private final double cost;
|
||||
private final OptionState ignoreCase;
|
||||
private final OptionState ignoreColor;
|
||||
|
||||
/**
|
||||
* 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 cost <p>The cost of creating this paid sign</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()) {
|
||||
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.cost = cost;
|
||||
this.ignoreCase = ignoreCase;
|
||||
this.ignoreColor = ignoreColor;
|
||||
this.cleanId = getCleanString(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id string of this paid sign
|
||||
*
|
||||
* @return <p>The id string of this paid sign</p>
|
||||
*/
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 line on the sign the id must be on to trigger payment
|
||||
*
|
||||
* @return <p>The sign line to search for the id</p>
|
||||
*/
|
||||
public short getLineIndex() {
|
||||
return lineIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the clean id of this paid sign
|
||||
*
|
||||
* @return <p>The clean id of 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
if (this.ignoreCase == OptionState.DEFAULT) {
|
||||
return PaidSigns.getInstance().ignoreCase();
|
||||
} else {
|
||||
return OptionState.getBooleanValue(this.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() {
|
||||
if (this.ignoreColor == OptionState.DEFAULT) {
|
||||
return PaidSigns.getInstance().ignoreColor();
|
||||
} else {
|
||||
return OptionState.getBooleanValue(this.ignoreColor);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user