This commit is contained in:
parent
d2f152334f
commit
16faa1ddb2
@ -31,15 +31,14 @@ public class AddCommand implements CommandExecutor {
|
|||||||
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
|
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
|
||||||
|
|
||||||
String id = arguments.get(0);
|
String id = arguments.get(0);
|
||||||
short line;
|
|
||||||
double cost;
|
double cost;
|
||||||
try {
|
try {
|
||||||
line = (short) (Short.parseShort(arguments.get(1)) - 1);
|
|
||||||
cost = Double.parseDouble(arguments.get(2));
|
cost = Double.parseDouble(arguments.get(2));
|
||||||
} catch (NumberFormatException exception) {
|
} catch (NumberFormatException exception) {
|
||||||
sender.sendMessage("You provided an invalid number");
|
sender.sendMessage("You provided an invalid number");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
String permission = arguments.get(2);
|
||||||
OptionState ignoreCase = OptionState.DEFAULT;
|
OptionState ignoreCase = OptionState.DEFAULT;
|
||||||
OptionState ignoreColor = OptionState.DEFAULT;
|
OptionState ignoreColor = OptionState.DEFAULT;
|
||||||
if (arguments.size() > 3) {
|
if (arguments.size() > 3) {
|
||||||
@ -50,13 +49,14 @@ public class AddCommand implements CommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PaidSign sign = new PaidSign(id, line, cost, ignoreCase, ignoreColor);
|
PaidSign sign = new PaidSign(id, cost, permission, ignoreCase, ignoreColor);
|
||||||
|
/* TODO: Rewrite the method for comparing if paid signs are duplicated
|
||||||
for (PaidSign similarSign : manager.getPaidSigns(sign.getCleanId(), sign.getLineIndex())) {
|
for (PaidSign similarSign : manager.getPaidSigns(sign.getCleanId(), sign.getLineIndex())) {
|
||||||
if (sign.matches(similarSign)) {
|
if (sign.matches(similarSign)) {
|
||||||
sender.sendMessage("A paid sign with the same id and line already exists");
|
sender.sendMessage("A paid sign with the same id and line already exists");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
manager.addPaidSign(sign);
|
manager.addPaidSign(sign);
|
||||||
sender.sendMessage("Successfully added new paid sign");
|
sender.sendMessage("Successfully added new paid sign");
|
||||||
return true;
|
return true;
|
||||||
|
@ -2,7 +2,6 @@ package net.knarcraft.paidsigns.command;
|
|||||||
|
|
||||||
import net.knarcraft.paidsigns.PaidSigns;
|
import net.knarcraft.paidsigns.PaidSigns;
|
||||||
import net.knarcraft.paidsigns.utility.Tokenizer;
|
import net.knarcraft.paidsigns.utility.Tokenizer;
|
||||||
import org.apache.commons.lang.ArrayUtils;
|
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -26,17 +25,10 @@ public class RemoveCommand implements CommandExecutor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
|
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
|
||||||
String[] input = arguments.get(0).split("\\|");
|
String name = arguments.get(0);
|
||||||
short line;
|
|
||||||
try {
|
try {
|
||||||
line = (short) (Short.parseShort(input[0]) - 1);
|
if (PaidSigns.getInstance().getSignManager().removePaidSign(name)) {
|
||||||
} catch (NumberFormatException exception) {
|
|
||||||
sender.sendMessage("Invalid line number given");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
String id = String.join("|", (String[]) ArrayUtils.remove(input, 0));
|
|
||||||
try {
|
|
||||||
if (PaidSigns.getInstance().getSignManager().removePaidSign(id, line)) {
|
|
||||||
sender.sendMessage("Successfully removed paid sign");
|
sender.sendMessage("Successfully removed paid sign");
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage("No matching paid sign was found");
|
sender.sendMessage("No matching paid sign was found");
|
||||||
|
@ -23,7 +23,7 @@ public class RemoveTabCompleter implements TabCompleter {
|
|||||||
List<PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns();
|
List<PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns();
|
||||||
List<String> signIds = new ArrayList<>();
|
List<String> signIds = new ArrayList<>();
|
||||||
for (PaidSign sign : allPaidSigns) {
|
for (PaidSign sign : allPaidSigns) {
|
||||||
signIds.add("\"" + (sign.getLineIndex() + 1) + "|" + sign.getId() + "\"");
|
signIds.add("\"" + sign.getName() + "\"");
|
||||||
}
|
}
|
||||||
return signIds;
|
return signIds;
|
||||||
}
|
}
|
||||||
|
@ -2,58 +2,55 @@ package net.knarcraft.paidsigns.container;
|
|||||||
|
|
||||||
import net.knarcraft.paidsigns.PaidSigns;
|
import net.knarcraft.paidsigns.PaidSigns;
|
||||||
import net.knarcraft.paidsigns.property.OptionState;
|
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
|
* A representation of a paid sign
|
||||||
*/
|
*/
|
||||||
public class PaidSign {
|
public class PaidSign {
|
||||||
|
|
||||||
private final String id;
|
private final String name;
|
||||||
private final String cleanId;
|
|
||||||
private final short lineIndex;
|
|
||||||
private final double cost;
|
private final double cost;
|
||||||
|
private final String permission;
|
||||||
private final OptionState ignoreCase;
|
private final OptionState ignoreCase;
|
||||||
private final OptionState ignoreColor;
|
private final OptionState ignoreColor;
|
||||||
|
private final Map<Short, PaidSignCondition> conditions = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new paid sign
|
* Instantiates a new paid sign
|
||||||
*
|
*
|
||||||
* @param id <p>The string that identifies this type of paid sign</p>
|
* @param name <p>A recognizable, unique, name used to identify this 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 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 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>
|
* @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) {
|
public PaidSign(String name, double cost, String permission, OptionState ignoreCase, OptionState ignoreColor) {
|
||||||
if (id == null || id.trim().isBlank()) {
|
if (name == null || name.trim().isBlank()) {
|
||||||
throw new IllegalArgumentException("Id cannot be empty");
|
throw new IllegalArgumentException("Id cannot be empty");
|
||||||
}
|
}
|
||||||
if (cost <= 0) {
|
if (cost <= 0) {
|
||||||
throw new IllegalArgumentException("Cost must be larger than 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) {
|
if (ignoreCase == null || ignoreColor == null) {
|
||||||
throw new IllegalArgumentException("Ignore case and ignore color options cannot be null");
|
throw new IllegalArgumentException("Ignore case and ignore color options cannot be null");
|
||||||
}
|
}
|
||||||
this.id = id;
|
this.name = name;
|
||||||
this.lineIndex = lineIndex;
|
|
||||||
this.cost = cost;
|
this.cost = cost;
|
||||||
|
this.permission = permission;
|
||||||
this.ignoreCase = ignoreCase;
|
this.ignoreCase = ignoreCase;
|
||||||
this.ignoreColor = ignoreColor;
|
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() {
|
public String getName() {
|
||||||
return id;
|
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() {
|
public String getPermission() {
|
||||||
return lineIndex;
|
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() {
|
public Map<Short, PaidSignCondition> getConditions() {
|
||||||
return cleanId;
|
return new HashMap<>(this.conditions);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,11 +86,7 @@ public class PaidSign {
|
|||||||
* @return <p>Whether the text case should be ignored for this paid sign</p>
|
* @return <p>Whether the text case should be ignored for this paid sign</p>
|
||||||
*/
|
*/
|
||||||
public boolean getIgnoreCase() {
|
public boolean getIgnoreCase() {
|
||||||
if (this.ignoreCase == OptionState.DEFAULT) {
|
return OptionState.getBooleanValue(this.ignoreCase, PaidSigns.getInstance().ignoreCase());
|
||||||
return PaidSigns.getInstance().ignoreCase();
|
|
||||||
} else {
|
|
||||||
return OptionState.getBooleanValue(this.ignoreCase);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -147,11 +95,46 @@ public class PaidSign {
|
|||||||
* @return <p>Whether the text color should be ignored for this paid sign</p>
|
* @return <p>Whether the text color should be ignored for this paid sign</p>
|
||||||
*/
|
*/
|
||||||
public boolean getIgnoreColor() {
|
public boolean getIgnoreColor() {
|
||||||
if (this.ignoreColor == OptionState.DEFAULT) {
|
return OptionState.getBooleanValue(this.ignoreColor, PaidSigns.getInstance().ignoreColor());
|
||||||
return PaidSigns.getInstance().ignoreColor();
|
}
|
||||||
} else {
|
|
||||||
return OptionState.getBooleanValue(this.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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,96 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,7 +3,6 @@ package net.knarcraft.paidsigns.listener;
|
|||||||
import net.knarcraft.paidsigns.PaidSigns;
|
import net.knarcraft.paidsigns.PaidSigns;
|
||||||
import net.knarcraft.paidsigns.container.PaidSign;
|
import net.knarcraft.paidsigns.container.PaidSign;
|
||||||
import net.knarcraft.paidsigns.manager.EconomyManager;
|
import net.knarcraft.paidsigns.manager.EconomyManager;
|
||||||
import net.knarcraft.paidsigns.manager.PaidSignManager;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
@ -17,51 +16,51 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class SignListener implements Listener {
|
public class SignListener implements Listener {
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.LOW)
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public void onSignChange(SignChangeEvent event) {
|
public void onSignChange(SignChangeEvent event) {
|
||||||
if (event.isCancelled() || event.getPlayer().hasPermission("paidsigns.paymentexempt")) {
|
if (event.isCancelled() || event.getPlayer().hasPermission("paidsigns.paymentexempt")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] lines = event.getLines();
|
String[] lines = event.getLines();
|
||||||
PaidSignManager signManager = PaidSigns.getInstance().getSignManager();
|
List<PaidSign> matchingSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns();
|
||||||
for (short lineIndex = 0; lineIndex < lines.length; lineIndex++) {
|
for (PaidSign paidSign : matchingSigns) {
|
||||||
//Get all "weak" matches (any paid sign with a clean id matching the clean line)
|
//If a match is found, just return
|
||||||
List<PaidSign> matchingSigns = signManager.getPaidSigns(PaidSign.getCleanString(lines[lineIndex]), lineIndex);
|
if (matchSign(paidSign, lines, event)) {
|
||||||
if (matchingSigns.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (testMatchingSigns(lineIndex, lines[lineIndex], matchingSigns, event)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests all weak matches of paid signs to check if a strong match is found
|
* Checks if the given paid sign matches the given sign lines
|
||||||
*
|
*
|
||||||
* @param lineIndex <p>The index of the currently managed line</p>
|
* @param paidSign <p>The paid sign to test against the sign lines</p>
|
||||||
* @param line <p>The text on the currently managed line</p>
|
* @param lines <p>The lines of a sign</p>
|
||||||
* @param matchingSigns <p>The signs that weakly match the </p>
|
* @param event <p>The triggered sign change event to cancel if necessary</p>
|
||||||
* @param event <p>The triggered sign change event</p>
|
* @return <p>True if a match was found and actions have been taken</p>
|
||||||
* @return <p>True if a match was found and thw work is finished</p>
|
|
||||||
*/
|
*/
|
||||||
private boolean testMatchingSigns(short lineIndex, String line, List<PaidSign> matchingSigns, SignChangeEvent event) {
|
private boolean matchSign(PaidSign paidSign, String[] lines, SignChangeEvent event) {
|
||||||
for (PaidSign paidSign : matchingSigns) {
|
if (paidSign.matches(lines)) {
|
||||||
if (paidSign.matches(lineIndex, line)) {
|
Player player = event.getPlayer();
|
||||||
Player player = event.getPlayer();
|
String permission = paidSign.getPermission();
|
||||||
double cost = paidSign.getCost();
|
//If a match is found, but the player is missing the permission, assume no plugin sign was created
|
||||||
boolean canAfford = EconomyManager.canAfford(player, cost);
|
if (permission != null && !permission.trim().isEmpty() && !player.hasPermission(permission)) {
|
||||||
if (!canAfford) {
|
|
||||||
player.sendMessage("[PaidSigns] You cannot afford to create this sign");
|
|
||||||
event.setCancelled(true);
|
|
||||||
} else {
|
|
||||||
String unit = EconomyManager.getCurrency(cost != 1);
|
|
||||||
player.sendMessage(String.format("[PaidSigns] You paid %.2f %s to create the sign", cost, unit));
|
|
||||||
EconomyManager.withdraw(player, cost);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double cost = paidSign.getCost();
|
||||||
|
boolean canAfford = EconomyManager.canAfford(player, cost);
|
||||||
|
if (!canAfford) {
|
||||||
|
player.sendMessage("[PaidSigns] You cannot afford to create this sign");
|
||||||
|
event.setCancelled(true);
|
||||||
|
} else {
|
||||||
|
String unit = EconomyManager.getCurrency(cost != 1);
|
||||||
|
player.sendMessage(String.format("[PaidSigns] You paid %.2f %s to create the sign", cost, unit));
|
||||||
|
EconomyManager.withdraw(player, cost);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package net.knarcraft.paidsigns.manager;
|
|||||||
|
|
||||||
import net.knarcraft.paidsigns.PaidSigns;
|
import net.knarcraft.paidsigns.PaidSigns;
|
||||||
import net.knarcraft.paidsigns.container.PaidSign;
|
import net.knarcraft.paidsigns.container.PaidSign;
|
||||||
|
import net.knarcraft.paidsigns.container.PaidSignCondition;
|
||||||
import net.knarcraft.paidsigns.property.OptionState;
|
import net.knarcraft.paidsigns.property.OptionState;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
@ -10,7 +11,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Predicate;
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -20,7 +21,6 @@ public final class PaidSignManager {
|
|||||||
|
|
||||||
private final List<PaidSign> paidSigns;
|
private final List<PaidSign> paidSigns;
|
||||||
private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml");
|
private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml");
|
||||||
private static final String signLineIdSeparator = "-,_,-";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate a new paid sign manager
|
* Instantiate a new paid sign manager
|
||||||
@ -45,29 +45,18 @@ public final class PaidSignManager {
|
|||||||
/**
|
/**
|
||||||
* Removes a paid sign from this paid sign manager
|
* Removes a paid sign from this paid sign manager
|
||||||
*
|
*
|
||||||
* @param id <p>The identifier for the paid sign to remove</p>
|
* @param name <p>The name of the paid sign to remove</p>
|
||||||
* @param line <p>The line the identifier has to match to be valid</p>
|
|
||||||
* @return <p>True if a sign was removed</p>
|
* @return <p>True if a sign was removed</p>
|
||||||
* @throws IOException <p>If unable to write to the signs file</p>
|
* @throws IOException <p>If unable to write to the signs file</p>
|
||||||
*/
|
*/
|
||||||
public boolean removePaidSign(String id, short line) throws IOException {
|
public boolean removePaidSign(String name) throws IOException {
|
||||||
boolean removed = this.paidSigns.removeIf((sign) -> sign.getId().equals(id) && sign.getLineIndex() == line);
|
boolean removed = this.paidSigns.removeIf((sign) -> sign.getName().equals(name));
|
||||||
if (!removed) {
|
if (!removed) {
|
||||||
return false;
|
return false;
|
||||||
|
} else {
|
||||||
|
saveSigns(this.paidSigns);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
saveSigns(this.paidSigns);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the paid signs that match the given properties
|
|
||||||
*
|
|
||||||
* @param cleanId <p>The clean id to search for</p>
|
|
||||||
* @param line <p>The line number to search for</p>
|
|
||||||
* @return <p>The paid signs that matched the given properties</p>
|
|
||||||
*/
|
|
||||||
public List<PaidSign> getPaidSigns(String cleanId, short line) {
|
|
||||||
return filterPaidSigns(filterPaidSigns(paidSigns, line), cleanId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -79,17 +68,6 @@ public final class PaidSignManager {
|
|||||||
return new ArrayList<>(paidSigns);
|
return new ArrayList<>(paidSigns);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Filters a list of paid signs to match the given line number
|
|
||||||
*
|
|
||||||
* @param paidSigns <p>The list of paid signs to start with</p>
|
|
||||||
* @param line <p>The line number to filter by</p>
|
|
||||||
* @return <p>The filtered list of paid signs</p>
|
|
||||||
*/
|
|
||||||
private static List<PaidSign> filterPaidSigns(List<PaidSign> paidSigns, short line) {
|
|
||||||
return filterPaidSigns(paidSigns, (paidSign) -> paidSign.getLineIndex() == line);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads paid signs from the signs file
|
* Loads paid signs from the signs file
|
||||||
*
|
*
|
||||||
@ -104,18 +82,40 @@ public final class PaidSignManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<PaidSign> paidSigns = new ArrayList<>();
|
List<PaidSign> paidSigns = new ArrayList<>();
|
||||||
for (String combinedId : signSection.getKeys(false)) {
|
for (String name : signSection.getKeys(false)) {
|
||||||
String[] idParts = combinedId.split(signLineIdSeparator);
|
double cost = signSection.getDouble(name + ".cost");
|
||||||
short lineNumber = Short.parseShort(idParts[0]);
|
String permission = signSection.getString(name + ".permission");
|
||||||
String id = idParts[1];
|
OptionState ignoreCase = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreCase"));
|
||||||
double cost = signSection.getDouble(combinedId + ".cost");
|
OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreColor"));
|
||||||
OptionState ignoreCase = OptionState.getFromBoolean(signSection.getBoolean(combinedId + ".ignoreCase"));
|
PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor);
|
||||||
OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(combinedId + ".ignoreColor"));
|
loadConditions(signSection, sign);
|
||||||
paidSigns.add(new PaidSign(id, lineNumber, cost, ignoreCase, ignoreColor));
|
paidSigns.add(sign);
|
||||||
}
|
}
|
||||||
return paidSigns;
|
return paidSigns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads any saved paid sign conditions and applies them to the given sign
|
||||||
|
*
|
||||||
|
* @param signSection <p>The configuration section containing sign information</p>
|
||||||
|
* @param sign <p>The sign to load conditions for</p>
|
||||||
|
*/
|
||||||
|
private static void loadConditions(ConfigurationSection signSection, PaidSign sign) {
|
||||||
|
ConfigurationSection conditionSection = signSection.getConfigurationSection(sign.getName() + ".conditions");
|
||||||
|
if (conditionSection != null) {
|
||||||
|
for (String lineIndex : conditionSection.getKeys(false)) {
|
||||||
|
short lineNumber = Short.parseShort(lineIndex);
|
||||||
|
String stringToMatch = conditionSection.getString(lineIndex + ".stringToMatch");
|
||||||
|
boolean executeRegEx = conditionSection.getBoolean(lineIndex + ".executeRegEx");
|
||||||
|
boolean ignoreConditionCase = conditionSection.getBoolean(lineIndex + ".ignoreCase");
|
||||||
|
boolean ignoreConditionColor = conditionSection.getBoolean(lineIndex + ".ignoreColor");
|
||||||
|
sign.addCondition(lineNumber, stringToMatch, executeRegEx,
|
||||||
|
OptionState.getFromBoolean(ignoreConditionCase),
|
||||||
|
OptionState.getFromBoolean(ignoreConditionColor));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the given paid signs to the signs file
|
* Saves the given paid signs to the signs file
|
||||||
*
|
*
|
||||||
@ -127,34 +127,22 @@ public final class PaidSignManager {
|
|||||||
ConfigurationSection signSection = configuration.createSection("paidSigns");
|
ConfigurationSection signSection = configuration.createSection("paidSigns");
|
||||||
|
|
||||||
for (PaidSign sign : signs) {
|
for (PaidSign sign : signs) {
|
||||||
String signId = sign.getLineIndex() + signLineIdSeparator + sign.getId();
|
String name = sign.getName();
|
||||||
signSection.set(signId + ".cost", sign.getCost());
|
signSection.set(name + ".cost", sign.getCost());
|
||||||
signSection.set(signId + ".ignoreCase", sign.getIgnoreCase());
|
signSection.set(name + ".permission", sign.getPermission());
|
||||||
signSection.set(signId + ".ignoreColor", sign.getIgnoreColor());
|
signSection.set(name + ".ignoreCase", sign.getIgnoreCase());
|
||||||
|
signSection.set(name + ".ignoreColor", sign.getIgnoreColor());
|
||||||
|
ConfigurationSection conditionsSection = signSection.createSection(name + ".conditions");
|
||||||
|
Map<Short, PaidSignCondition> signConditions = sign.getConditions();
|
||||||
|
for (short lineIndex : signConditions.keySet()) {
|
||||||
|
PaidSignCondition condition = signConditions.get(lineIndex);
|
||||||
|
conditionsSection.set(lineIndex + ".stringToMatch", condition.getStringToMatch());
|
||||||
|
conditionsSection.set(lineIndex + ".executeRegEx", condition.executeRegex());
|
||||||
|
conditionsSection.set(lineIndex + ".ignoreCase", condition.ignoreCase());
|
||||||
|
conditionsSection.set(lineIndex + ".ignoreColor", condition.ignoreColor());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
configuration.save(signsFile);
|
configuration.save(signsFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Filters a list of paid signs to match the given clean id
|
|
||||||
*
|
|
||||||
* @param paidSigns <p>The list of paid signs to start with</p>
|
|
||||||
* @param cleanId <p>The clean id to filter by</p>
|
|
||||||
* @return <p>The filtered list of paid signs</p>
|
|
||||||
*/
|
|
||||||
private static List<PaidSign> filterPaidSigns(List<PaidSign> paidSigns, String cleanId) {
|
|
||||||
return filterPaidSigns(paidSigns, (paidSign) -> paidSign.getCleanId().equals(cleanId));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filters a list of paid signs using the given predicate
|
|
||||||
*
|
|
||||||
* @param paidSigns <p>The list of paid signs to start with</p>
|
|
||||||
* @param predicate <p>The predicate used to filter paid signs</p>
|
|
||||||
* @return <p>The filtered list of paid signs</p>
|
|
||||||
*/
|
|
||||||
private static List<PaidSign> filterPaidSigns(List<PaidSign> paidSigns, Predicate<PaidSign> predicate) {
|
|
||||||
return new ArrayList<>(paidSigns).stream().filter(predicate).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,14 +23,15 @@ public enum OptionState {
|
|||||||
/**
|
/**
|
||||||
* Gets the boolean value of the given option state if it's boolean compatible
|
* Gets the boolean value of the given option state if it's boolean compatible
|
||||||
*
|
*
|
||||||
* @param optionState <p>The option state to get the boolean from</p>
|
* @param optionState <p>The option state to get the boolean from</p>
|
||||||
* @return <p>The boolean value, or an illegal argument exception if called on DEFAULT</p>
|
* @param defaultValue <p>The default value to use if the option state is DEFAULT</p>
|
||||||
|
* @return <p>The boolean value</p>
|
||||||
*/
|
*/
|
||||||
public static boolean getBooleanValue(OptionState optionState) {
|
public static boolean getBooleanValue(OptionState optionState, boolean defaultValue) {
|
||||||
return switch (optionState) {
|
return switch (optionState) {
|
||||||
case TRUE -> true;
|
case TRUE -> true;
|
||||||
case FALSE -> false;
|
case FALSE -> false;
|
||||||
case DEFAULT -> throw new IllegalArgumentException("No boolean value available for DEFAULT");
|
case DEFAULT -> defaultValue;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user