From b6a8d372f5dea5bfcc763ca2b243f9fc6cc1c27b Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Mon, 4 Jun 2018 13:14:15 +0200 Subject: [PATCH] Improve disabling of the plugin --- src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java | 11 ++++++++++- .../Minepacks/Bukkit/Database/Database.java | 2 ++ .../pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java | 2 ++ src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java | 11 +++++++++-- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java index fe39430..bc2b42a 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java @@ -114,11 +114,13 @@ public void open(@NotNull Player player, boolean editable) // It's not perfect, but it is the only way of doing this. // This sets the title of the inventory based on the person who is opening it. - // The owner will see an other title then everyone else. + // The owner will see an other title, then everyone else. // This way we can add owner name to the tile for everyone else. try { + //noinspection ConstantConditions FIELD_TITLE.setAccessible(true); + //noinspection ConstantConditions FIELD_TITLE.set(METHOD_GET_INVENTORY.invoke(bp), player.equals(owner) ? Minepacks.getInstance().backpackTitle : titleOther); } catch(Exception e) @@ -134,6 +136,13 @@ public void close(Player p) opened.remove(p); } + public void closeAll() + { + opened.forEach((key, value) -> key.closeInventory()); + opened.clear(); + save(); + } + @Override public boolean isOpen() { diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java index 166328d..37e7824 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java @@ -69,6 +69,8 @@ public void init() public void close() { HandlerList.unregisterAll(this); + backpacks.forEach((key, value) -> value.closeAll()); + backpacks.clear(); unCacheStrategie.close(); } diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java index 6fb7683..13b9d98 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java @@ -22,6 +22,7 @@ import at.pcgamingfreaks.Minepacks.Bukkit.Backpack; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import at.pcgamingfreaks.UUIDConverter; +import at.pcgamingfreaks.Utils; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -122,6 +123,7 @@ protected void loadSettings() public void close() { super.close(); + Utils.blockThread(1); // Give the database some time to perform async operations dataSource.close(); } diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java index 3ab01c1..13a530a 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java @@ -43,6 +43,7 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; @@ -72,6 +73,7 @@ public class Minepacks extends JavaPlugin implements MinepacksPlugin private int maxSize; private Collection worldBlacklist; private WorldBlacklistMode worldBlacklistMode; + private ItemsCollector collector; public static Minepacks getInstance() { @@ -160,7 +162,8 @@ private void load() //endregion if(config.getFullInvCollect()) { - (new ItemsCollector(this)).runTaskTimer(this, config.getFullInvCheckInterval(), config.getFullInvCheckInterval()); + collector = new ItemsCollector(this); + collector.runTaskTimer(this, config.getFullInvCheckInterval(), config.getFullInvCheckInterval()); } worldBlacklist = config.getWorldBlacklist(); if(worldBlacklist.size() == 0) @@ -175,7 +178,11 @@ private void load() private void unload() { - getServer().getScheduler().cancelTasks(this); // Stop the listener, we don't need them any longer + //TODO disable command + collector.cancel(); + if(database != null) database.close(); + HandlerList.unregisterAll(this); // Stop the listeners + getServer().getScheduler().cancelTasks(this); // Kill all running task database.close(); // Close the DB connection, we won't need them any longer instance = null; }