Taboolib 5.0 fully refactored & Not a plugin now.
This commit is contained in:
106
src/main/scala/io/izzel/taboolib/module/config/TConfig.java
Normal file
106
src/main/scala/io/izzel/taboolib/module/config/TConfig.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package io.izzel.taboolib.module.config;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.util.Files;
|
||||
import io.izzel.taboolib.util.Ref;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-09-08 15:00
|
||||
*/
|
||||
public class TConfig extends YamlConfiguration {
|
||||
|
||||
private static Map<String, List<File>> files = Maps.newHashMap();
|
||||
private File file;
|
||||
private Runnable runnable;
|
||||
|
||||
private TConfig(File file, Plugin plugin) {
|
||||
files.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(file);
|
||||
this.file = file;
|
||||
reload();
|
||||
TConfigWatcher.getInst().addSimpleListener(this.file, this::reload);
|
||||
TabooLibAPI.debug("Loaded TConfiguration \"" + file.getName() + "\" from Plugin \"" + plugin.getName() + "\"");
|
||||
}
|
||||
|
||||
public static Map<String, List<File>> getFiles() {
|
||||
return files;
|
||||
}
|
||||
|
||||
public static TConfig create(File file) {
|
||||
return new TConfig(file, Ref.getCallerPlugin(Ref.getCallerClass(3).orElse(TabooLib.class)));
|
||||
}
|
||||
|
||||
public static TConfig create(File file, Plugin plugin) {
|
||||
return new TConfig(file, plugin);
|
||||
}
|
||||
|
||||
public static TConfig create(Plugin plugin, String path) {
|
||||
File file = new File(plugin.getDataFolder(), path);
|
||||
if (!file.exists()) {
|
||||
Files.releaseResource(plugin, path, false);
|
||||
}
|
||||
return create(file, plugin);
|
||||
}
|
||||
|
||||
public String getStringColored(String path) {
|
||||
return TLocale.Translate.setColored(getString(path));
|
||||
}
|
||||
|
||||
public String getStringColored(String path, String def) {
|
||||
return TLocale.Translate.setColored(getString(path, def));
|
||||
}
|
||||
|
||||
public List<String> getStringListColored(String path) {
|
||||
return TLocale.Translate.setColored(getStringList(path));
|
||||
}
|
||||
|
||||
public void release() {
|
||||
TConfigWatcher.getInst().removeListener(file);
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
try {
|
||||
load(file);
|
||||
runListener();
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
TLogger.getGlobalLogger().warn("Cannot load configuration from stream: " + e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public TConfig listener(Runnable runnable) {
|
||||
this.runnable = runnable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void runListener() {
|
||||
try {
|
||||
Optional.ofNullable(runnable).ifPresent(Runnable::run);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package io.izzel.taboolib.module.config;
|
||||
|
||||
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
||||
import org.apache.commons.lang3.tuple.Triple;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @author lzzelAliz
|
||||
*/
|
||||
public class TConfigWatcher {
|
||||
|
||||
private static TConfigWatcher configWatcher = new TConfigWatcher();
|
||||
private final ScheduledExecutorService service = Executors.newScheduledThreadPool(1, new BasicThreadFactory.Builder().namingPattern("TConfigWatcherService-%d").build());
|
||||
private final Map<WatchService, Triple<File, Object, Consumer<Object>>> map = new HashMap<>();
|
||||
|
||||
public TConfigWatcher() {
|
||||
service.scheduleAtFixedRate(() -> {
|
||||
synchronized (map) {
|
||||
map.forEach((service, triple) -> {
|
||||
WatchKey key;
|
||||
while ((key = service.poll()) != null) {
|
||||
for (WatchEvent<?> watchEvent : key.pollEvents()) {
|
||||
if (triple.getLeft().getName().equals(Objects.toString(watchEvent.context()))) {
|
||||
triple.getRight().accept(triple.getMiddle());
|
||||
}
|
||||
}
|
||||
key.reset();
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 1000, 100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public static TConfigWatcher getInst() {
|
||||
return configWatcher;
|
||||
}
|
||||
|
||||
public void addSimpleListener(File file, Runnable runnable) {
|
||||
addListener(file, null, obj -> runnable.run());
|
||||
}
|
||||
|
||||
public void addOnListen(File file, Object obj, Consumer<Object> consumer) {
|
||||
try {
|
||||
WatchService service = FileSystems.getDefault().newWatchService();
|
||||
file.getParentFile().toPath().register(service, StandardWatchEventKinds.ENTRY_MODIFY);
|
||||
map.putIfAbsent(service, Triple.of(file, obj, consumer));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> void addListener(File file, T obj, Consumer<T> consumer) {
|
||||
addOnListen(file, obj, (Consumer<Object>) consumer);
|
||||
}
|
||||
|
||||
public void removeListener(File file) {
|
||||
synchronized (map) {
|
||||
map.entrySet().removeIf(entry -> {
|
||||
if (entry.getValue().getLeft().equals(file)) {
|
||||
try {
|
||||
entry.getKey().close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void unregisterAll() {
|
||||
service.shutdown();
|
||||
map.forEach((service, pair) -> {
|
||||
try {
|
||||
service.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user