|
19 | 19 | */ |
20 | 20 | package net.pistonmaster.pistonqueue.bukkit; |
21 | 21 |
|
| 22 | +import de.exlll.configlib.NameFormatters; |
| 23 | +import de.exlll.configlib.YamlConfigurations; |
22 | 24 | import lombok.Getter; |
23 | 25 | import net.md_5.bungee.api.ChatColor; |
| 26 | +import net.pistonmaster.pistonqueue.bukkit.config.BukkitConfig; |
24 | 27 | import net.pistonmaster.pistonutils.update.GitHubUpdateChecker; |
25 | 28 | import net.pistonmaster.pistonutils.update.SemanticVersion; |
26 | 29 | import org.bukkit.World; |
| 30 | +import org.bukkit.configuration.file.YamlConfiguration; |
27 | 31 | import org.bukkit.plugin.java.JavaPlugin; |
28 | 32 |
|
| 33 | +import java.io.File; |
29 | 34 | import java.io.IOException; |
30 | | -import java.util.logging.Logger; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.function.Consumer; |
| 40 | +import java.util.function.IntConsumer; |
31 | 41 | import java.util.logging.Level; |
| 42 | +import java.util.logging.Logger; |
32 | 43 |
|
33 | 44 | @Getter |
34 | 45 | public final class PistonQueueBukkit extends JavaPlugin { |
| 46 | + private final Set<String> warnedLegacyPaths = new HashSet<>(); |
| 47 | + private BukkitConfig pluginConfig; |
35 | 48 | private boolean forceLocation; |
36 | 49 |
|
37 | 50 | private String forcedWorldName; |
@@ -72,36 +85,13 @@ public void onEnable() { |
72 | 85 | log.info(ChatColor.BLUE + "PistonQueue V" + getDescription().getVersion()); |
73 | 86 |
|
74 | 87 | log.info(ChatColor.BLUE + "Loading config"); |
75 | | - saveDefaultConfig(); |
76 | | - |
77 | | - forceLocation = getConfig().getBoolean("forceLocation"); |
78 | | - forcedWorldName = getConfig().getString("forcedWorldName"); |
79 | | - forcedX = getConfig().getInt("forcedX"); |
80 | | - forcedY = getConfig().getInt("forcedY"); |
81 | | - forcedZ = getConfig().getInt("forcedZ"); |
82 | | - hidePlayers = getConfig().getBoolean("hidePlayers"); |
83 | | - restrictMovement = getConfig().getBoolean("restrictMovement"); |
84 | | - forceGamemode = getConfig().getBoolean("forceGamemode"); |
85 | | - disableChat = getConfig().getBoolean("disableChat"); |
86 | | - disableCmd = getConfig().getBoolean("disableCmd"); |
87 | | - forcedGamemode = getConfig().getString("forcedGamemode"); |
88 | | - team = getConfig().getBoolean("team"); |
89 | | - teamName = getConfig().getString("teamName"); |
90 | | - |
91 | | - preventExperience = getConfig().getBoolean("preventExperience"); |
92 | | - preventDamage = getConfig().getBoolean("preventDamage"); |
93 | | - preventHunger = getConfig().getBoolean("preventHunger"); |
94 | | - |
95 | | - disableDebug = getConfig().getBoolean("disableDebug"); |
96 | | - |
97 | | - noChunkPackets = getConfig().getBoolean("noChunkPackets"); |
98 | | - noTimePackets = getConfig().getBoolean("noTimePackets"); |
99 | | - noHealthPackets = getConfig().getBoolean("noHealthPackets"); |
100 | | - noAdvancementPackets = getConfig().getBoolean("noAdvancementPackets"); |
101 | | - noExperiencePackets = getConfig().getBoolean("noExperiencePackets"); |
102 | | - showHeadPacket = getConfig().getBoolean("showHeadPacket"); |
103 | | - |
104 | | - playXP = getConfig().getBoolean("playXP"); |
| 88 | + try { |
| 89 | + pluginConfig = loadConfig(); |
| 90 | + } catch (IOException e) { |
| 91 | + throw new IllegalStateException("Failed to load config", e); |
| 92 | + } |
| 93 | + |
| 94 | + applyConfig(pluginConfig); |
105 | 95 |
|
106 | 96 | log.info(ChatColor.BLUE + "Preparing server"); |
107 | 97 | if (hidePlayers) { |
@@ -149,4 +139,134 @@ public void onEnable() { |
149 | 139 | public void onDisable() { |
150 | 140 | this.getServer().getMessenger().unregisterOutgoingPluginChannel(this); |
151 | 141 | } |
| 142 | + |
| 143 | + private BukkitConfig loadConfig() throws IOException { |
| 144 | + Path dataFolder = getDataFolder().toPath(); |
| 145 | + if (Files.notExists(dataFolder)) { |
| 146 | + Files.createDirectories(dataFolder); |
| 147 | + } |
| 148 | + |
| 149 | + Path configFile = dataFolder.resolve("config.yml"); |
| 150 | + BukkitConfig config = YamlConfigurations.update( |
| 151 | + configFile, |
| 152 | + BukkitConfig.class, |
| 153 | + builder -> builder.setNameFormatter(NameFormatters.IDENTITY) |
| 154 | + ); |
| 155 | + |
| 156 | + if (applyLegacyOverrides(configFile.toFile(), config)) { |
| 157 | + YamlConfigurations.save(configFile, BukkitConfig.class, config); |
| 158 | + } |
| 159 | + |
| 160 | + return config; |
| 161 | + } |
| 162 | + |
| 163 | + private void applyConfig(BukkitConfig config) { |
| 164 | + forceLocation = config.location.enabled; |
| 165 | + forcedWorldName = config.location.world; |
| 166 | + forcedX = config.location.coordinates.x; |
| 167 | + forcedY = config.location.coordinates.y; |
| 168 | + forcedZ = config.location.coordinates.z; |
| 169 | + |
| 170 | + hidePlayers = config.visibility.hidePlayers; |
| 171 | + restrictMovement = config.visibility.restrictMovement; |
| 172 | + forceGamemode = config.visibility.forceGamemode.enabled; |
| 173 | + forcedGamemode = config.visibility.forceGamemode.mode; |
| 174 | + team = config.visibility.team.enabled; |
| 175 | + teamName = config.visibility.team.name; |
| 176 | + |
| 177 | + disableChat = config.communication.disableChat; |
| 178 | + disableCmd = config.communication.disableCommands; |
| 179 | + |
| 180 | + playXP = config.audio.playXpSound; |
| 181 | + |
| 182 | + preventExperience = config.protections.preventExperience; |
| 183 | + preventDamage = config.protections.preventDamage; |
| 184 | + preventHunger = config.protections.preventHunger; |
| 185 | + |
| 186 | + disableDebug = config.protocolLib.disableDebug; |
| 187 | + noChunkPackets = config.protocolLib.suppressPackets.chunk; |
| 188 | + noTimePackets = config.protocolLib.suppressPackets.time; |
| 189 | + noHealthPackets = config.protocolLib.suppressPackets.health; |
| 190 | + noAdvancementPackets = config.protocolLib.suppressPackets.advancement; |
| 191 | + noExperiencePackets = config.protocolLib.suppressPackets.experience; |
| 192 | + showHeadPacket = config.protocolLib.showFullHead; |
| 193 | + } |
| 194 | + |
| 195 | + private boolean applyLegacyOverrides(File file, BukkitConfig config) { |
| 196 | + if (file == null || !file.exists()) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + YamlConfiguration legacy = YamlConfiguration.loadConfiguration(file); |
| 201 | + boolean updated = false; |
| 202 | + |
| 203 | + updated |= copyBoolean(legacy, "forceLocation", "location.enabled", value -> config.location.enabled = value); |
| 204 | + updated |= copyString(legacy, "forcedWorldName", "location.world", value -> config.location.world = value); |
| 205 | + updated |= copyInt(legacy, "forcedX", "location.coordinates.x", value -> config.location.coordinates.x = value); |
| 206 | + updated |= copyInt(legacy, "forcedY", "location.coordinates.y", value -> config.location.coordinates.y = value); |
| 207 | + updated |= copyInt(legacy, "forcedZ", "location.coordinates.z", value -> config.location.coordinates.z = value); |
| 208 | + |
| 209 | + updated |= copyBoolean(legacy, "hidePlayers", "visibility.hidePlayers", value -> config.visibility.hidePlayers = value); |
| 210 | + updated |= copyBoolean(legacy, "restrictMovement", "visibility.restrictMovement", value -> config.visibility.restrictMovement = value); |
| 211 | + updated |= copyBoolean(legacy, "forceGamemode", "visibility.forceGamemode.enabled", value -> config.visibility.forceGamemode.enabled = value); |
| 212 | + updated |= copyString(legacy, "forcedGamemode", "visibility.forceGamemode.mode", value -> config.visibility.forceGamemode.mode = value); |
| 213 | + updated |= copyBoolean(legacy, "team", "visibility.team.enabled", value -> config.visibility.team.enabled = value); |
| 214 | + updated |= copyString(legacy, "teamName", "visibility.team.name", value -> config.visibility.team.name = value); |
| 215 | + |
| 216 | + updated |= copyBoolean(legacy, "disableChat", "communication.disableChat", value -> config.communication.disableChat = value); |
| 217 | + updated |= copyBoolean(legacy, "disableCmd", "communication.disableCommands", value -> config.communication.disableCommands = value); |
| 218 | + |
| 219 | + updated |= copyBoolean(legacy, "playXP", "audio.playXpSound", value -> config.audio.playXpSound = value); |
| 220 | + |
| 221 | + updated |= copyBoolean(legacy, "preventExperience", "protections.preventExperience", value -> config.protections.preventExperience = value); |
| 222 | + updated |= copyBoolean(legacy, "preventDamage", "protections.preventDamage", value -> config.protections.preventDamage = value); |
| 223 | + updated |= copyBoolean(legacy, "preventHunger", "protections.preventHunger", value -> config.protections.preventHunger = value); |
| 224 | + |
| 225 | + updated |= copyBoolean(legacy, "disableDebug", "protocolLib.disableDebug", value -> config.protocolLib.disableDebug = value); |
| 226 | + updated |= copyBoolean(legacy, "noChunkPackets", "protocolLib.suppressPackets.chunk", value -> config.protocolLib.suppressPackets.chunk = value); |
| 227 | + updated |= copyBoolean(legacy, "noTimePackets", "protocolLib.suppressPackets.time", value -> config.protocolLib.suppressPackets.time = value); |
| 228 | + updated |= copyBoolean(legacy, "noHealthPackets", "protocolLib.suppressPackets.health", value -> config.protocolLib.suppressPackets.health = value); |
| 229 | + updated |= copyBoolean(legacy, "noAdvancementPackets", "protocolLib.suppressPackets.advancement", value -> config.protocolLib.suppressPackets.advancement = value); |
| 230 | + updated |= copyBoolean(legacy, "noExperiencePackets", "protocolLib.suppressPackets.experience", value -> config.protocolLib.suppressPackets.experience = value); |
| 231 | + updated |= copyBoolean(legacy, "showHeadPacket", "protocolLib.showFullHead", value -> config.protocolLib.showFullHead = value); |
| 232 | + |
| 233 | + return updated; |
| 234 | + } |
| 235 | + |
| 236 | + private boolean copyBoolean(YamlConfiguration legacy, String legacyPath, String newPath, Consumer<Boolean> setter) { |
| 237 | + if (!legacy.contains(legacyPath)) { |
| 238 | + return false; |
| 239 | + } |
| 240 | + |
| 241 | + setter.accept(legacy.getBoolean(legacyPath)); |
| 242 | + logLegacyUsage(newPath, legacyPath); |
| 243 | + return true; |
| 244 | + } |
| 245 | + |
| 246 | + private boolean copyInt(YamlConfiguration legacy, String legacyPath, String newPath, IntConsumer setter) { |
| 247 | + if (!legacy.contains(legacyPath)) { |
| 248 | + return false; |
| 249 | + } |
| 250 | + |
| 251 | + setter.accept(legacy.getInt(legacyPath)); |
| 252 | + logLegacyUsage(newPath, legacyPath); |
| 253 | + return true; |
| 254 | + } |
| 255 | + |
| 256 | + private boolean copyString(YamlConfiguration legacy, String legacyPath, String newPath, Consumer<String> setter) { |
| 257 | + if (!legacy.contains(legacyPath)) { |
| 258 | + return false; |
| 259 | + } |
| 260 | + |
| 261 | + setter.accept(legacy.getString(legacyPath)); |
| 262 | + logLegacyUsage(newPath, legacyPath); |
| 263 | + return true; |
| 264 | + } |
| 265 | + |
| 266 | + private void logLegacyUsage(String newPath, String legacyPath) { |
| 267 | + if (warnedLegacyPaths.add(legacyPath)) { |
| 268 | + getLogger() |
| 269 | + .warning(String.format("Configuration option '%s' has moved to '%s'. Please update your config.", legacyPath, newPath)); |
| 270 | + } |
| 271 | + } |
152 | 272 | } |
0 commit comments