Rewrites a bunch of code to improve the structure as specified in #5, #4, #2, #1

This commit is contained in:
2022-02-18 19:39:20 +01:00
parent d2f152334f
commit 16faa1ddb2
8 changed files with 254 additions and 195 deletions

View File

@ -2,6 +2,7 @@ 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;
@ -10,7 +11,7 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.Map;
import java.util.logging.Level;
/**
@ -20,7 +21,6 @@ public final class PaidSignManager {
private final List<PaidSign> paidSigns;
private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml");
private static final String signLineIdSeparator = "-,_,-";
/**
* Instantiate a new paid sign manager
@ -45,29 +45,18 @@ public final class PaidSignManager {
/**
* Removes a paid sign from this paid sign manager
*
* @param id <p>The identifier for the paid sign to remove</p>
* @param line <p>The line the identifier has to match to be valid</p>
* @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 id, short line) throws IOException {
boolean removed = this.paidSigns.removeIf((sign) -> sign.getId().equals(id) && sign.getLineIndex() == line);
public boolean removePaidSign(String name) throws IOException {
boolean removed = this.paidSigns.removeIf((sign) -> sign.getName().equals(name));
if (!removed) {
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);
}
/**
* 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
*
@ -104,18 +82,40 @@ public final class PaidSignManager {
}
List<PaidSign> paidSigns = new ArrayList<>();
for (String combinedId : signSection.getKeys(false)) {
String[] idParts = combinedId.split(signLineIdSeparator);
short lineNumber = Short.parseShort(idParts[0]);
String id = idParts[1];
double cost = signSection.getDouble(combinedId + ".cost");
OptionState ignoreCase = OptionState.getFromBoolean(signSection.getBoolean(combinedId + ".ignoreCase"));
OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(combinedId + ".ignoreColor"));
paidSigns.add(new PaidSign(id, lineNumber, cost, ignoreCase, ignoreColor));
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"));
PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor);
loadConditions(signSection, sign);
paidSigns.add(sign);
}
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
*
@ -127,34 +127,22 @@ public final class PaidSignManager {
ConfigurationSection signSection = configuration.createSection("paidSigns");
for (PaidSign sign : signs) {
String signId = sign.getLineIndex() + signLineIdSeparator + sign.getId();
signSection.set(signId + ".cost", sign.getCost());
signSection.set(signId + ".ignoreCase", sign.getIgnoreCase());
signSection.set(signId + ".ignoreColor", sign.getIgnoreColor());
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());
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);
}
/**
* 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();
}
}