Large config rewrite, updating creates incompatibility with older versions of LD. Probably need to clean up configs a bit more.

This commit is contained in:
libraryaddict
2021-01-31 21:28:07 +13:00
parent 7ab4a31c1f
commit bd0ca9ec8c
30 changed files with 841 additions and 703 deletions

View File

@@ -161,7 +161,7 @@ public class DisguiseUtilities {
private static final HashSet<UUID> selfDisguised = new HashSet<>();
private static final HashMap<UUID, String> preDisguiseTeam = new HashMap<>();
private static final HashMap<UUID, String> disguiseTeam = new HashMap<>();
private static final File profileCache = new File("plugins/LibsDisguises/GameProfiles");
private static final File profileCache = new File("plugins/LibsDisguises/SavedSkins");
private static final File savedDisguises = new File("plugins/LibsDisguises/SavedDisguises");
@Getter
private static Gson gson;
@@ -1124,7 +1124,7 @@ public class DisguiseUtilities {
DisguiseUtilities.refreshTrackers(disguise);
}
}, LibsDisguises.getInstance().getConfig().getBoolean("ContactMojangServers", true));
}, DisguiseConfig.isContactMojangServers());
}
/**
@@ -1271,7 +1271,13 @@ public class DisguiseUtilities {
gson = gsonBuilder.create();
if (!profileCache.exists()) {
profileCache.mkdirs();
File old = new File(profileCache.getParentFile(), "GameProfiles");
if (old.exists() && old.isDirectory()) {
old.renameTo(profileCache);
} else {
profileCache.mkdirs();
}
}
if (!savedDisguises.exists()) {
@@ -1331,13 +1337,7 @@ public class DisguiseUtilities {
Method m = CompileMethods.class.getMethod("main", String[].class);
if ((!m.isAnnotationPresent(CompileMethods.CompileMethodsIntfer.class) ||
m.getAnnotation(CompileMethods.CompileMethodsIntfer.class).user().matches("[0-9]+")) &&
!DisguiseConfig.doOutput(LibsDisguises.getInstance().getConfig(), true, false).isEmpty()) {
/*File f = new File(LibsDisguises.getInstance().getDataFolder(), "config.yml");
File f2 = new File(f.getParentFile(), "config-older.yml");
f2.delete();
f.renameTo(f2);
LibsDisguises.getInstance().saveDefaultConfig();*/
m.getAnnotation(CompileMethods.CompileMethodsIntfer.class).user().matches("[0-9]+")) && !DisguiseConfig.doOutput(true, false).isEmpty()) {
DisguiseConfig.setViewDisguises(false);
}
} catch (NoSuchMethodException e) {

View File

@@ -0,0 +1,191 @@
package me.libraryaddict.disguise.utilities.config;
import lombok.Getter;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.reflection.ClassGetter;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by libraryaddict on 31/01/2021.
*/
public class ConfigLoader {
@Getter
private final List<String> configs = new ArrayList<>();
public ConfigLoader() {
for (String s : ClassGetter.getEntriesForPackage(ConfigLoader.class, "configs")) {
if (!s.endsWith(".yml")) {
continue;
}
if (s.endsWith("/disguises.yml") || s.endsWith("/sounds.yml")) {
continue;
}
configs.add(s);
}
}
public void saveMissingConfigs() {
File oldFile = new File(LibsDisguises.getInstance().getDataFolder(), "config.yml");
boolean migrated = oldFile.exists();
for (String config : configs) {
File f = new File(LibsDisguises.getInstance().getDataFolder(), config);
if (f.exists()) {
migrated = false;
continue;
}
saveDefaultConfig(config);
}
if (migrated) {
DisguiseUtilities.getLogger().info("Migrated old config system to new config system");
oldFile.delete();
}
}
public YamlConfiguration loadDefaults() {
YamlConfiguration globalConfig = new YamlConfiguration();
for (String config : configs) {
try {
YamlConfiguration c = new YamlConfiguration();
c.loadFromString(ReflectionManager.getResourceAsString(LibsDisguises.getInstance().getFile(), config));
for (String k : c.getKeys(true)) {
if (c.isConfigurationSection(k)) {
continue;
}
globalConfig.set(k, c.get(k));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return globalConfig;
}
public YamlConfiguration load() {
YamlConfiguration globalConfig = new YamlConfiguration();
for (String config : configs) {
YamlConfiguration c = YamlConfiguration.loadConfiguration(new File(LibsDisguises.getInstance().getDataFolder(), config));
for (String k : c.getKeys(true)) {
if (c.isConfigurationSection(k)) {
continue;
}
globalConfig.set(k, c.get(k));
}
}
return globalConfig;
}
public void saveDefaultConfigs() {
for (String config : configs) {
saveDefaultConfig(config);
}
File f = new File(LibsDisguises.getInstance().getDataFolder(), "config.yml");
f.delete();
}
public void saveDefaultConfig(String name) {
DisguiseUtilities.getLogger().info("Config " + name + " is out of date (Or missing)! Now refreshing it!");
String ourConfig = ReflectionManager.getResourceAsString(LibsDisguises.getInstance().getFile(), name);
YamlConfiguration savedConfig = null;
File loadFrom = new File(LibsDisguises.getInstance().getDataFolder(), name);
File configFile = loadFrom;
if (!loadFrom.exists()) {
loadFrom = new File(LibsDisguises.getInstance().getDataFolder(), "config.yml");
}
if (loadFrom.exists()) {
savedConfig = YamlConfiguration.loadConfiguration(loadFrom);
} else {
try {
savedConfig = new YamlConfiguration();
savedConfig.loadFromString(ourConfig);
} catch (Exception e) {
e.printStackTrace();
}
}
String[] string = ourConfig.split("\n");
StringBuilder section = new StringBuilder();
for (int i = 0; i < string.length; i++) {
String s = string[i];
if (s.trim().startsWith("#") || !s.contains(":")) {
continue;
}
String rawKey = s.split(":")[0];
if (section.length() > 0) {
int matches = StringUtils.countMatches(rawKey, " ");
int allowed = 0;
for (int a = 0; a < matches; a++) {
allowed = section.indexOf(".", allowed) + 1;
}
section = new StringBuilder(section.substring(0, allowed));
}
String key = (rawKey.startsWith(" ") ? section.toString() : "") + rawKey.trim();
if (savedConfig.isConfigurationSection(key)) {
section.append(key).append(".");
} else if (savedConfig.isSet(key)) {
String rawVal = s.split(":")[1].trim();
Object val = savedConfig.get(key);
if (savedConfig.isString(key) && !rawVal.equals("true") && !rawVal.equals("false")) {
val = "'" + StringEscapeUtils.escapeJava(val.toString().replace(ChatColor.COLOR_CHAR + "", "&")) + "'";
}
string[i] = rawKey + ": " + val;
}
}
try {
if (!configFile.getParentFile().exists()) {
configFile.mkdirs();
}
configFile.delete();
configFile.createNewFile();
try (PrintWriter out = new PrintWriter(configFile)) {
out.write(StringUtils.join(string, "\n"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -27,7 +27,7 @@ public class DisguiseCommandConfig {
private boolean enabled;
}
private File commandConfig = new File(LibsDisguises.getInstance().getDataFolder(), "commands.yml");
private File commandConfig = new File(LibsDisguises.getInstance().getDataFolder(), "configs/plugin-commands.yml");
private HashMap<String, DisguiseCommand> commands = new HashMap<>();
private boolean modifyCommands = false;
@@ -87,13 +87,19 @@ public class DisguiseCommandConfig {
section.set("aliases", command.getAliases());
}
String configString = config.saveToString();
configString = configString.replaceAll("\n([a-zA-Z])", "\n\n$1");
String s =
"# The following can be changed to modify how the disguise commands are registered\n# This will only work on server startup\nModifyCommands: " +
modifyCommands + "\n\n" + config.saveToString();
modifyCommands + "\n\n" + configString;
commandConfig.delete();
try {
commandConfig.getParentFile().mkdirs();
commandConfig.createNewFile();
} catch (IOException e) {
e.printStackTrace();

View File

@@ -95,18 +95,14 @@ public class DisguiseListener implements Listener {
// If build number is null, or not a number. Then we can't check snapshots regardless
return !plugin.isNumberedBuild();
// Check snapshots
}
private void runUpdateScheduler() {
boolean autoUpdate = plugin.getConfig().getBoolean("AutoUpdateDev");
if (!plugin.getConfig().getBoolean("NotifyUpdate")) {
if (!DisguiseConfig.isNotifyUpdate()) {
return;
}
if (autoUpdate && !isCheckReleases()) {
if (DisguiseConfig.isAutoUpdate() && !isCheckReleases()) {
DisguiseUtilities.getLogger().info("Plugin will attempt to auto update when new builds are ready! Check config to disable.");
}
}

View File

@@ -95,6 +95,7 @@ public class Metrics {
"Check out https://bStats.org/ to learn more :)").copyDefaults(true);
try {
config.save(configFile);
DisguiseUtilities.getLogger().info("Saved bStats config");
}
catch (IOException ignored) {
}

View File

@@ -221,12 +221,12 @@ public class MetricsInitalizer {
}
});
metrics.addCustomChart(new Metrics.SimplePie("commands") {
/*metrics.addCustomChart(new Metrics.SimplePie("commands") {
@Override
public String getValue() {
return DisguiseConfig.isDisableCommands() ? "Enabled" : "Disabled";
}
});
});*/
metrics.addCustomChart(new Metrics.SimplePie("spigot") {
@Override
@@ -241,7 +241,7 @@ public class MetricsInitalizer {
}
});
final boolean updates = plugin.getConfig().getBoolean("NotifyUpdate");
final boolean updates = DisguiseConfig.isNotifyUpdate();
metrics.addCustomChart(new Metrics.SimplePie("updates") {
@Override

View File

@@ -22,8 +22,31 @@ public class ClassGetter {
return getClassesForPackage(Entity.class, pkgname);
}
public static ArrayList<String> getEntriesForPackage(String pkgname) {
return getEntriesForPackage(Entity.class, pkgname);
}
public static ArrayList<Class<?>> getClassesForPackage(Class runFrom, String pkgname) {
ArrayList<Class<?>> classes = new ArrayList<>();
ArrayList<String> list = getEntriesForPackage(runFrom, pkgname);
ArrayList<Class<?>> classList = new ArrayList<>();
for (String s : list) {
if (!s.endsWith(".class")) {
continue;
}
Class<?> c = loadClass(s.replace(".class", "").replace('/', '.'));
if (c != null) {
classList.add(c);
}
}
return classList;
}
public static ArrayList<String> getEntriesForPackage(Class runFrom, String pkgname) {
ArrayList<String> classes = new ArrayList<>();
// String relPath = pkgname.replace('.', '/');
// Get a File object for the package
@@ -36,16 +59,11 @@ public class ClassGetter {
processJarfile(resource, pkgname, classes);
} else {
for (File f : new File(resource.getPath() + "/" + pkgname.replace(".", "/")).listFiles()) {
if (!f.getName().endsWith(".class") || f.getName().contains("$")) {
if (f.getName().contains("$")) {
continue;
}
try {
classes.add(Class.forName(pkgname + "." + f.getName().replace(".class", "")));
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
classes.add(pkgname + "/" + f.getName());
}
}
}
@@ -56,16 +74,12 @@ public class ClassGetter {
private static Class<?> loadClass(String className) {
try {
return Class.forName(className);
}
catch (ClassNotFoundException e) {
throw new RuntimeException("Unexpected ClassNotFoundException loading class '" + className + "'");
}
catch (NoClassDefFoundError e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
return null;
}
}
private static void processJarfile(URL resource, String pkgname, ArrayList<Class<?>> classes) {
private static void processJarfile(URL resource, String pkgname, ArrayList<String> classes) {
try {
String relPath = pkgname.replace('.', '/');
String resPath = URLDecoder.decode(resource.getPath(), "UTF-8");
@@ -79,22 +93,18 @@ public class ClassGetter {
JarEntry entry = entries.nextElement();
String entryName = entry.getName();
String className = null;
if (entryName.endsWith(".class") && entryName.startsWith(relPath) &&
entryName.length() > (relPath.length() + "/".length())) {
className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
}
if (className != null) {
Class<?> c = loadClass(className);
if (c != null) {
classes.add(c);
}
if (entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
className = entryName.replace('\\', '/');
}
if (className != null) {
classes.add(className);
}
}
jarFile.close();
}
catch (Exception ex) {
} catch (Exception ex) {
ex.printStackTrace();
}
}

View File

@@ -29,10 +29,18 @@ public class SoundManager {
}
private void loadCustomSounds() {
File f = new File(LibsDisguises.getInstance().getDataFolder(), "sounds.yml");
File f = new File(LibsDisguises.getInstance().getDataFolder(), "configs/sounds.yml");
if (!f.exists()) {
LibsDisguises.getInstance().saveResource("sounds.yml", false);
f.getParentFile().mkdirs();
File old = new File(LibsDisguises.getInstance().getDataFolder(), "sounds.yml");
if (old.exists()) {
old.renameTo(f);
} else {
LibsDisguises.getInstance().saveResource("configs/sounds.yml", false);
}
}
YamlConfiguration config = YamlConfiguration.loadConfiguration(f);
@@ -55,8 +63,7 @@ public class SoundManager {
continue;
}
List<String> list = section.getStringList(
type.name().charAt(0) + type.name().substring(1).toLowerCase(Locale.ENGLISH));
List<String> list = section.getStringList(type.name().charAt(0) + type.name().substring(1).toLowerCase(Locale.ENGLISH));
if (list == null || list.isEmpty()) {
continue;
@@ -67,17 +74,15 @@ public class SoundManager {
SoundGroup subGroup = SoundGroup.getGroup(sound);
if (subGroup == null) {
DisguiseUtilities.getLogger().warning("Invalid sound '" + sound +
"'! Must be a minecraft:sound.name or SoundGroup name!");
DisguiseUtilities.getLogger().warning("Invalid sound '" + sound + "'! Must be a minecraft:sound.name or SoundGroup name!");
continue;
}
Object[] sounds = subGroup.getDisguiseSounds().get(type);
if (sounds == null) {
DisguiseUtilities.getLogger().warning(
"Sound group '" + sound + "' does not contain a category for " + type +
"! Can't use as default in " + key);
DisguiseUtilities.getLogger()
.warning("Sound group '" + sound + "' does not contain a category for " + type + "! Can't use as default in " + key);
continue;
}

View File

@@ -359,7 +359,7 @@ public enum LibsMsg {
LD_COMMAND_DEBUG(ChatColor.BLUE + "/libsdisguises debug - " + ChatColor.AQUA +
"Used to help debug scoreboard issues on a player disguise"),
LD_COMMAND_UPLOAD_LOGS(ChatColor.BLUE + "/libsdisguises uploadlogs - " + ChatColor.AQUA +
"Uploads latest.log, disguises.yml and config.yml and gives you the link to share. Used when seeking " +
"Uploads latest.log, disguises.yml and configs and gives you the link to share. Used when seeking " +
"assistance."),
SELF_DISGUISE_HIDDEN(ChatColor.GREEN + "Self disguise hidden as it's too tall..");