Added NPC location config

This commit is contained in:
Teriuihi 2023-09-21 00:18:35 +02:00
parent 140a0f0e97
commit e50bf5a4b2

View File

@ -0,0 +1,60 @@
package com.alttd.fishingevent.config;
import com.alttd.fishingevent.FishingEvent;
import com.alttd.fishingevent.util.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import java.util.*;
public class NPCLocationConfig extends AbstractConfig{
static NPCLocationConfig config;
private final Logger logger;
NPCLocationConfig(FishingEvent fishingEvent, Logger logger) {
super(fishingEvent, "npc-location.yml", logger);
this.logger = logger;
}
public static void reload(FishingEvent fishingEvent, Logger logger) {
config = new NPCLocationConfig(fishingEvent, logger);
config.readConfig(NPCLocationConfig.class, null);
}
public static class NPC_LOCATION {
private static final String prefix = "npc.";
public static Map<String, Location> ALL = new HashMap<>();
@SuppressWarnings("unused")
private static void load() {
ALL.clear();
ConfigurationSection configurationSection = config.getConfigurationSection(prefix);
if (configurationSection == null)
return;
Set<String> npcKeys = configurationSection.getKeys(false);
for (String key : npcKeys) {
Optional<Location> location = getLocation(prefix + key + ".location.");
if (location.isEmpty())
continue;
ALL.put(key, location.get());
}
}
private static Optional<Location> getLocation(String prefix) {
World world = Bukkit.getWorld(config.getString(prefix, "world", "world"));
if (world == null) {
config.logger.warning("Unable to load world for [%]", prefix);
return Optional.empty();
}
double x = config.getDouble(prefix, "x", 0);
double y = config.getDouble(prefix, "y", 0);
double z = config.getDouble(prefix, "z", 0);
float pitch = (float) config.getDouble(prefix, "pitch", 0);
float yaw = (float) config.getDouble(prefix, "yaw", 0);
return Optional.of(new Location(world, x, y, z, pitch, yaw));
}
}
}