switch config to configurate
This commit is contained in:
parent
c487b621e2
commit
37faacdc6b
|
|
@ -1,151 +0,0 @@
|
||||||
package com.alttd.afkdectector.config;
|
|
||||||
|
|
||||||
import com.alttd.afkdectector.AFKDetector;
|
|
||||||
import com.alttd.afkdectector.util.Logger;
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
@SuppressWarnings({"unused", "SameParameterValue"})
|
|
||||||
abstract class AbstractConfig {
|
|
||||||
File file;
|
|
||||||
YamlConfiguration yaml;
|
|
||||||
File configPath;
|
|
||||||
|
|
||||||
AbstractConfig(String filename) {
|
|
||||||
this.configPath = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName());
|
|
||||||
this.file = new File(configPath, filename);
|
|
||||||
this.yaml = new YamlConfiguration();
|
|
||||||
if (!this.file.getParentFile().exists()) {
|
|
||||||
if(!this.file.getParentFile().mkdirs()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
yaml.load(file);
|
|
||||||
} catch (IOException ignore) {
|
|
||||||
} catch (InvalidConfigurationException ex) {
|
|
||||||
Logger.severe(String.format("Could not load %s, please correct your syntax errors", filename));
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
yaml.options().copyDefaults(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void readConfig(Class<?> clazz, Object instance) {
|
|
||||||
for (Method method : clazz.getDeclaredMethods()) {
|
|
||||||
if (Modifier.isPrivate(method.getModifiers())) {
|
|
||||||
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
|
|
||||||
try {
|
|
||||||
method.setAccessible(true);
|
|
||||||
method.invoke(instance);
|
|
||||||
} catch (InvocationTargetException ex) {
|
|
||||||
throw new RuntimeException(ex.getCause());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Logger.severe("Error invoking " + method);
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
yaml.save(file);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
Logger.severe("Could not save " + file);
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void set(String path, Object val) {
|
|
||||||
yaml.addDefault(path, val);
|
|
||||||
yaml.set(path, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
String getString(String path, String def) {
|
|
||||||
yaml.addDefault(path, def);
|
|
||||||
return yaml.getString(path, yaml.getString(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean getBoolean(String path, boolean def) {
|
|
||||||
yaml.addDefault(path, def);
|
|
||||||
return yaml.getBoolean(path, yaml.getBoolean(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
int getInt(String path, int def) {
|
|
||||||
yaml.addDefault(path, def);
|
|
||||||
return yaml.getInt(path, yaml.getInt(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
double getDouble(String path, double def) {
|
|
||||||
yaml.addDefault(path, def);
|
|
||||||
return yaml.getDouble(path, yaml.getDouble(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
<T> List<?> getList(String path, T def) {
|
|
||||||
yaml.addDefault(path, def);
|
|
||||||
return yaml.getList(path, yaml.getList(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
<T> Map<String, T> getMap(final @NonNull String path, final @Nullable Map<String, T> def) {
|
|
||||||
final ImmutableMap.Builder<String, T> builder = ImmutableMap.builder();
|
|
||||||
if (def != null && yaml.getConfigurationSection(path) == null) {
|
|
||||||
yaml.addDefault(path, def.isEmpty() ? new HashMap<>() : def);
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
final ConfigurationSection section = yaml.getConfigurationSection(path);
|
|
||||||
if (section != null) {
|
|
||||||
for (String key : section.getKeys(false)) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
final T val = (T) section.get(key);
|
|
||||||
if (val != null) {
|
|
||||||
builder.put(key, val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return builder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack getItemStack(String path, ItemStack def) {
|
|
||||||
yaml.addDefault(path, def);
|
|
||||||
return yaml.getItemStack(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setLocations(String path, ArrayList<Location> locs) {
|
|
||||||
ArrayList<String> list = new ArrayList<>();
|
|
||||||
for(Location l : locs) {
|
|
||||||
String toSave = l.getWorld().getName() + ":" + l.getX() + ":" + l.getY() + ":" + l.getZ();
|
|
||||||
list.add(toSave);
|
|
||||||
}
|
|
||||||
yaml.set(path, list);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Location> getLocations(String path){
|
|
||||||
List<String> attempts = yaml.getStringList(path);
|
|
||||||
|
|
||||||
if (attempts == null || attempts.isEmpty()) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
List<Location> locations = new ArrayList<>();
|
|
||||||
|
|
||||||
for (String attempt : attempts) {
|
|
||||||
String[] parts = attempt.split(":");
|
|
||||||
locations.add(new Location(Bukkit.getWorld(parts[0]), Double.parseDouble(parts[1]), Double.parseDouble(parts[2]), Double.parseDouble(parts[3])));
|
|
||||||
}
|
|
||||||
return locations;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,11 +1,15 @@
|
||||||
package com.alttd.afkdectector.config;
|
package com.alttd.afkdectector.config;
|
||||||
|
|
||||||
|
import com.alttd.afkdectector.AFKDetector;
|
||||||
|
import com.alttd.galaxy.configuration.AbstractConfiguration;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class Config extends AbstractConfig {
|
public class Config extends AbstractConfiguration {
|
||||||
private Config() {
|
private Config() {
|
||||||
super("config.yml");
|
super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName()), "config.yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
static Config config;
|
static Config config;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,14 @@
|
||||||
package com.alttd.afkdectector.config;
|
package com.alttd.afkdectector.config;
|
||||||
|
|
||||||
|
import com.alttd.afkdectector.AFKDetector;
|
||||||
|
import com.alttd.galaxy.configuration.AbstractConfiguration;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class MessagesConfig extends AbstractConfig {
|
public class MessagesConfig extends AbstractConfiguration {
|
||||||
private MessagesConfig() {
|
private MessagesConfig() {
|
||||||
super("messages.yml");
|
super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName()), "messages.yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
static MessagesConfig config;
|
static MessagesConfig config;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user