Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/main/java/net/tfminecraft/geminfusion/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,9 @@ public Gemstone getGemFromConfig(FileConfiguration config, String key) {
gem.setSocketColour(config.getConfigurationSection("gems."+key).getString("socket_colour"));
gem.setSocketNameColour(ChatColor.valueOf(config.getConfigurationSection("gems."+key).getString("socket_name_colour").toUpperCase()));
gem.setMMOItem(config.getConfigurationSection("gems."+key).getString("gem"));
Set<String> set = config.getConfigurationSection("gems."+key+".rarities").getKeys(false);

List<String> list = new ArrayList<String>(set);
for(String id : list) {
GemStat stat = GemStat.parse(key, id, config.getConfigurationSection("gems."+key+".rarities."+id));
if (stat != null) {
gem.addStat(stat);
}
for (GemStat stat : GemStat.parseGem(key, config.getConfigurationSection("gems."+key),
GemStat.ownSection(config, "stat-bands"))) {
gem.addStat(stat);
}
return gem;
}
Expand Down
71 changes: 70 additions & 1 deletion src/main/java/net/tfminecraft/geminfusion/GemStat.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package net.tfminecraft.geminfusion;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.configuration.ConfigurationSection;

public class GemStat {
/** {@code min-max}, or one number for a fixed stat. */
private static final Pattern RANGE = Pattern.compile("(\\d+(?:\\.\\d+)?)(?:\\s*-\\s*(\\d+(?:\\.\\d+)?))?");

private final String id;
private final String statId;
private final double min;
Expand Down Expand Up @@ -35,7 +41,70 @@ public double getMax() {
}

/**
* New shape: {@code stat} / {@code min} / {@code max}.
* Reads a gem's stat for each rarity. Compact shape: one {@code stat} for the gem and {@code bands}, either the
* name of a set under the top-level {@code stat-bands} or a section of {@code <rarity>: min-max}. Older configs
* list {@code rarities.<rarity>} blocks instead (see {@link #parse}), which still load.
* Only keys written in this config count: Bukkit falls back to the bundled config.yml for missing keys, which
* would hand an older server config the bundled bands (or empty sections made from them).
*/
public static List<GemStat> parseGem(String gemId, ConfigurationSection gem, ConfigurationSection statBands) {
List<GemStat> stats = new ArrayList<>();
Object bandsValue = gem.get("bands", null);
if (bandsValue != null) {
if (gem.get("rarities", null) != null) {
warn("Gem '" + gemId + "' has both bands and rarities; using bands.");
}
String statId = gem.getString("stat", null);
if (statId == null || statId.isBlank()) {
warn("Gem '" + gemId + "' has bands but no stat, skipping its stats.");
return stats;
}
ConfigurationSection bands = bandsValue instanceof ConfigurationSection own ? own
: ownSection(statBands, String.valueOf(bandsValue));
if (bands == null) {
warn("Gem '" + gemId + "' uses unknown stat-bands set '" + bandsValue + "', skipping its stats.");
return stats;
}
for (String rarityId : bands.getKeys(false)) {
GemStat stat = parseRange(gemId, rarityId, statId, bands.getString(rarityId, null));
if (stat != null) {
stats.add(stat);
}
}
return stats;
}
ConfigurationSection rarities = ownSection(gem, "rarities");
if (rarities == null) {
warn("Gem '" + gemId + "' has neither bands nor rarities, skipping its stats.");
return stats;
}
for (String rarityId : rarities.getKeys(false)) {
GemStat stat = parse(gemId, rarityId, rarities.getConfigurationSection(rarityId));
if (stat != null) {
stats.add(stat);
}
}
return stats;
}

/** The section written at this key of this config, ignoring the bundled defaults; null when there is none. */
static ConfigurationSection ownSection(ConfigurationSection parent, String key) {
return parent != null && parent.get(key, null) instanceof ConfigurationSection own ? own : null;
}

private static GemStat parseRange(String gemId, String rarityId, String statId, String text) {
Matcher range = RANGE.matcher(String.valueOf(text).trim());
if (!range.matches()) {
warn("Gem '" + gemId + "' " + rarityId + " band '" + text + "' is not min-max, skipping.");
return null;
}
double min = Double.parseDouble(range.group(1));
double max = range.group(2) == null ? min : Double.parseDouble(range.group(2));
return new GemStat(rarityId, statId, min, max);
}

/**
* Old per-rarity shape: {@code stat} / {@code min} / {@code max}.
* Legacy: first entry of {@code stats: - type(min-max)}.
* Returns null when the block cannot be used.
*/
Expand Down
Loading
Loading