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 conditions = new HashMap<>(); /** * Instantiates a new paid sign * * @param name

A recognizable, unique, name used to identify this paid sign

* @param cost

The cost of creating this paid sign

* @param permission

The permission required to create the sign this paid sign matches

* @param ignoreCase

Whether to ignore case when looking for this permission sign

* @param ignoreColor

Whether to ignore color when looking for this permission sign

*/ 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

The name identifying this paid sign

*/ public String getName() { return name; } /** * Gets the cost of creating a sign matching this paid sign * * @return

The cost of creating a sign matching this paid sign

*/ public double getCost() { return this.cost; } /** * Gets the permission required by a player to create the sign this paid sign matches * * @return

The permission required by a player to create the sign this paid sign matches

*/ public String getPermission() { return this.permission; } /** * Gets all conditions registered for this paid sign * * @return

All conditions registered for this paid sign

*/ public Map getConditions() { return new HashMap<>(this.conditions); } /** * Gets whether the text case should be ignored for this paid sign * * @return

Whether the text case should be ignored for this paid sign

*/ public boolean getIgnoreCase() { return OptionState.getBooleanValue(this.ignoreCase, PaidSigns.getInstance().ignoreCase()); } /** * Gets whether the text color should be ignored for this paid sign * * @return

Whether the text color should be ignored for this paid sign

*/ 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

The sign lines to test against

* @return

True if this paid sign matches the given lines

*/ 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

The line on the sign the matched text must be on

* @param stringToMatch

The string that should be matched for the new condition to be true

* @param executeRegex

Whether to execute the string as RegEx

* @param ignoreCase

Whether to ignore case when matching against the condition

* @param ignoreColor

Whether to ignore color when matching against the condition

*/ 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

The sign line the condition belongs to

*/ public void removeCondition(short line) { this.conditions.remove(line); } }