mirror of
https://github.com/darksaid98/Builders-Utilities.git
synced 2026-12-02 06:28:53 +00:00
Little cleanup & setup (#4)
* Setup project * Update namespace - Relocate bstats * Some reformatting and documentation * Setup issue templates * chmod+x gradlew * Create pull request template * Apply license * Exclude XMaterial from licensing
This commit is contained in:
1 parent
f763d4c472
commit
9806a083bc
129 files changed
+5068
-1506
No files matched your search
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* ____ _ _ _ _ _ _ _ _ _ _ _
|
||||
* | _ \ (_) | | | | | | | | (_) (_) | (_)
|
||||
* | |_) |_ _ _| | __| | ___ _ __ ___ ______| | | | |_ _| |_| |_ _ ___ ___
|
||||
* | _ <| | | | | |/ _` |/ _ \ '__/ __|______| | | | __| | | | __| |/ _ \/ __|
|
||||
* | |_) | |_| | | | (_| | __/ | \__ \ | |__| | |_| | | | |_| | __/\__ \
|
||||
* |____/ \__,_|_|_|\__,_|\___|_| |___/ \____/ \__|_|_|_|\__|_|\___||___/
|
||||
*
|
||||
* Builder's Utilities is a collection of a lot of tiny features that help with building.
|
||||
* Copyright (C) 2020 Arcaniax
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.arcaniax.buildersutilities.utils;
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class CustomConfig {
|
||||
|
||||
private final JavaPlugin plugin;
|
||||
private final String fileName;
|
||||
private final File configFile;
|
||||
private FileConfiguration config;
|
||||
|
||||
public CustomConfig(JavaPlugin plugin, File folder, String fileName) {
|
||||
this.plugin = plugin;
|
||||
if (!fileName.endsWith(".yml")) {
|
||||
fileName += ".yml";
|
||||
}
|
||||
this.fileName = fileName;
|
||||
configFile = new File(folder, fileName);
|
||||
reloadConfig();
|
||||
}
|
||||
|
||||
public CustomConfig(JavaPlugin plugin, String fileName) {
|
||||
this(plugin, plugin.getDataFolder(), fileName);
|
||||
}
|
||||
|
||||
public void reloadConfig() {
|
||||
if (!configFile.exists()) {
|
||||
plugin.getLogger().info("Attempting to save resource: " + configFile.getName());
|
||||
plugin.saveResource(fileName, true);
|
||||
}
|
||||
config = YamlConfiguration.loadConfiguration(configFile);
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
try {
|
||||
config.save(configFile);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().severe(String.format("Couldn't save '%s', because: '%s'", fileName,
|
||||
e.getMessage()));
|
||||
}
|
||||
reloadConfig();
|
||||
}
|
||||
|
||||
public void set(String path, Object value, boolean save) {
|
||||
config.set(path, value);
|
||||
if (save) {
|
||||
saveConfig();
|
||||
}
|
||||
}
|
||||
|
||||
public void set(String path, Object value) {
|
||||
set(path, value, false);
|
||||
}
|
||||
|
||||
public Set<String> getKeys(boolean deep) {
|
||||
return this.config.getKeys(deep);
|
||||
}
|
||||
|
||||
public ConfigurationSection getConfigurationSection(String path) {
|
||||
return this.config.getConfigurationSection(path);
|
||||
}
|
||||
|
||||
public boolean has(String path) {
|
||||
return config.contains(path);
|
||||
}
|
||||
|
||||
public <T> T get(String path, Class<T> tClass) {
|
||||
return get(path, tClass, null);
|
||||
}
|
||||
|
||||
public <T> T get(String path, Class<T> tClass, T tDefault) {
|
||||
if (tClass.isPrimitive()) {
|
||||
throw new IllegalArgumentException(
|
||||
tClass + " is of a primitive type, please use the objectified version.");
|
||||
}
|
||||
if (!has(path)) {
|
||||
if (tDefault == null) {
|
||||
try {
|
||||
throw new ConfigurationException(
|
||||
"Configuration key doesn't exist and the default is null");
|
||||
} catch (ConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return tDefault;
|
||||
}
|
||||
Object object = config.get(path);
|
||||
if (object == null) {
|
||||
return tDefault;
|
||||
}
|
||||
if (!tClass.isInstance(object)) {
|
||||
throw new IllegalArgumentException(path + " is not of type " + tClass.getSimpleName());
|
||||
}
|
||||
return tClass.cast(object);
|
||||
}
|
||||
|
||||
public List<String> getStringList(String path) {
|
||||
if (!has(path)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return config.getStringList(path);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user