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 author: ericek111
website: https://git.lixko.eu/MarwLand/MarwCore website: https://git.lixko.eu/MarwLand/MarwCore
depend: [LuckPerms] depend: [LuckPerms]
softdepend: [WorldGuard, Factions] softdepend: [WorldGuard, Factions, PlaceholderAPI]
commands: commands:
marw: marw:
description: Core plugin controls. description: Core plugin controls.

View File

@ -6,6 +6,9 @@ import java.net.URLClassLoader;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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 java.util.logging.Level;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -30,6 +33,7 @@ public class MarwCore extends JavaPlugin {
private File libsFolder = null; private File libsFolder = null;
private ModuleClassLoader moduleClassLoader; private ModuleClassLoader moduleClassLoader;
private SQLStorage sqlStorage; private SQLStorage sqlStorage;
private ScheduledExecutorService sqlExecutor;
@Override @Override
public void onEnable() { public void onEnable() {
@ -38,11 +42,12 @@ public class MarwCore extends JavaPlugin {
modulesFolder.mkdirs(); modulesFolder.mkdirs();
libsFolder = this.getLibsFolderPath().toFile(); libsFolder = this.getLibsFolderPath().toFile();
libsFolder.mkdir(); libsFolder.mkdir();
configManager = new ConfigManager(this); configManager = new ConfigManager();
configManager.registerConfig("config.yml"); configManager.registerConfig(this.getClass(), "config.yml");
configManager.loadConfig("config.yml"); configManager.loadConfig("config.yml");
this.loadModulesAndLibs(); this.loadModulesAndLibs();
this.loadDatabase(); this.loadDatabase();
sqlExecutor = Executors.newScheduledThreadPool(sqlStorage.getConnectionFactory().getConfiguration().getMaxPoolSize() - 1);
this.getCommand("marw").setExecutor(new MarwCommandExecutor()); this.getCommand("marw").setExecutor(new MarwCommandExecutor());
this.enableModules(); this.enableModules();
@ -51,8 +56,18 @@ public class MarwCore extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
this.features.forEach((k, v) -> v.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(); this.configManager.save();
if (sqlStorage != null)
this.sqlStorage.shutdown(); this.sqlStorage.shutdown();
} }
@ -95,7 +110,7 @@ public class MarwCore extends JavaPlugin {
this.addFeature(f); this.addFeature(f);
} }
configManager.load(); this.reloadConfig();
this.features.forEach((k, v) -> v.onEnable()); this.features.forEach((k, v) -> v.onEnable());
} }
@ -170,4 +185,7 @@ public class MarwCore extends JavaPlugin {
return this.sqlStorage; 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.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import cz.marwland.mc.core.MarwCore;
public class ConfigFile { public class ConfigFile {
private final Plugin plugin; private final Class<?> parent;
private File file; private File file;
private String path; private String template;
private FileConfiguration cfg; private FileConfiguration cfg;
public ConfigFile(Plugin plugin, String template, String path) { public ConfigFile(Class<?> parent, String template, String path) {
this.plugin = plugin; this.parent = parent;
this.path = template; this.template = template;
this.file = new File(plugin.getDataFolder(), path); this.file = new File(MarwCore.getInstance().getDataFolder(), path);
} }
public ConfigFile(Plugin plugin, String path) { public ConfigFile(Class<?> parent, String path) {
this(plugin, path, path); this(parent, path, path);
} }
public File getFile() { public File getFile() {
@ -41,9 +42,9 @@ public class ConfigFile {
if (!file.exists()) { if (!file.exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
InputStream in = plugin.getResource(path); InputStream in = parent.getResourceAsStream(template);
if (in == null) { 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; return;
} }
@ -57,7 +58,7 @@ public class ConfigFile {
out.close(); out.close();
in.close(); in.close();
} catch (IOException ex) { } 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InvalidConfigurationException e) { } 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() { public void save() {
if (cfg == null)
return;
try { try {
cfg.save(file); cfg.save(file);
} catch (IOException e) { } 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 java.util.HashMap;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
public class ConfigManager { public class ConfigManager {
private final Plugin plugin;
private HashMap<String, ConfigFile> configs = new HashMap<>(); private HashMap<String, ConfigFile> configs = new HashMap<>();
public ConfigManager(Plugin pl) { public ConfigManager() {
this.plugin = pl;
} }
public void registerConfig(String path) { public void registerConfig(Class<?> parent, String path) {
configs.put(path, new ConfigFile(plugin, path)); configs.put(path, new ConfigFile(parent, path));
} }
public void registerConfig(String template, String path) { public void registerConfig(Class<?> parent, String template, String path) {
configs.put(path, new ConfigFile(plugin, template, path)); configs.put(path, new ConfigFile(parent, template, path));
} }
public void loadConfig(String path) { public void loadConfig(String path) {

View File

@ -68,11 +68,11 @@ public abstract class Feature implements Listener {
} }
protected void registerConfig(String path) { protected void registerConfig(String path) {
registerConfig(path, path); registerConfig("/resources/" + path, path);
} }
protected void registerConfig(String template, String 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) { protected FileConfiguration getConfig(String path) {

View File

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

View File

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

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();
}
}