Adds and option to match any paid sign condition instead of all conditions Adds checks for whether line indices are outside the allowed range Disallows any invalid regular expressions in sign conditions
189 lines
7.5 KiB
Java
189 lines
7.5 KiB
Java
package net.knarcraft.paidsigns.manager;
|
|
|
|
import net.knarcraft.paidsigns.PaidSigns;
|
|
import net.knarcraft.paidsigns.container.PaidSign;
|
|
import net.knarcraft.paidsigns.container.PaidSignCondition;
|
|
import net.knarcraft.paidsigns.property.OptionState;
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/**
|
|
* A manager that keeps track of all registered paid signs
|
|
*/
|
|
public final class PaidSignManager {
|
|
|
|
private final Map<String, PaidSign> paidSigns;
|
|
private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml");
|
|
|
|
/**
|
|
* Instantiate a new paid sign manager
|
|
*
|
|
* @param paidSigns <p>The paid signs this manager should manage</p>
|
|
*/
|
|
public PaidSignManager(Map<String, PaidSign> paidSigns) {
|
|
this.paidSigns = paidSigns;
|
|
}
|
|
|
|
/**
|
|
* Adds a new paid sign to this paid sign manager
|
|
*
|
|
* @param paidSign <p>The paid sign to add</p>
|
|
* @throws IOException <p>If unable to write to the signs file</p>
|
|
*/
|
|
public void addPaidSign(PaidSign paidSign) throws IOException {
|
|
this.paidSigns.put(paidSign.getName(), paidSign);
|
|
saveSigns();
|
|
}
|
|
|
|
/**
|
|
* Removes a paid sign from this paid sign manager
|
|
*
|
|
* @param name <p>The name of the paid sign to remove</p>
|
|
* @return <p>True if a sign was removed</p>
|
|
* @throws IOException <p>If unable to write to the signs file</p>
|
|
*/
|
|
public boolean removePaidSign(String name) throws IOException {
|
|
boolean removed = this.paidSigns.remove(name) != null;
|
|
if (!removed) {
|
|
return false;
|
|
} else {
|
|
saveSigns();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the paid sign with the given name
|
|
*
|
|
* @param name <p>The paid sign with the given name</p>
|
|
* @return <p>The paid sign with the given name, or null if it does not exist</p>
|
|
*/
|
|
public PaidSign getPaidSign(String name) {
|
|
return paidSigns.get(name);
|
|
}
|
|
|
|
/**
|
|
* Gets a copy of all registered paid signs
|
|
*
|
|
* @return <p>All registered paid signs</p>
|
|
*/
|
|
public Map<String, PaidSign> getAllPaidSigns() {
|
|
return new HashMap<>(paidSigns);
|
|
}
|
|
|
|
/**
|
|
* Loads paid signs from the signs file
|
|
*
|
|
* @return <p>The loaded paid signs</p>
|
|
*/
|
|
public static Map<String, PaidSign> loadSigns() {
|
|
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
|
|
ConfigurationSection signSection = configuration.getConfigurationSection("paidSigns");
|
|
if (signSection == null) {
|
|
PaidSigns.getInstance().getLogger().log(Level.WARNING, "Signs section not found in data.yml");
|
|
return new HashMap<>();
|
|
}
|
|
|
|
Map<String, PaidSign> paidSigns = new HashMap<>();
|
|
for (String name : signSection.getKeys(false)) {
|
|
double cost = signSection.getDouble(name + ".cost");
|
|
String permission = signSection.getString(name + ".permission");
|
|
OptionState ignoreCase = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreCase"));
|
|
OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreColor"));
|
|
boolean matchAnyCondition = signSection.getBoolean(name + ".matchAnyCondition");
|
|
PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor, matchAnyCondition);
|
|
loadConditions(signSection, sign);
|
|
paidSigns.put(name, sign);
|
|
}
|
|
return paidSigns;
|
|
}
|
|
|
|
/**
|
|
* Saves all signs registered to this paid sign manager
|
|
*
|
|
* @throws IOException <p>If unable to write to the signs file</p>
|
|
*/
|
|
public void saveSigns() throws IOException {
|
|
saveSigns(this.paidSigns);
|
|
}
|
|
|
|
/**
|
|
* Saves the given paid signs to the signs file
|
|
*
|
|
* @param signs <p>The signs to save</p>
|
|
* @throws IOException <p>If unable to write to the signs file</p>
|
|
*/
|
|
public static void saveSigns(Map<String, PaidSign> signs) throws IOException {
|
|
try {
|
|
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
|
|
ConfigurationSection signSection = configuration.createSection("paidSigns");
|
|
|
|
for (PaidSign sign : signs.values()) {
|
|
saveSign(signSection, sign);
|
|
}
|
|
configuration.save(signsFile);
|
|
} catch (IOException exception) {
|
|
Logger logger = PaidSigns.getInstance().getLogger();
|
|
logger.log(Level.SEVERE, "Exception encountered while trying to write " +
|
|
"to the data file");
|
|
logger.log(Level.SEVERE, Arrays.toString(exception.getStackTrace()));
|
|
throw exception;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Saves the given sign to the given configuration section
|
|
*
|
|
* @param signSection <p>The configuration section to save the sign to</p>
|
|
* @param sign <p>The sign to save</p>
|
|
*/
|
|
private static void saveSign(ConfigurationSection signSection, PaidSign sign) {
|
|
String name = sign.getName();
|
|
signSection.set(name + ".cost", sign.getCost());
|
|
signSection.set(name + ".permission", sign.getPermission());
|
|
signSection.set(name + ".ignoreCase", sign.getIgnoreCase());
|
|
signSection.set(name + ".ignoreColor", sign.getIgnoreColor());
|
|
signSection.set(name + ".matchAnyCondition", sign.matchAnyCondition());
|
|
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());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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));
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|