Skip to content

Instantly share code, notes, and snippets.

@willies952002
Last active October 6, 2020 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willies952002/5f5a5354f5f290585b8e to your computer and use it in GitHub Desktop.
Save willies952002/5f5a5354f5f290585b8e to your computer and use it in GitHub Desktop.
ConfigUtil v1.3.3 - A tool for easily creating and getting custom configuration files for use with the Spigot Plugin API

Version 1.3.3:

  • Fix bug where a folder was being made instread of a file

Version 1.3.2:

  • Update Initilization Message to Reflect Current Verison.

Version 1.3.1:

  • Add missing call to file.setReadable()
  • Change output from stout to Global Logger.

Version 1.3:

  • Added Method for fixing file system permissions.
  • Fixed Imports.

Version 1.2.1:

  • Added Initialization Message.

Version 1.2:

  • Added ConfigUtil.initialize() - Must be called in onEnable();
  • Fixed Bug with config files being created in Server Root Directory.

Version 1.1:

  • Added Documentation.
/*
* ==================================================================
* 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!");
}
}
}
// Usage of ConfigUtil.getConfig();
HashMap defaults = new HashMap<>();
defaults.put("key1", "value1");
defaults.put("key2", "value2");
// ...
defaults.put("keyN", "valueN");
config = ConfigUtil.getConfig("name", defaults);
// Example Plugin Main
public class Main extends JavaPlugin {
private static YamlConfiguration someConfig;
private static
public void onEnable() {
ConfigUtil.initialize(this);
loadConfigs();
}
public void onDisable() {
ConfigUtil.saveConfig(someConfig, "baz");
}
private void loadConfigs() {
HashMap defaults = new HashMap<>();
defaults.put("foo", "bar");
someConfig = ConfigUtil.getConfig("baz", defaults);
}
public static void getSomeConfig() {
return someConfig;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment