local to JAR files

This commit is contained in:
Erik Bročko 2018-10-09 00:01:20 +02:00
parent a84ad39328
commit 96f2dd34b6
9 changed files with 81 additions and 37 deletions

View File

@ -5,7 +5,7 @@ api-version: 1.12
author: ericek111
website: https://git.lixko.eu/MarwLand/MarwCore
depend: [LuckPerms]
softdepend: [WorldGuard, Factions]
softdepend: [WorldGuard, Factions, PlaceholderAPI]
commands:
marw:
description: Core plugin controls.

View File

@ -6,6 +6,9 @@ import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.bukkit.configuration.file.FileConfiguration;
@ -30,6 +33,7 @@ public class MarwCore extends JavaPlugin {
private File libsFolder = null;
private ModuleClassLoader moduleClassLoader;
private SQLStorage sqlStorage;
private ScheduledExecutorService sqlExecutor;
@Override
public void onEnable() {
@ -38,11 +42,12 @@ public class MarwCore extends JavaPlugin {
modulesFolder.mkdirs();
libsFolder = this.getLibsFolderPath().toFile();
libsFolder.mkdir();
configManager = new ConfigManager(this);
configManager.registerConfig("config.yml");
configManager = new ConfigManager();
configManager.registerConfig(this.getClass(), "config.yml");
configManager.loadConfig("config.yml");
this.loadModulesAndLibs();
this.loadDatabase();
sqlExecutor = Executors.newScheduledThreadPool(sqlStorage.getConnectionFactory().getConfiguration().getMaxPoolSize() - 1);
this.getCommand("marw").setExecutor(new MarwCommandExecutor());
this.enableModules();
@ -51,9 +56,19 @@ public class MarwCore extends JavaPlugin {
@Override
public void onDisable() {
this.features.forEach((k, v) -> v.onDisable());
this.sqlExecutor.shutdown();
try {
if (!sqlExecutor.awaitTermination(60, TimeUnit.SECONDS)) {
sqlExecutor.shutdownNow();
if (!sqlExecutor.awaitTermination(60, TimeUnit.SECONDS))
this.getLogger().severe("Failed to shut down SQL Executor thread pool!");
}
} catch (InterruptedException ie) {
sqlExecutor.shutdownNow();
Thread.currentThread().interrupt();
}
this.configManager.save();
if (sqlStorage != null)
this.sqlStorage.shutdown();
this.sqlStorage.shutdown();
}
@Override
@ -95,7 +110,7 @@ public class MarwCore extends JavaPlugin {
this.addFeature(f);
}
configManager.load();
this.reloadConfig();
this.features.forEach((k, v) -> v.onEnable());
}
@ -169,5 +184,8 @@ public class MarwCore extends JavaPlugin {
public SQLStorage getStorage() {
return this.sqlStorage;
}
public ScheduledExecutorService getStorageExecutor() {
return this.sqlExecutor;
}
}

View File

@ -10,23 +10,24 @@ import java.util.logging.Level;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import cz.marwland.mc.core.MarwCore;
public class ConfigFile {
private final Plugin plugin;
private final Class<?> parent;
private File file;
private String path;
private String template;
private FileConfiguration cfg;
public ConfigFile(Plugin plugin, String template, String path) {
this.plugin = plugin;
this.path = template;
this.file = new File(plugin.getDataFolder(), path);
public ConfigFile(Class<?> parent, String template, String path) {
this.parent = parent;
this.template = template;
this.file = new File(MarwCore.getInstance().getDataFolder(), path);
}
public ConfigFile(Plugin plugin, String path) {
this(plugin, path, path);
public ConfigFile(Class<?> parent, String path) {
this(parent, path, path);
}
public File getFile() {
@ -41,12 +42,12 @@ public class ConfigFile {
if (!file.exists()) {
file.getParentFile().mkdirs();
InputStream in = plugin.getResource(path);
InputStream in = parent.getResourceAsStream(template);
if (in == null) {
plugin.getLogger().config("The embedded resource '" + path + "' cannot be found!");
MarwCore.getInstance().getLogger().log(Level.WARNING, "The embedded resource '" + parent.getName() + "/" + template + "' cannot be found!");
return;
}
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
@ -57,7 +58,7 @@ public class ConfigFile {
out.close();
in.close();
} catch (IOException ex) {
plugin.getLogger().log(Level.CONFIG, "Could not save '" + path + "' to " + file, ex);
MarwCore.getInstance().getLogger().log(Level.WARNING, "Could not save '" + parent.getName() + "/" + template + "' to " + file, ex);
}
}
@ -67,15 +68,17 @@ public class ConfigFile {
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
plugin.getLogger().config("Invalid configuration " + path + ": " + e.getMessage());
MarwCore.getInstance().getLogger().log(Level.WARNING, "Invalid configuration '" + parent.getName() + "/" + template + "': " + e.getMessage());
}
}
public void save() {
if (cfg == null)
return;
try {
cfg.save(file);
} catch (IOException e) {
plugin.getLogger().config("Cannot save " + path + ": " + e.getMessage());
MarwCore.getInstance().getLogger().log(Level.WARNING, "Cannot save '" + parent.getName() + "/" + template + "': " + e.getMessage());
}
}

View File

@ -3,23 +3,20 @@ package cz.marwland.mc.core.config;
import java.util.HashMap;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
public class ConfigManager {
private final Plugin plugin;
private HashMap<String, ConfigFile> configs = new HashMap<>();
public ConfigManager(Plugin pl) {
this.plugin = pl;
public ConfigManager() {
}
public void registerConfig(String path) {
configs.put(path, new ConfigFile(plugin, path));
public void registerConfig(Class<?> parent, String path) {
configs.put(path, new ConfigFile(parent, path));
}
public void registerConfig(String template, String path) {
configs.put(path, new ConfigFile(plugin, template, path));
public void registerConfig(Class<?> parent, String template, String path) {
configs.put(path, new ConfigFile(parent, template, path));
}
public void loadConfig(String path) {

View File

@ -68,11 +68,11 @@ public abstract class Feature implements Listener {
}
protected void registerConfig(String path) {
registerConfig(path, path);
registerConfig("/resources/" + path, path);
}
protected void registerConfig(String template, String path) {
plugin.getConfigManager().registerConfig(template, path);
plugin.getConfigManager().registerConfig(this.getClass(), template, path);
}
protected FileConfiguration getConfig(String path) {

View File

@ -11,6 +11,7 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
protected final StorageCredentials configuration;
private HikariDataSource hikari;
private HikariConfig config;
public HikariConnectionFactory(StorageCredentials configuration) {
this.configuration = configuration;
@ -42,8 +43,8 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
@Override
public void init() {
HikariConfig config = new HikariConfig();
config.setPoolName("luckperms-hikari");
config = new HikariConfig();
config.setPoolName("marwcore");
appendConfigurationInfo(config);
appendProperties(config, this.configuration);
@ -73,6 +74,11 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
if (connection == null) {
throw new SQLException("Unable to get a connection from the pool.");
}
if (connection.isClosed()) {
System.out.println("Returned CLOSED connection!");
this.init();
connection = this.hikari.getConnection();
}
return connection;
}

View File

@ -71,8 +71,6 @@ public class SQLStorage {
}
}
s.executeBatch();
} finally {
connection.close();
}
}
} finally {

View File

@ -30,7 +30,7 @@ public class FileUtil {
while ((length = in.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
String ret = result.toString(StandardCharsets.UTF_8.name() );
String ret = result.toString(StandardCharsets.UTF_8.name());
in.close();
return ret;
}

View File

@ -0,0 +1,22 @@
package cz.marwland.mc.core.util;
import java.nio.ByteBuffer;
import java.util.UUID;
public class UuidUtils {
public static UUID asUuid(byte[] bytes) {
ByteBuffer bb = ByteBuffer.wrap(bytes);
long firstLong = bb.getLong();
long secondLong = bb.getLong();
return new UUID(firstLong, secondLong);
}
public static byte[] asBytes(UUID uuid) {
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
}
}