From a37bf66d6e20d9c8b4f304e8ac67f9be0ab00ee9 Mon Sep 17 00:00:00 2001 From: Mineshafter61 Date: Mon, 29 Jul 2024 17:52:13 +0800 Subject: [PATCH 01/12] Add Javadocs and list methods --- .../iciwi/config/CustomConfig.java | 293 +++++++++++++++++- 1 file changed, 284 insertions(+), 9 deletions(-) diff --git a/src/main/java/mikeshafter/iciwi/config/CustomConfig.java b/src/main/java/mikeshafter/iciwi/config/CustomConfig.java index 45f98cd..82b2320 100644 --- a/src/main/java/mikeshafter/iciwi/config/CustomConfig.java +++ b/src/main/java/mikeshafter/iciwi/config/CustomConfig.java @@ -4,7 +4,11 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + import java.io.File; +import java.util.List; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -35,9 +39,7 @@ public void save () { protected Plugin getConfigPlugin () {return this.plugin;} -public void saveDefaultConfig () { - if (!file.exists()) { plugin.saveResource(name, false); } -} +public void saveDefaultConfig () { if (!file.exists()) { plugin.saveResource(name, false); } } public File getFile () {return file;} @@ -46,25 +48,298 @@ public YamlConfiguration get () { return config; } +/** + * Gets the requested Object by path. + *

+ * If the Object does not exist but a default value has been specified, + * this will return the default value. If the Object does not exist and no + * default value was specified, this will return null. + * + * @param path Path of the Object to get. + * @return Requested Object. + */ public Object get (String path) {return this.config.get(path);} -public String getString (String path) { +/** + * Gets the requested String by path. + *

+ * If the String does not exist but a default value has been specified, + * this will return the default value. If the String does not exist and no + * default value was specified, this will return null. + * + * @param path Path of the String to get. + * @return Requested String. + */ +public String getString (@NotNull String path) { var s = this.config.getString(path); return s == null ? "" : s; } -public boolean getBoolean (String path) { return this.config.getBoolean(path); } +/** + * Gets the requested boolean by path. + *

+ * If the boolean does not exist but a default value has been specified, + * this will return the default value. If the boolean does not exist and + * no default value was specified, this will return false. + * + * @param path Path of the boolean to get. + * @return Requested boolean. + */ +public boolean getBoolean (@NotNull String path) { return this.config.getBoolean(path); } + +/** + * Gets the requested int by path. + *

+ * If the int does not exist but a default value has been specified, this + * will return the default value. If the int does not exist and no default + * value was specified, this will return 0. + * + * @param path Path of the int to get. + * @return Requested int. + */ +public int getInt (@NotNull String path) { return this.config.getInt(path); } + +/** + * Gets the requested double by path. + *

+ * If the double does not exist but a default value has been specified, + * this will return the default value. If the double does not exist and no + * default value was specified, this will return 0. + * + * @param path Path of the double to get. + * @return Requested double. + */ +public double getDouble (@NotNull String path) { return this.config.getDouble(path); } + +/** + * Gets the requested long by path. + *

+ * If the long does not exist but a default value has been specified, this + * will return the default value. If the long does not exist and no + * default value was specified, this will return 0. + * + * @param path Path of the long to get. + * @return Requested long. + */ +public long getLong (@NotNull String path) { return this.config.getLong(path); } + +/** + * Gets the requested List by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return null. + * + * @param path Path of the List to get. + * @return Requested List. + */ +public List getList(@NotNull String path) { return this.config.getList(path); }; + +/** + * Gets the requested List of String by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a String if possible, + * but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of String. + */ +@NotNull +public List getStringList(@NotNull String path){return this.config.getStringList(path);} + +/** + * Gets the requested List of Integer by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Integer if possible, + * but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Integer. + */ +@NotNull +public List getIntegerList(@NotNull String path){return this.config.getIntegerList(path);} + +/** + * Gets the requested List of Boolean by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Boolean if possible, + * but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Boolean. + */ +@NotNull +public List getBooleanList(@NotNull String path){return this.config.getBooleanList(path);} -public int getInt (String path) { return this.config.getInt(path); } +/** + * Gets the requested List of Double by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Double if possible, + * but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Double. + */ +@NotNull +public List getDoubleList(@NotNull String path){return this.config.getDoubleList(path);}; -public double getDouble (String path) { return this.config.getDouble(path); } +/** + * Gets the requested List of Float by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Float if possible, + * but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Float. + */ +@NotNull +public List getFloatList(@NotNull String path){return this.config.getFloatList(path);} -public long getLong (String path) { return this.config.getLong(path); } +/** + * Gets the requested List of Long by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Long if possible, + * but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Long. + */ +@NotNull +public List getLongList(@NotNull String path){return this.config.getLongList(path);} -public ConfigurationSection getConfigurationSection (String path) { return this.config.getConfigurationSection(path); } +/** + * Gets the requested List of Byte by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Byte if possible, + * but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Byte. + */ +@NotNull +public List getByteList(@NotNull String path){return this.config.getByteList(path);} +/** + * Gets the requested List of Character by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Character if + * possible, but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Character. + */ +@NotNull +public List getCharacterList(@NotNull String path) {return this.config.getCharacterList(path);} + +/** + * Gets the requested List of Short by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Short if possible, + * but may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Short. + */ +@NotNull +public List getShortList(@NotNull String path) {return this.config.getShortList(path);} + +/** + * Gets the requested List of Maps by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no + * default value was specified, this will return an empty List. + *

+ * This method will attempt to cast any values into a Map if possible, but + * may miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Maps. + */ +@NotNull +public List> getMapList(@NotNull String path) {return this.config.getMapList(path);} + +/** + * Gets the requested ConfigurationSection by path. + *

+ * If the ConfigurationSection does not exist but a default value has been + * specified, this will return the default value. If the + * ConfigurationSection does not exist and no default value was specified, + * this will return null. + * + * @param path Path of the ConfigurationSection to get. + * @return Requested ConfigurationSection. + */ +public ConfigurationSection getConfigurationSection (@NotNull String path) { return this.config.getConfigurationSection(path); } + +/** + * Sets the specified path to the given value. + *

+ * If value is null, the entry will be removed. Any existing entry will be + * replaced, regardless of what the new value is. + *

+ * + * @param path Path of the object to set. + * @param value New value to set the path to. + */ public void set (String path, Object value) { this.config.set(path, value); } +/** + * Converts an array of path nodes to a path string. + *

+ * The final return value will be in the format "element 0"."element 1"."element 2"... and so on. + *

+ * + * @param pathNodes Names of the nodes of the path + * @return The path string for other methods. + */ +protected String toPath (String @NotNull ... pathNodes) { + // optimisation + if (pathNodes.length == 1) return pathNodes[0]; + + StringBuilder s = new StringBuilder(pathNodes[0]); + for (int i = 1; i < pathNodes.length; i++) { + s.append(".").append(pathNodes[i]); + } + return s.toString(); +} + public void reload () { file = new File(plugin.getDataFolder(), name); try { From 52c56781a6624155ddd8542db13b1f002a493fa7 Mon Sep 17 00:00:00 2001 From: Mineshafter61 Date: Mon, 29 Jul 2024 17:53:08 +0800 Subject: [PATCH 02/12] Improve readability --- .../java/mikeshafter/iciwi/util/GateCreateListener.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/mikeshafter/iciwi/util/GateCreateListener.java b/src/main/java/mikeshafter/iciwi/util/GateCreateListener.java index d2cbb8e..6c7d1e3 100644 --- a/src/main/java/mikeshafter/iciwi/util/GateCreateListener.java +++ b/src/main/java/mikeshafter/iciwi/util/GateCreateListener.java @@ -8,25 +8,22 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.SignChangeEvent; -import static mikeshafter.iciwi.util.IciwiUtil.parseComponent; -import static mikeshafter.iciwi.util.IciwiUtil.containsMany; public class GateCreateListener implements Listener { @EventHandler(priority = EventPriority.LOWEST) public void onGateCreate(SignChangeEvent event) { Lang lang = Iciwi.getPlugin(Iciwi.class).lang; - final String line = parseComponent(event.line(0)); + final String line = IciwiUtil.parseComponent(event.line(0)); final Player player = event.getPlayer(); - int createdSign = containsMany(line, lang.getString("entry"), lang.getString("exit"), lang.getString("member"), lang.getString("payment"), lang.getString("faregate"), lang.getString("validator"), lang.getString("tickets"), lang.getString("cards"), lang.getString("passes"), lang.getString("custom-tickets")); + int createdSign = IciwiUtil.containsMany(line, lang.getString("entry"), lang.getString("exit"), lang.getString("member"), lang.getString("payment"), lang.getString("faregate"), lang.getString("validator"), lang.getString("tickets"), lang.getString("cards"), lang.getString("passes"), lang.getString("custom-tickets")); if (createdSign != -1 && !player.hasPermission("iciwi.create")) { event.setCancelled(true); return; } - switch (createdSign) { case 0 -> player.sendMessage(lang.getString("create-entry-sign")); case 1 -> player.sendMessage(lang.getString("create-exit-sign")); From 6e8293ee639547bd2ea1f5863f50224e7fdac2c6 Mon Sep 17 00:00:00 2001 From: Mineshafter61 Date: Mon, 29 Jul 2024 17:53:36 +0800 Subject: [PATCH 03/12] Update override method --- src/main/java/mikeshafter/iciwi/config/Lang.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/mikeshafter/iciwi/config/Lang.java b/src/main/java/mikeshafter/iciwi/config/Lang.java index 8e71b9b..e007c9a 100644 --- a/src/main/java/mikeshafter/iciwi/config/Lang.java +++ b/src/main/java/mikeshafter/iciwi/config/Lang.java @@ -1,5 +1,6 @@ package mikeshafter.iciwi.config; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; public class Lang extends CustomConfig { public Lang () {super("lang.yml");} @@ -7,7 +8,7 @@ public Component getComponent (String path) { var s = super.getString(path); return s.isEmpty() ? Component.text("Error: No text input for path: " + path) : Component.text(s); } -@Override public String getString (String path) { +@Override public String getString (@NotNull String path) { var s = super.getString(path); return s.isEmpty() ? "Error: No text input for path: " + path : s; } From 73437dc4bba3cd4e1ec418c5b5665fa94eef2f61 Mon Sep 17 00:00:00 2001 From: Mineshafter61 Date: Mon, 29 Jul 2024 17:54:48 +0800 Subject: [PATCH 04/12] Change fares.yml format to class>start>end:price --- src/main/java/mikeshafter/iciwi/Commands.java | 32 ++-- src/main/java/mikeshafter/iciwi/Iciwi.java | 2 +- .../java/mikeshafter/iciwi/config/Fares.java | 174 ++++++++++-------- 3 files changed, 114 insertions(+), 94 deletions(-) diff --git a/src/main/java/mikeshafter/iciwi/Commands.java b/src/main/java/mikeshafter/iciwi/Commands.java index 7a996c2..4f1ddbc 100644 --- a/src/main/java/mikeshafter/iciwi/Commands.java +++ b/src/main/java/mikeshafter/iciwi/Commands.java @@ -48,12 +48,12 @@ private String formatString(String message, String... items) { return owners.getAllCompanies().stream().toList(); } - @Suggestions("station_list") - public List suggestStationList( + @Suggestions("start_list") + public List suggestStartList ( final @NonNull CommandContext ctx, final @NonNull String input ) { - return fares.getAllStations().stream().toList(); + return fares.getAllStarts().stream().toList(); } @Suggestions("railpass_list") @@ -224,7 +224,7 @@ public void owners_alias_unset( public void owners_operator_add( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "station", suggestions = "station_list") String station, + final @NonNull @Argument(value = "station", suggestions = "start_list") String station, final @NonNull @Argument(value = "company", suggestions = "company_list") String company ) { owners.addOwner(station, company); @@ -238,7 +238,7 @@ public void owners_operator_add( public void owners_operator_remove( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "station", suggestions = "station_list") String station, + final @NonNull @Argument(value = "station", suggestions = "start_list") String station, final @NonNull @Argument(value = "company", suggestions = "company_list") String company ) { owners.addOwner(station, company); @@ -252,7 +252,7 @@ public void owners_operator_remove( public void owners_operator_set( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "station", suggestions = "station_list") String station, + final @NonNull @Argument(value = "station", suggestions = "start_list") String station, final @NonNull @Argument(value = "company", suggestions = "company_list") String company ) { owners.setOwners(station, Collections.singletonList(company)); @@ -266,7 +266,7 @@ public void owners_operator_set( public void owners_operator_delete( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "station", suggestions = "station_list") String station + final @NonNull @Argument(value = "station", suggestions = "start_list") String station ) { owners.set("Operators." + station, null); owners.save(); @@ -348,8 +348,8 @@ public void owners_railpass_delete( public void fares_set( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "start", suggestions = "station_list") String start, - final @NonNull @Argument(value = "end", suggestions = "station_list") String end, + final @NonNull @Argument(value = "start", suggestions = "start_list") String start, + final @NonNull @Argument(value = "end", suggestions = "start_list") String end, final @NonNull @Argument(value = "fareClass") String fareClass, final @NonNull @Argument(value = "price") Double price ) { @@ -363,8 +363,8 @@ public void fares_set( public void fares_check( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "start", suggestions = "station_list") String start, - final @Argument(value = "end", suggestions = "station_list") String end, + final @NonNull @Argument(value = "start", suggestions = "start_list") String start, + final @Argument(value = "end", suggestions = "start_list") String end, final @Argument(value = "fareClass") @Quoted String fareClass ) { Set s; @@ -381,8 +381,8 @@ public void fares_check( public void fares_set( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "start", suggestions = "station_list") String start, - final @NonNull @Argument(value = "end", suggestions = "station_list") String end, + final @NonNull @Argument(value = "start", suggestions = "start_list") String start, + final @NonNull @Argument(value = "end", suggestions = "start_list") String end, final @NonNull @Argument(value = "fareClass") String fareClass ) { fares.unsetFare(start, end, fareClass); @@ -395,8 +395,8 @@ public void fares_set( public void fares_set( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "start", suggestions = "station_list") String start, - final @NonNull @Argument(value = "end", suggestions = "station_list") String end + final @NonNull @Argument(value = "start", suggestions = "start_list") String start, + final @NonNull @Argument(value = "end", suggestions = "start_list") String end ) { fares.deleteJourney(start, end); sender.sendMessage(formatString("All fares from %s to %s has been deleted.", start, end)); @@ -408,7 +408,7 @@ public void fares_set( public void fares_set( final @NonNull CommandSender sender, final Iciwi plugin, - final @NonNull @Argument(value = "start", suggestions = "station_list") String start + final @NonNull @Argument(value = "start", suggestions = "start_list") String start ) { fares.deleteStation(start); sender.sendMessage(formatString("All fares to all stations from %s has been deleted.", start)); diff --git a/src/main/java/mikeshafter/iciwi/Iciwi.java b/src/main/java/mikeshafter/iciwi/Iciwi.java index 71d7a05..8b29aad 100644 --- a/src/main/java/mikeshafter/iciwi/Iciwi.java +++ b/src/main/java/mikeshafter/iciwi/Iciwi.java @@ -73,7 +73,7 @@ private void registerEvents () { } private void registerStations () { - Set stations = fares.getAllStations(); + Set stations = fares.getAllStarts(); if (stations != null) stations.forEach(station -> owners.getOwners(station)); } diff --git a/src/main/java/mikeshafter/iciwi/config/Fares.java b/src/main/java/mikeshafter/iciwi/config/Fares.java index 26370eb..83396c1 100644 --- a/src/main/java/mikeshafter/iciwi/config/Fares.java +++ b/src/main/java/mikeshafter/iciwi/config/Fares.java @@ -1,111 +1,114 @@ package mikeshafter.iciwi.config; -import mikeshafter.iciwi.Iciwi; import org.bukkit.configuration.ConfigurationSection; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; - +import java.util.*; +// New fares: .. public class Fares extends CustomConfig { public Fares () {super("fares.yml");} -public Set getAllStations () {return this.get().getKeys(false);} +public Set getAllClasses () {return this.get().getKeys(false);} /** Remove a fare from fares.yml - @param from Starting station - @param to Ending station - @param fareClass Fare class + @param f Starting station + @param e Ending station + @param c Fare class */ -public void unsetFare (String from, String to, String fareClass) { - super.set(from + "." + to + "." + fareClass, null); +public void unsetFare (String f, String e, String c) { + super.set(toPath(c, f, e), null); super.save(); } /** - Remove all fares between two stations from fares.yml - @param from Starting station - @param to Ending station + Remove all fares from a station given a fare class + @param c fare class + @param f starting station */ -public void deleteJourney (String from, String to) { - super.set(from + "." + to, null); +public void deleteStationFromClass (String c, String f) { + super.set(toPath(c, f), null); super.save(); } /** - Remove all fares starting from a specified station from fares.yml - @param station Station + Remove all fares belonging to a fare class. + @param c fare class */ -public void deleteStation (String station) { - super.set(station, null); +public void deleteClass (String c) { + super.set(c, null); super.save(); } /** - Set a fare to fares.yml - @param from Starting station - @param to Ending station - @param fareClass Fare class - @param price Price to set + Remove all fares between two stations from fares.yml + @param f Starting station + @param e Ending station */ -public void setFare (String from, String to, String fareClass, double price) { - super.set(from + "." + to + "." + fareClass, price); +public void deleteJourney (String f, String e) { + for (var c : getAllClasses()) + super.set(toPath(c, f, e), null); super.save(); } /** - Get the corresponding fare if the player pays by card - @param from Starting station - @param to Ending station - @param fareClassNoUnderscore Fare class, without the starting underscore - @return Fare + Remove all fares starting from a specified station from fares.yml + @param f Station */ -public double getCardFare (String from, String to, String fareClassNoUnderscore) { - if (fareClassNoUnderscore.indexOf("_") == 0) fareClassNoUnderscore = fareClassNoUnderscore.substring(1); - final double fare = this.getDouble(from + "." + to + "._" + fareClassNoUnderscore); - if (fare == 0d) return this.getDouble(from + "." + to + "." + fareClassNoUnderscore); - else return fare; +public void deleteStation (String f) { + for (var c : getAllClasses()) + super.set(toPath(c, f), null); + super.save(); } /** - Get the corresponding fare if the player pays by cash - @param from Starting station - @param to Ending station - @param fareClass Fare class - @return Fare + Set a fare to fares.yml + @param f Starting station + @param e Ending station + @param c Fare class + @param price Price to set */ -public double getFare (String from, String to, String fareClass) {return this.getDouble(from + "." + to + "." + fareClass);} +public void setFare (String f, String e, String c, double price) { + super.set(toPath(c, f, e), price); + super.save(); +} /** - Get the corresponding fare using the default train class - @param from Starting station - @param to Ending station + Get the corresponding fare if the player pays by card + @param f Starting station + @param e Ending station + @param cSans_ Fare class, without the starting underscore @return Fare */ -@Deprecated public double getFare (String from, String to) {return getFare(from, to, Iciwi.getPlugin(Iciwi.class).getConfig().getString("default-class"));} +public double getCardFare (String f, String e, String cSans_) { + if (cSans_.indexOf("_") == 0) cSans_ = cSans_.substring(1); + final double fare = this.getDouble(toPath("_" + cSans_, f, e)); + if (fare == 0d) return this.getDouble(toPath(cSans_, f, e)); + else return fare; +} /** - Get all fares starting from a certain station - @param station Starting station + Get the corresponding fare if the player pays by cash + @param f Starting station + @param e Ending station + @param c Fare class @return Fare */ -@Deprecated public Map getFares (String station) {return getFares(station, Iciwi.getPlugin(Iciwi.class).getConfig().getString("default-class"));} +public double getFare (String f, String e, String c) {return this.getDouble(toPath(c, f, e));} /** Get all fares starting from a certain station, with the specified class - @param from Starting station - @param fareClass Fare class - @return Fare + @param f Starting station + @param c Fare class + @return A treemap in the format ENDPOINT, PRICE */ -public Map getFares (String from, String fareClass) { - ConfigurationSection section = this.get().getConfigurationSection(from); +public TreeMap getFares (String f, String c) { + ConfigurationSection section = this.getConfigurationSection(toPath(c, f)); if (section != null) { - var fareMap = new HashMap(); - section.getKeys(false).forEach(to -> fareMap.put(to, this.getDouble(from + "." + to + "." + fareClass))); + var fareMap = new TreeMap(); + + section.getKeys(false).forEach(e -> fareMap.put(e, section.getDouble(e, 0d))); return fareMap; } else return null; @@ -113,39 +116,56 @@ public Map getFares (String from, String fareClass) { /** Get all fares starting from a certain station and ending at another station. - @param from Starting station - @param to Ending station + @param f Starting station + @param e Ending station @return Fare */ -public TreeMap getFaresFromDestinations (String from, String to) { - ConfigurationSection section = this.get().getConfigurationSection(from + "." + to); - if (section != null) { - var fareMap = new TreeMap(); - - section.getKeys(false).forEach(fareClass -> fareMap.put(fareClass, this.getDouble(from + "." + to + "." + fareClass))); - return fareMap; +public TreeMap getFaresFromDestinations (String f, String e) { + var fareMap = new TreeMap(); + for (var c : getAllClasses()) { + if (getFare(f, e, c) > 0d) + fareMap.put(c, getFare(f, e, c)); } - else return null; + return fareMap; } /** Get all fare classes starting from a certain station and ending at another station. - @param from Starting station - @param to Ending station + @param f Starting station + @param e Ending station @return Fare classes */ -public Set getClasses (String from, String to) { - ConfigurationSection section = this.get().getConfigurationSection(from + "." + to); - return section == null ? null : section.getKeys(false); +public TreeSet getClasses (String f, String e) { + var set = new TreeSet(); + for (var c : getAllClasses()) { + if (getFare(f, e, c) > 0d) + set.add(c); + } + return set; } /** Get all end stations starting from a certain station. - @param from Starting station + @param f Starting station @return Fare classes */ -public Set getDestinations (String from) { - ConfigurationSection section = this.get().getConfigurationSection(from); - return section == null ? null : section.getKeys(false); +public TreeSet getDestinations (String f) { + var set = new TreeSet(); + for (var c : getAllClasses()) { + set.addAll(super.getConfigurationSection(toPath(c, f)).getKeys(false)); + } + return set; +} + +/** + Get all starting stations starting from a certain station. + @return All starting stations + */ +public Set getAllStarts () { + var set = new TreeSet(); + for (var c : getAllClasses()) { + set.addAll(super.getConfigurationSection(c).getKeys(false)); + } + return set; } } From 0ef820c902abdaab6168dd3943bcb0135352cb9e Mon Sep 17 00:00:00 2001 From: Mineshafter61 Date: Wed, 7 Aug 2024 14:56:32 +0800 Subject: [PATCH 05/12] add fare class --- .idea/workspace.xml | 26 ++++--------------- src/main/java/mikeshafter/iciwi/Commands.java | 16 +++++++++--- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a9efa54..1f898b4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,26 +4,9 @@