New config

This commit is contained in:
CmdrKittens
2020-04-09 00:18:17 -04:00
parent 97d977b996
commit 2286fd7656
113 changed files with 19303 additions and 100 deletions
@@ -0,0 +1,47 @@
/*
* PlayerVaultsX
* Copyright (C) 2013 Trent Hensler, Laxwashere, CmdrKittens
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.config;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.config.file.Config;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.io.IOException;
import java.util.logging.Level;
public class ConfigManager {
private final PlayerVaults plugin;
private final Config config = new Config();
public ConfigManager(@NonNull PlayerVaults plugin) {
this.plugin = plugin;
}
public void loadConfig() {
try {
Loader.loadAndSave("main", this.config);
} catch (IOException | IllegalAccessException e) {
this.plugin.getLogger().log(Level.SEVERE, "Could not load config. Using all default values until resolved.", e);
}
}
public @NonNull Config getConf() {
return this.config;
}
}
@@ -0,0 +1,161 @@
/*
* PlayerVaultsX
* Copyright (C) 2013 Trent Hensler, Laxwashere, CmdrKittens
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.config;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.config.annotation.WipeOnReload;
import com.drtshock.playervaults.lib.com.typesafe.config.Config;
import com.drtshock.playervaults.lib.com.typesafe.config.ConfigFactory;
import com.drtshock.playervaults.lib.com.typesafe.config.ConfigRenderOptions;
import com.drtshock.playervaults.lib.com.typesafe.config.ConfigValue;
import com.drtshock.playervaults.lib.com.typesafe.config.ConfigValueFactory;
import com.drtshock.playervaults.lib.com.typesafe.config.ConfigValueType;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import com.drtshock.playervaults.config.annotation.Comment;
import com.drtshock.playervaults.config.annotation.ConfigName;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Loader {
public static void loadAndSave(@NonNull String fileName, @NonNull Object config) throws IOException, IllegalAccessException {
File file = Loader.getFile(fileName);
Loader.loadAndSave(file, Loader.getConf(file), config);
}
public static @NonNull File getFile(@NonNull String file) {
Path configFolder = PlayerVaults.getInstance().getDataFolder().toPath();
if (!configFolder.toFile().exists()) {
configFolder.toFile().mkdir();
}
Path path = configFolder.resolve(file + ".conf");
return path.toFile();
}
public static @NonNull Config getConf(@NonNull File file) {
return ConfigFactory.parseFile(file);
}
public static void loadAndSave(@NonNull File file, @NonNull Config config, @NonNull Object configObject) throws IOException, IllegalAccessException {
ConfigValue value = Loader.loadNode(config, configObject);
String s = value.render(ConfigRenderOptions.defaults().setOriginComments(false).setComments(true).setJson(false));
Files.write(file.toPath(), s.getBytes(StandardCharsets.UTF_8));
}
public static ConfigValue load(Config config, Object configObject) throws IOException, IllegalAccessException {
return Loader.loadNode(config, configObject);
}
private static Set<Class<?>> types = new HashSet<>();
static {
Loader.types.add(Boolean.TYPE);
Loader.types.add(Byte.TYPE);
Loader.types.add(Character.TYPE);
Loader.types.add(Double.TYPE);
Loader.types.add(Float.TYPE);
Loader.types.add(Integer.TYPE);
Loader.types.add(Long.TYPE);
Loader.types.add(Short.TYPE);
Loader.types.add(List.class);
Loader.types.add(Map.class);
Loader.types.add(Set.class);
Loader.types.add(String.class);
}
private static @NonNull ConfigValue loadNode(@NonNull Config current, @NonNull Object object) throws IllegalAccessException {
Map<String, ConfigValue> map = new HashMap<>();
for (Field field : Loader.getFields(object.getClass())) {
if (field.isSynthetic()) {
continue;
}
if ((field.getModifiers() & Modifier.TRANSIENT) != 0) {
if (field.getAnnotation(WipeOnReload.class) != null) {
field.setAccessible(true);
field.set(object, null);
}
continue;
}
field.setAccessible(true);
ConfigName configName = field.getAnnotation(ConfigName.class);
Comment comment = field.getAnnotation(Comment.class);
String confName = configName == null || configName.value().isEmpty() ? field.getName() : configName.value();
ConfigValue curValue = Loader.getOrNull(current, confName);
boolean needsValue = curValue == null;
ConfigValue newValue;
Object defaultValue = field.get(object);
if (Loader.types.contains(field.getType())) {
if (needsValue) {
newValue = ConfigValueFactory.fromAnyRef(defaultValue);
} else {
try {
if (Set.class.isAssignableFrom(field.getType()) && curValue.valueType() == ConfigValueType.LIST) {
field.set(object, new HashSet<Object>((List<?>) curValue.unwrapped()));
} else {
field.set(object, curValue.unwrapped());
}
newValue = curValue;
} catch (IllegalArgumentException ex) {
System.out.println("Found incorrect type for " + confName + ": Expected " + field.getType() + ", found " + curValue.unwrapped().getClass());
field.set(object, defaultValue);
newValue = ConfigValueFactory.fromAnyRef(defaultValue);
}
}
} else {
newValue = Loader.loadNode(current, defaultValue);
}
if (comment != null) {
newValue = newValue.withOrigin(newValue.origin().withComments(Arrays.asList(comment.value().split("\n"))));
}
map.put(confName, newValue);
}
return ConfigValueFactory.fromMap(map);
}
private static @Nullable ConfigValue getOrNull(@NonNull Config config, @NonNull String path) {
return config.hasPath(path) ? config.getValue(path) : null;
}
private static @NonNull List<Field> getFields(Class<?> clazz) {
return Loader.getFields(new ArrayList<>(), clazz);
}
private static @NonNull List<Field> getFields(@NonNull List<Field> fields, @NonNull Class<?> clazz) {
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
if (clazz.getSuperclass() != null) {
Loader.getFields(fields, clazz.getSuperclass());
}
return fields;
}
}
@@ -0,0 +1,31 @@
/*
* PlayerVaultsX
* Copyright (C) 2013 Trent Hensler, Laxwashere, CmdrKittens
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.config.annotation;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Comment {
@NonNull String value();
}
@@ -0,0 +1,31 @@
/*
* PlayerVaultsX
* Copyright (C) 2013 Trent Hensler, Laxwashere, CmdrKittens
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.config.annotation;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ConfigName {
@NonNull String value();
}
@@ -0,0 +1,28 @@
/*
* PlayerVaultsX
* Copyright (C) 2013 Trent Hensler, Laxwashere, CmdrKittens
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.config.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface WipeOnReload {
}
@@ -0,0 +1,209 @@
/*
* PlayerVaultsX
* Copyright (C) 2013 Trent Hensler, Laxwashere, CmdrKittens
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.config.file;
import com.drtshock.playervaults.config.annotation.Comment;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@SuppressWarnings({"FieldCanBeLocal", "InnerClassMayBeStatic", "unused"})
public class Config {
public class Block {
private boolean enabled = true;
@Comment("Material list for blocked items (does not support ID's), only effective if the feature is enabled.\n" +
" If you don't know material names: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html")
private List<String> list = new ArrayList<String>() {
{
this.add("PUMPKIN");
this.add("DIAMOND_BLOCK");
}
};
public boolean isEnabled() {
return this.enabled;
}
public List<String> getList() {
if (this.list == null) {
this.list = new ArrayList<>();
}
return Collections.unmodifiableList(list);
}
}
public class Economy {
@Comment("Set me to true to enable economy features!")
private boolean enabled = false;
private double feeToCreate = 100;
private double feeToOpen = 10;
private double refundOnDelete = 50;
public boolean isEnabled() {
return this.enabled;
}
public double getFeeToCreate() {
return this.feeToCreate;
}
public double getFeeToOpen() {
return this.feeToOpen;
}
public double getRefundOnDelete() {
return this.refundOnDelete;
}
}
public class PurgePlanet {
private boolean enabled = false;
@Comment("Time, in days, since last edit")
private int daysSinceLastEdit = 30;
public boolean isEnabled() {
return this.enabled;
}
public int getDaysSinceLastEdit() {
return this.daysSinceLastEdit;
}
}
public class Storage {
public class FlatFile {
@Comment("Backups\n" +
" Enabling this will create backups of vaults automagically.")
private boolean backups = true;
public boolean isBackups() {
return this.backups;
}
}
private FlatFile flatFile = new FlatFile();
private String storageType = "flatfile";
public FlatFile getFlatFile() {
return this.flatFile;
}
public String getStorageType() {
return this.storageType;
}
}
@Comment("PlayerVaults\n" +
"Created by: https://github.com/drtshock/PlayerVaults/graphs/contributors/\n" +
"Resource page: https://www.spigotmc.org/resources/51204/\n" +
"Discord server: https://discordapp.com/invite/JZcWDEt/\n" +
"Made with love <3")
private boolean aPleasantHello=true;
@Comment("Debug Mode\n" +
" This will print everything the plugin is doing to console.\n" +
" You should only enable this if you're working with a contributor to fix something.")
private boolean debug = false;
@Comment("Language\n" +
" This determines which language file the plugin will read from.\n" +
" Valid options are (don't include .yml): bulgarian, dutch, english, german, turkish, russian")
private String language = "english";
@Comment("Signs\n" +
" This will determine whether vault signs are enabled.\n" +
" If you don't know what this is or if it's for you, see the resource page.")
private boolean signs = false;
@Comment("Economy\n" +
" These are all of the settings for the economy integration. (Requires Vault)\n" +
" Bypass permission is: playervaults.free")
private Economy economy = new Economy();
@Comment("Blocked Items\n" +
" This will allow you to block specific materials from vaults.\n" +
" Bypass permission is: playervaults.bypassblockeditems")
private Block itemBlocking = new Block();
@Comment("Cleanup\n" +
" Enabling this will purge vaults that haven't been touched in the specified time frame.\n" +
" Reminder: This is only checked during startup.\n" +
" This will not lag your server or touch the backups folder.")
private PurgePlanet purge = new PurgePlanet();
@Comment("Sets the highest vault amount this plugin will test perms for")
private int maxVaultAmountPermTest = 99;
@Comment("Storage option. Currently only flatfile, but soon more! :)")
private Storage storage = new Storage();
public void setFromConfig(FileConfiguration c) {
this.debug = c.getBoolean("debug", false);
this.language = c.getString("language", "english");
this.signs = c.getBoolean("signs-enabled", false);
this.economy.enabled = c.getBoolean("economy.enabled", false);
this.economy.feeToCreate = c.getDouble("economy.cost-to-create", 100);
this.economy.feeToOpen = c.getDouble("economy.cost-to-open", 10);
this.economy.refundOnDelete = c.getDouble("economy.refund-on-delete", 50);
this.itemBlocking.enabled = c.getBoolean("blockitems", true);
this.itemBlocking.list = c.getStringList("blocked-items");
if (this.itemBlocking.list == null) {
this.itemBlocking.list = new ArrayList<>();
this.itemBlocking.list.add("PUMPKIN");
this.itemBlocking.list.add("DIAMOND_BLOCK");
}
this.purge.enabled = c.getBoolean("cleanup.enable", false);
this.purge.daysSinceLastEdit = c.getInt("cleanup.lastEdit", 30);
this.storage.flatFile.backups = c.getBoolean("backups.enabled", true);
this.maxVaultAmountPermTest = c.getInt("max-vault-amount-perm-to-test", 99);
}
public boolean isDebug() {
return this.debug;
}
public String getLanguage() {
return this.language;
}
public boolean isSigns() {
return this.signs;
}
public Economy getEconomy() {
return this.economy;
}
public Block getItemBlocking() {
return this.itemBlocking;
}
public PurgePlanet getPurge() {
return this.purge;
}
public int getMaxVaultAmountPermTest() {
return this.maxVaultAmountPermTest;
}
public Storage getStorage() {
return this.storage;
}
}