Initial commit
This commit is contained in:
@ -0,0 +1,160 @@
|
||||
package net.knarcraft.paidsigns.manager;
|
||||
|
||||
import net.knarcraft.paidsigns.PaidSigns;
|
||||
import net.knarcraft.paidsigns.container.PaidSign;
|
||||
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.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* A manager that keeps track of all registered paid signs
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @param paidSigns <p>The paid signs this manager should manage</p>
|
||||
*/
|
||||
public PaidSignManager(List<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.add(paidSign);
|
||||
saveSigns(this.paidSigns);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* @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);
|
||||
if (!removed) {
|
||||
return false;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a copy of all registered paid signs
|
||||
*
|
||||
* @return <p>All registered paid signs</p>
|
||||
*/
|
||||
public List<PaidSign> getAllPaidSigns() {
|
||||
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
|
||||
*
|
||||
* @return <p>The loaded paid signs</p>
|
||||
*/
|
||||
public static List<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 ArrayList<>();
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
return 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(List<PaidSign> signs) throws IOException {
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
|
||||
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());
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user