Merge pull request #2 from true-og/codex/fix-config-file-reading-and-remove-writing
Load canonical read-only config.yml and stop automatic writes
This commit was merged in pull request #2.
This commit is contained in:
2 files changed
+39
-3
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package me.eccentric_nz.gamemodeinventories;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
@@ -15,6 +16,8 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
@@ -36,6 +39,7 @@ public class GameModeInventories extends JavaPlugin {
|
||||
private GMIDebug db_level;
|
||||
private String prefix;
|
||||
private HikariDataSource dataSource;
|
||||
private FileConfiguration canonicalConfig;
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
@@ -54,6 +58,11 @@ public class GameModeInventories extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
plugin = this;
|
||||
PluginManager pm = Bukkit.getServer().getPluginManager();
|
||||
loadCanonicalConfig();
|
||||
if (canonicalConfig == null) {
|
||||
pm.disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
Version bukkitversion = getServerVersion(getServer().getVersion());
|
||||
Version minversion = new Version("1.13");
|
||||
// check CraftBukkit version
|
||||
@@ -122,6 +131,26 @@ public class GameModeInventories extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCanonicalConfig() {
|
||||
File configFile = new File(getDataFolder(), "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
getLogger()
|
||||
.log(
|
||||
Level.SEVERE,
|
||||
"config.yml was not found at "
|
||||
+ configFile.getAbsolutePath()
|
||||
+ ". This plugin only reads config.yml and will not generate or modify it.");
|
||||
canonicalConfig = null;
|
||||
return;
|
||||
}
|
||||
canonicalConfig = YamlConfiguration.loadConfiguration(configFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileConfiguration getConfig() {
|
||||
return (canonicalConfig != null) ? canonicalConfig : super.getConfig();
|
||||
}
|
||||
|
||||
private Version getServerVersion(String s) {
|
||||
Pattern pat = Pattern.compile("\\((.+?)\\)", Pattern.DOTALL);
|
||||
Matcher mat = pat.matcher(s);
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
/**
|
||||
* @author eccentric_nz
|
||||
@@ -18,7 +17,6 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||
public class GameModeInventoriesConfig {
|
||||
|
||||
private final GameModeInventories plugin;
|
||||
private final FileConfiguration config;
|
||||
private final File configFile;
|
||||
HashMap<String, String> strOptions = new HashMap<>();
|
||||
HashMap<String, Integer> intOptions = new HashMap<>();
|
||||
@@ -32,7 +30,6 @@ public class GameModeInventoriesConfig {
|
||||
public GameModeInventoriesConfig(GameModeInventories plugin) {
|
||||
this.plugin = plugin;
|
||||
configFile = new File(plugin.getDataFolder(), "config.yml");
|
||||
config = YamlConfiguration.loadConfiguration(configFile);
|
||||
// string
|
||||
strOptions.put("debug_level", "ERROR");
|
||||
strOptions.put("storage.mysql.server", "localhost");
|
||||
@@ -142,6 +139,16 @@ public class GameModeInventoriesConfig {
|
||||
}
|
||||
|
||||
public void checkConfig() {
|
||||
if (!configFile.exists()) {
|
||||
plugin.getLogger()
|
||||
.log(
|
||||
Level.SEVERE,
|
||||
"config.yml was not found at "
|
||||
+ configFile.getAbsolutePath()
|
||||
+ ". This plugin only reads config.yml and will not generate or modify it.");
|
||||
return;
|
||||
}
|
||||
FileConfiguration config = plugin.getConfig();
|
||||
int missing = 0;
|
||||
// string values
|
||||
for (Map.Entry<String, String> entry : strOptions.entrySet()) {
|
||||
|
||||
Reference in new issue
Block a user