From 96f2dd34b6353a15a2f4ef6a75c09f296ae562f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bro=C4=8Dko?= Date: Tue, 9 Oct 2018 00:01:20 +0200 Subject: [PATCH] local to JAR files --- resources/plugin.yml | 2 +- src/cz/marwland/mc/core/MarwCore.java | 30 +++++++++++++---- .../marwland/mc/core/config/ConfigFile.java | 33 ++++++++++--------- .../mc/core/config/ConfigManager.java | 13 +++----- src/cz/marwland/mc/core/features/Feature.java | 4 +-- .../core/storage/HikariConnectionFactory.java | 10 ++++-- .../marwland/mc/core/storage/SQLStorage.java | 2 -- src/cz/marwland/mc/core/util/FileUtil.java | 2 +- src/cz/marwland/mc/core/util/UuidUtils.java | 22 +++++++++++++ 9 files changed, 81 insertions(+), 37 deletions(-) create mode 100644 src/cz/marwland/mc/core/util/UuidUtils.java diff --git a/resources/plugin.yml b/resources/plugin.yml index f613184..0230b0a 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -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. diff --git a/src/cz/marwland/mc/core/MarwCore.java b/src/cz/marwland/mc/core/MarwCore.java index 9418b56..0c53324 100644 --- a/src/cz/marwland/mc/core/MarwCore.java +++ b/src/cz/marwland/mc/core/MarwCore.java @@ -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; + } } diff --git a/src/cz/marwland/mc/core/config/ConfigFile.java b/src/cz/marwland/mc/core/config/ConfigFile.java index ce94ee9..fb38ec6 100644 --- a/src/cz/marwland/mc/core/config/ConfigFile.java +++ b/src/cz/marwland/mc/core/config/ConfigFile.java @@ -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()); } } diff --git a/src/cz/marwland/mc/core/config/ConfigManager.java b/src/cz/marwland/mc/core/config/ConfigManager.java index 6d107b6..0f75ee6 100644 --- a/src/cz/marwland/mc/core/config/ConfigManager.java +++ b/src/cz/marwland/mc/core/config/ConfigManager.java @@ -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 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) { diff --git a/src/cz/marwland/mc/core/features/Feature.java b/src/cz/marwland/mc/core/features/Feature.java index f783cf8..f74dc30 100644 --- a/src/cz/marwland/mc/core/features/Feature.java +++ b/src/cz/marwland/mc/core/features/Feature.java @@ -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) { diff --git a/src/cz/marwland/mc/core/storage/HikariConnectionFactory.java b/src/cz/marwland/mc/core/storage/HikariConnectionFactory.java index 6b5d598..91f0b34 100644 --- a/src/cz/marwland/mc/core/storage/HikariConnectionFactory.java +++ b/src/cz/marwland/mc/core/storage/HikariConnectionFactory.java @@ -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; } diff --git a/src/cz/marwland/mc/core/storage/SQLStorage.java b/src/cz/marwland/mc/core/storage/SQLStorage.java index 1b2eafb..bde60ef 100644 --- a/src/cz/marwland/mc/core/storage/SQLStorage.java +++ b/src/cz/marwland/mc/core/storage/SQLStorage.java @@ -71,8 +71,6 @@ public class SQLStorage { } } s.executeBatch(); - } finally { - connection.close(); } } } finally { diff --git a/src/cz/marwland/mc/core/util/FileUtil.java b/src/cz/marwland/mc/core/util/FileUtil.java index 62d9001..b1c13e4 100644 --- a/src/cz/marwland/mc/core/util/FileUtil.java +++ b/src/cz/marwland/mc/core/util/FileUtil.java @@ -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; } diff --git a/src/cz/marwland/mc/core/util/UuidUtils.java b/src/cz/marwland/mc/core/util/UuidUtils.java new file mode 100644 index 0000000..321df1d --- /dev/null +++ b/src/cz/marwland/mc/core/util/UuidUtils.java @@ -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(); + } + +} \ No newline at end of file