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 {@link Config} without defaults. You can use the standard
	 * method of 
	 * adding defaults.
	 * 
	 * @param plugin
	 *            you want to load the {@link Config}.
	 * @param name
	 *            of the YML file.
	 */
	public Config(JavaPlugin plugin, String name) {
		this(plugin, name, true);
	}
	
	private Config(JavaPlugin plugin, String name, boolean b) {
		if(!name.endsWith(".yml")) name += ".yml";
		file = new File(plugin.getDataFolder(), name);
		if(file.exists()) reload();
		else {
			if(b) save();
		}
	}
	
	/**
	 * Creates a new {@link Config} with defaults.
	 * 
	 * @param plugin
	 *            you want to load the {@link Config}.
	 * @param name
	 *            of the YML file.
	 * @param defaults
	 *            a {@link Map List<String, Object>} to be created in the
	 *            {@link Config} as it generates. The {@link String} represents
	 *            the path to where the {@link Object} is located at in the
	 *            {@link Config}. The {@link Object} represents the
	 *            {@link Object} that is set at the corresponding {@link String}
	 *            's location.
	 */
	public Config(JavaPlugin plugin, String name, Map defaults) {
		this(plugin, name, false);
		addDefaults(defaults);
		options().copyDefaults(true);
		save();
		reload();
	}
	
	/**
	 * Saves the {@link Config} to the disk. Values in the {@link Config} which
	 * were changed manually and not loaded via {@link Config#reload() reload}
	 * will be overwritten. Values modified via a plugin after saving are not
	 * saved automatically.
	 */
	public void save() {
		try {
			save(file);
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Loads the {@link Config} from disk. If the {@link Config} was not saved
	 * before loading, values loaded into memory will be overwritten by values
	 * currently in the {@link Config} file. This is the same as 
	 * loading the file.
	 */
	public void reload() {
		try {
			load(file);
		}
		catch(IOException | InvalidConfigurationException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param path
	 *            where the {@link Object} is located at in the {@link Config}.
	 * @return true if the {@link Object} located at the defined path is null,
	 *         false if not.
	 */
	public boolean isNull(String path) {
		return get(path) == null;
	}
	
	/**
	 * @return the {@link File} of the {@link Config} for direct manipulation.
	 */
	public File getFile() {
		return file;
	}
	
	/**
	 * @param path
	 *            where the {@link List List<String>} is located at in the
	 *            {@link Config}.
	 * @return a random {@link String} from a {@link List List<String>}
	 *         found in the {@link Config}.
	 */
	public String getRandomString(String path) {
		List list = getStringList(path);
		return list.get(ThreadLocalRandom.current().nextInt(list.size()));
	}
	
	/**
	 * @param path
	 *            where the {@link List List<Integer>} is located at in
	 *            the {@link Config}.
	 * @return a random {@link Integer} from an {@link List List<Integer>}
	 *         found in the {@link Config}.
	 */
	public Integer getRandomInt(String path) {
		List list = getIntegerList(path);
		return list.get(ThreadLocalRandom.current().nextInt(list.size()));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy