125 lines
5.0 KiB
Java
125 lines
5.0 KiB
Java
package com.alttd.fishingevent.config;
|
|
|
|
import com.alttd.fishingevent.FishingEvent;
|
|
import com.alttd.fishingevent.fish.Fish;
|
|
import com.alttd.fishingevent.objects.FishType;
|
|
import com.alttd.fishingevent.objects.Rarity;
|
|
import com.alttd.fishingevent.util.FishConfigHelper;
|
|
import com.alttd.fishingevent.util.Logger;
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import java.util.*;
|
|
|
|
public class Fishes extends AbstractConfig {
|
|
|
|
static Fishes config;
|
|
private final Logger logger;
|
|
private final FishingEvent fishingEvent;
|
|
|
|
Fishes(FishingEvent fishingEvent, Logger logger) {
|
|
super(fishingEvent, "fishes.yml", logger);
|
|
this.fishingEvent = fishingEvent;
|
|
this.logger = logger;
|
|
}
|
|
|
|
public static void reload(FishingEvent fishingEvent, Logger logger) {
|
|
logger.debug("Reloading config [fishes.yml]");
|
|
config = new Fishes(fishingEvent, logger);
|
|
config.readConfig(Fishes.class, null);
|
|
}
|
|
|
|
public static class FORMAT {
|
|
private static final String prefix = "format.";
|
|
|
|
public static String DISPLAY_NAME = "<gold><fish_name></gold>";
|
|
public static List<String> LORE = List.of(
|
|
"<gray>Length: <gold><length></gold>cm</gray>",
|
|
"<gray>Caught by: <player></gray>"
|
|
);
|
|
|
|
@SuppressWarnings("unused")
|
|
private static void load() {
|
|
DISPLAY_NAME = config.getString(prefix, "display-name", DISPLAY_NAME);
|
|
LORE = config.getStringList(prefix, "lore", LORE);
|
|
}
|
|
}
|
|
|
|
public static class FISH {
|
|
|
|
public static List<Fish> WATER_FISH = new ArrayList<>();
|
|
public static List<Fish> LAVA_FISH = new ArrayList<>();
|
|
public static HashMap<Rarity, List<Fish>> RARITY_WATER_FISH_MAP = new HashMap<>();
|
|
public static HashMap<Rarity, List<Fish>> RARITY_LAVA_FISH_MAP = new HashMap<>();
|
|
|
|
@SuppressWarnings("unused")
|
|
private static void load() {
|
|
WATER_FISH.clear();
|
|
RARITY_WATER_FISH_MAP.clear();
|
|
ConfigurationSection configurationSection = config.getConfigurationSection("water-fish");
|
|
if (configurationSection == null) {
|
|
return;
|
|
}
|
|
for (String key : configurationSection.getKeys(false)) {
|
|
ConfigurationSection fishSection = configurationSection.getConfigurationSection(key);
|
|
if (fishSection == null) {
|
|
config.logger.warning("Invalid water fish section: " + configurationSection.getCurrentPath() + "." + key);
|
|
continue;
|
|
}
|
|
loadFish(fishSection);
|
|
}
|
|
for (Fish fish : WATER_FISH) {
|
|
config.logger.debug("Adding water fish [%] with rarity [%]", fish.normalFishName(), fish.getRarity().toString());
|
|
RARITY_WATER_FISH_MAP.computeIfAbsent(fish.getRarity(), list -> new ArrayList<>()).add(fish);
|
|
}
|
|
|
|
for (Fish fish : LAVA_FISH) {
|
|
config.logger.debug("Adding lava fish [%] with rarity [%]", fish.normalFishName(), fish.getRarity().toString());
|
|
RARITY_LAVA_FISH_MAP.computeIfAbsent(fish.getRarity(), list -> new ArrayList<>()).add(fish);
|
|
}
|
|
}
|
|
|
|
private static void loadFish(ConfigurationSection fishSection) {
|
|
Optional<Rarity> rarity = FishConfigHelper.getRarity(config.logger, fishSection);
|
|
if (rarity.isEmpty())
|
|
return;
|
|
|
|
Optional<ItemStack> optionalItemStack = FishConfigHelper.loadFishItem(config.logger, fishSection);
|
|
if (optionalItemStack.isEmpty()) {
|
|
config.logger.warning("Invalid water fish item for " + fishSection.getCurrentPath());
|
|
return;
|
|
}
|
|
|
|
Set<FishType> fishTypes = getFishTypes(fishSection);
|
|
|
|
for (FishType fishType : fishTypes) {
|
|
Fish fish = new Fish(
|
|
config.fishingEvent,
|
|
config.logger,
|
|
(float) fishSection.getDouble("min-length", 0),
|
|
(float) fishSection.getDouble("max-length", 0),
|
|
fishSection.getString("fish-name", "default name"),
|
|
rarity.get(),
|
|
optionalItemStack.get());
|
|
switch (fishType) {
|
|
case WATER -> WATER_FISH.add(fish);
|
|
case LAVA -> LAVA_FISH.add(fish);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static Set<FishType> getFishTypes(ConfigurationSection fishSection) {
|
|
Set<FishType> fishTypes = new HashSet<>();
|
|
List<String> typeList = fishSection.getStringList("fish-type");
|
|
for (String fishType : typeList) {
|
|
try {
|
|
fishTypes.add(FishType.valueOf(fishType));
|
|
} catch (IllegalArgumentException ignored) {
|
|
config.logger.warning("Invalid fish type: %", fishType);
|
|
}
|
|
}
|
|
return fishTypes;
|
|
}
|
|
}
|
|
}
|