|
/* |
|
* ================================================================== |
|
* Copyright Domnian Dev. (c) 2015. All Rights Reserved |
|
* ================================================================== |
|
* |
|
* MIT License: |
|
* |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
* this software and associated documentation files (the "Software"), to deal in |
|
* the Software without restriction, including without limitation the rights to use, |
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
* Software, and to permit persons to whom the Software is furnished to do so, |
|
* subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in all |
|
* copies or substantial portions of the Software. |
|
*/ |
|
|
|
package com.example.util; |
|
|
|
import org.bukkit.configuration.file.YamlConfiguration; |
|
import org.bukkit.plugin.Plugin; |
|
import org.bukkit.plugin.PluginDescriptionFile; |
|
|
|
import java.io.File; |
|
import java.io.IOException; |
|
import java.lang.IllegalStateException; |
|
import java.util.Map; |
|
import java.util.logging.Logger; |
|
|
|
public class ConfigUtil { |
|
|
|
private static final String VERSION = "1.3.3"; |
|
private static Plugin owner; |
|
|
|
public static void initialize(Plugin owner) { |
|
PluginDescriptionFile pdf = owner.getDescription(); |
|
String plugin = pdf.getName() + " (" + pdf.getVersion() + ")"; |
|
Logger.getGlobal().info("[ConfigUtil] Initialized Configuration Utility v" + VERSION + " for " + plugin); |
|
ConfigUtil.owner = owner; |
|
} |
|
|
|
/** |
|
* Get or Create a Configuration with the specified name and defaults. |
|
* @param configName Name of the Configuration File |
|
* @param defaults A Map of Default Values |
|
* @return A YamlConfiguration object representing the configuration |
|
*/ |
|
public static YamlConfiguration getConfig(String configName, Map<String, Object> defaults) { |
|
if (owner == null) { |
|
throw new IllegalStateException("ConfigUtil is not Initialized - " + |
|
"Please call ConfigUtil.initialize() in your onEnable()"); |
|
} |
|
if (configName.equals("") || configName.equals(null)) { |
|
throw new IllegalArgumentException("Configuration Name Cannot be Blank or Null!"); |
|
} |
|
String folder = owner.getDataFolder().getAbsolutePath() + File.separatorChar; |
|
File file = new File(folder + configName + ".yml"); |
|
file.getParentFile().mkdirs(); |
|
YamlConfiguration config = YamlConfiguration.loadConfiguration(file); |
|
if (!file.exists()) { |
|
fixPermissions(file); // Fix File Permissions |
|
owner.getLogger().info("Config \"" + configName + "\" Not Found - Creating"); |
|
try { |
|
file.createNewFile(); |
|
if (defaults != null) { |
|
config.addDefaults(defaults); |
|
} |
|
config.save(file); |
|
} catch (Exception e) { |
|
Logger.getGlobal().severe("[Configuration] Unable to Create/Load " + configName + ".yml!"); |
|
e.printStackTrace(); |
|
} |
|
} else { |
|
config = YamlConfiguration.loadConfiguration(file); |
|
} |
|
return config; |
|
} |
|
|
|
/** |
|
* Save a Configuration to the File System with the specified name. |
|
* @param config A YamlConfiguration object representing the config to be saved. |
|
* @param configName Name of the Configuration File of which to be saved to. |
|
*/ |
|
public static void saveConfig(YamlConfiguration config, String configName) { |
|
File file = new File(configName + ".yml"); |
|
fixPermissions(file); |
|
try { |
|
config.save(file); |
|
} catch (IOException e) { |
|
Logger.getGlobal().severe("[Configuration] Unable to Save " + configName + ".yml!"); |
|
e.printStackTrace(); |
|
} |
|
} |
|
|
|
private static void fixPermissions(File file) { |
|
if ( !(file.canWrite() || file.canRead()) ) { |
|
file.setWritable(true, false); |
|
file.setReadable(true, false); |
|
Logger.getGlobal().severe("[Configuration] File Permissions on " + file.getName() + " Not Allowing Read/Write, Fixing!"); |
|
} |
|
} |
|
|
|
} |