|
| 1 | +package com.flyaway.flycontroller; |
| 2 | + |
| 3 | +import org.bukkit.configuration.file.FileConfiguration; |
| 4 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class ConfigManager { |
| 13 | + private final FlyPlugin plugin; |
| 14 | + private File configFile; |
| 15 | + private FileConfiguration config; |
| 16 | + |
| 17 | + public ConfigManager(FlyPlugin plugin) { |
| 18 | + this.plugin = plugin; |
| 19 | + this.configFile = new File(plugin.getDataFolder(), "config.yml"); |
| 20 | + this.config = YamlConfiguration.loadConfiguration(configFile); |
| 21 | + } |
| 22 | + |
| 23 | + public void loadConfig() { |
| 24 | + if (!configFile.exists()) { |
| 25 | + plugin.saveResource("config.yml", false); |
| 26 | + config = YamlConfiguration.loadConfiguration(configFile); |
| 27 | + } |
| 28 | + |
| 29 | + // Создаем папку если не существует |
| 30 | + if (!plugin.getDataFolder().exists()) { |
| 31 | + plugin.getDataFolder().mkdirs(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public Map<Integer, FlightTier> getFlightTiers() { |
| 36 | + Map<Integer, FlightTier> tiers = new HashMap<>(); |
| 37 | + |
| 38 | + if (config.contains("flight-tiers")) { |
| 39 | + for (String key : config.getConfigurationSection("flight-tiers").getKeys(false)) { |
| 40 | + try { |
| 41 | + int level = Integer.parseInt(key); |
| 42 | + double cost = config.getDouble("flight-tiers." + key + ".cost"); |
| 43 | + int duration = config.getInt("flight-tiers." + key + ".duration"); |
| 44 | + |
| 45 | + tiers.put(level, new FlightTier(level, cost, duration)); |
| 46 | + } catch (NumberFormatException e) { |
| 47 | + plugin.getLogger().warning("Неверный формат уровня полёта: " + key); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Если в конфиге нет уровней, используем значения по умолчанию |
| 53 | + if (tiers.isEmpty()) { |
| 54 | + tiers.put(1, new FlightTier(1, 50000, 120)); |
| 55 | + tiers.put(2, new FlightTier(2, 100000, 300)); |
| 56 | + tiers.put(3, new FlightTier(3, 200000, 600)); |
| 57 | + plugin.getLogger().warning("Используются уровни полёта по умолчанию"); |
| 58 | + } |
| 59 | + |
| 60 | + return tiers; |
| 61 | + } |
| 62 | + |
| 63 | + public Map<Integer, Float> getFlySpeeds() { |
| 64 | + Map<Integer, Float> speeds = new HashMap<>(); |
| 65 | + |
| 66 | + if (config.contains("fly-speeds")) { |
| 67 | + for (String key : config.getConfigurationSection("fly-speeds").getKeys(false)) { |
| 68 | + try { |
| 69 | + int level = Integer.parseInt(key); |
| 70 | + float speed = (float) config.getDouble("fly-speeds." + key); |
| 71 | + speeds.put(level, speed); |
| 72 | + } catch (NumberFormatException e) { |
| 73 | + plugin.getLogger().warning("Неверный формат скорости полёта: " + key); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Значения по умолчанию |
| 79 | + if (speeds.isEmpty()) { |
| 80 | + speeds.put(1, 0.1f); |
| 81 | + speeds.put(2, 0.2f); |
| 82 | + speeds.put(3, 0.4f); |
| 83 | + } |
| 84 | + |
| 85 | + return speeds; |
| 86 | + } |
| 87 | + |
| 88 | + public List<String> getWorlds() { |
| 89 | + return config.getStringList("worlds"); |
| 90 | + } |
| 91 | + |
| 92 | + public String getCurrency() { |
| 93 | + return config.getString("currency", "money"); // money по умолчанию |
| 94 | + } |
| 95 | + |
| 96 | + public long getCooldownTime() { |
| 97 | + return config.getLong("cooldown", 600000); // 10 минут по умолчанию |
| 98 | + } |
| 99 | + |
| 100 | + public FileConfiguration getConfig() { |
| 101 | + return config; |
| 102 | + } |
| 103 | + |
| 104 | + public void saveConfig() { |
| 105 | + try { |
| 106 | + config.save(configFile); |
| 107 | + } catch (IOException e) { |
| 108 | + plugin.getLogger().warning("Не удалось сохранить config.yml: " + e.getMessage()); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments