Implements the add condition command and its tab completion

This commit is contained in:
2022-02-19 17:51:27 +01:00
parent d76d5cdf93
commit 90b5ff7304
9 changed files with 247 additions and 46 deletions

View File

@@ -9,8 +9,7 @@ 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.HashMap;
import java.util.Map;
import java.util.logging.Level;
@@ -19,7 +18,7 @@ import java.util.logging.Level;
*/
public final class PaidSignManager {
private final List<PaidSign> paidSigns;
private final Map<String, PaidSign> paidSigns;
private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml");
/**
@@ -27,7 +26,7 @@ public final class PaidSignManager {
*
* @param paidSigns <p>The paid signs this manager should manage</p>
*/
public PaidSignManager(List<PaidSign> paidSigns) {
public PaidSignManager(Map<String, PaidSign> paidSigns) {
this.paidSigns = paidSigns;
}
@@ -38,7 +37,7 @@ public final class PaidSignManager {
* @throws IOException <p>If unable to write to the signs file</p>
*/
public void addPaidSign(PaidSign paidSign) throws IOException {
this.paidSigns.add(paidSign);
this.paidSigns.put(paidSign.getName(), paidSign);
saveSigns(this.paidSigns);
}
@@ -50,7 +49,7 @@ public final class PaidSignManager {
* @throws IOException <p>If unable to write to the signs file</p>
*/
public boolean removePaidSign(String name) throws IOException {
boolean removed = this.paidSigns.removeIf((sign) -> sign.getName().equals(name));
boolean removed = this.paidSigns.remove(name) != null;
if (!removed) {
return false;
} else {
@@ -59,13 +58,23 @@ public final class PaidSignManager {
}
}
/**
* 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 List<PaidSign> getAllPaidSigns() {
return new ArrayList<>(paidSigns);
public Map<String, PaidSign> getAllPaidSigns() {
return new HashMap<>(paidSigns);
}
/**
@@ -73,15 +82,15 @@ public final class PaidSignManager {
*
* @return <p>The loaded paid signs</p>
*/
public static List<PaidSign> loadSigns() {
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 ArrayList<>();
return new HashMap<>();
}
List<PaidSign> paidSigns = new ArrayList<>();
Map<String, PaidSign> paidSigns = new HashMap<>();
for (String name : signSection.getKeys(false)) {
double cost = signSection.getDouble(name + ".cost");
String permission = signSection.getString(name + ".permission");
@@ -89,7 +98,7 @@ public final class PaidSignManager {
OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreColor"));
PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor);
loadConditions(signSection, sign);
paidSigns.add(sign);
paidSigns.put(name, sign);
}
return paidSigns;
}
@@ -122,12 +131,12 @@ public final class PaidSignManager {
* @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 {
public static void saveSigns(Map<String, PaidSign> signs) throws IOException {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
ConfigurationSection signSection = configuration.createSection("paidSigns");
for (PaidSign sign : signs) {
String name = sign.getName();
for (PaidSign sign : signs.values()) {
String name = signSection.getName();
signSection.set(name + ".cost", sign.getCost());
signSection.set(name + ".permission", sign.getPermission());
signSection.set(name + ".ignoreCase", sign.getIgnoreCase());