All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.warren1001.configapi.Config Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
package io.github.warren1001.configapi;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * An API system for YamlConfiguration for SpigotMC.
 * 
 * @author Warren1001
 */
public class Config extends YamlConfiguration {
	
	private File file;
	
	/**
	 * Creates a new Config without defaults. You can use the standard method of adding defaults.
	 * 
	 * @param plugin represents the plugin you want to load the Config.
	 * @param name represents the name of the yml file.
	 */
	public Config(JavaPlugin plugin, String name) {
		this(plugin, name, false);
	}
	
	private Config(JavaPlugin plugin, String name, boolean b) {
		if(!name.endsWith(".yml")) name += ".yml";
		file = new File(plugin.getDataFolder(), name);
		if(!b) {
			if(!file.exists()) save();
			load();
		}
	}
	
	/**
	 * Creates a new Config with defaults.
	 * 
	 * @param plugin
	 *            respresents the plugin you want to load the Config.
	 * @param name
	 *            represents the name of the yml file.
	 * @param defaults
	 *            represents a Map of String & Object of the defaults to be
	 *            generated in the Config. The String represents the path to
	 *            where the Object is located at in the Config. The Object
	 *            represents the Object that is set at the corresponding
	 *            String's location.
	 */
	public Config(JavaPlugin plugin, String name, Map defaults) {
		this(plugin, name, true);
		addDefaults(defaults);
		options().copyDefaults(true);
		reload();
	}
	
	/**
	 * Saves the Config to disk. Values modified after saving are not saved
	 * automatically.
	 */
	public void save() {
		try {
			save(file);
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Loads the Config from disk. If the Config was not saved before loading,
	 * values loaded into memory will be overwritten by values currently in the
	 * Config.
	 */
	public void load() {
		try {
			load(file);
		}
		catch(IOException | InvalidConfigurationException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Reloads (saves then loads) the Config. If any fields in the Config have
	 * been modified, they will be overwritten when the Config is saved. If you
	 * do not want this, simply use {@link Config#load()}.
	 */
	public void reload() {
		save();
		load();
	}
	
	/**
	 * Checks if the Object located at the defined path is null.
	 * 
	 * @param path
	 *            represents the path to where the Object is located at in the
	 *            Config.
	 * @return
	 */
	public boolean isNull(String path) {
		return get(path) == null;
	}
	
	/**
	 * Returns the File of the Config for direct manipulation.
	 * 
	 * @return
	 */
	public File getFile() {
		return file;
	}
	
	/**
	 * Returns a random String from a StringList found in the Config.
	 * 
	 * @param path
	 *            represents the path to where the StringList is located at in
	 *            the Config.
	 * @return
	 */
	public String getRandomString(String path) {
		List list = getStringList(path);
		return list.get(ThreadLocalRandom.current().nextInt(list.size()));
	}
	
	/**
	 * Returns a random Integer from an IntegerList found in the Config.
	 * 
	 * @param path
	 *            represents the path to where the IntegerList is located at in
	 *            the Config.
	 * @return
	 */
	public Integer getRandomInt(String path) {
		List list = getIntegerList(path);
		return list.get(ThreadLocalRandom.current().nextInt(list.size()));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy