Improve unloading/disabling of the plugin

This commit is contained in:
GeorgH93 2018-06-01 18:19:33 +02:00
parent d493e9905a
commit 1506792757
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
5 changed files with 44 additions and 8 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId>
<version>1.17.8</version>
<version>1.17.9-SNAPSHOT</version>
<scm>
<connection>scm:git:git@github.com:GeorgH93/Minepacks.git</connection>

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 GeorgH93
* Copyright (C) 2014-2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -113,11 +113,13 @@ public void open(Player p, 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 se an other title then everyone else.
// The owner will se 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), p.equals(owner) ? MinePacks.backpackTitle : titleOther);
}
catch(Exception e)
@ -201,6 +203,16 @@ public List<ItemStack> setSize(int newSize)
return removedItems;
}
public void closeAll()
{
for(Entry<Player, Boolean> e : opened.entrySet())
{
e.getKey().closeInventory();
}
opened.clear();
save();
}
/**
* Get the object's inventory.
*

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2017 GeorgH93
* Copyright (C) 2014-2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -24,6 +24,7 @@
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
public class Database
{
@ -166,7 +167,15 @@ public void onFail()
}
// DB Functions
public void close() { }
public void close()
{
// Ensure that all backpacks are closed and saved before killing the database
for(Map.Entry<OfflinePlayer, Backpack> backpackEntry : backpacks.entrySet())
{
backpackEntry.getValue().closeAll();
}
backpacks.clear();
}
public void updatePlayerAndLoadBackpack(Player player)
{

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2017 GeorgH93
* Copyright (C) 2014-2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -20,6 +20,7 @@
import at.pcgamingfreaks.MinePacks.Backpack;
import at.pcgamingfreaks.MinePacks.MinePacks;
import at.pcgamingfreaks.UUIDConverter;
import at.pcgamingfreaks.Utils;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@ -92,6 +93,8 @@ protected void loadSettings()
@Override
public void close()
{
super.close();
Utils.blockThread(1);
dataSource.close();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2017 GeorgH93
* Copyright (C) 2014-2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -28,7 +28,11 @@
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
@ -112,6 +116,13 @@ public void onDone(UpdateResult updateResult)
@Override
public void onDisable()
{
getCommand("backpack").setExecutor(new CommandExecutor() {
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings)
{
return false;
}
});
Updater updater = null;
if(config != null && config.getAutoUpdate()) // Lets check for updates
{
@ -119,9 +130,10 @@ public void onDisable()
updater = new Updater(this, this.getFile(), true, 83445); // Create a new updater with dev.bukkit.org as update provider
updater.update(); // Starts the update, if there is a new update available it will download while we close the rest
}
getServer().getScheduler().cancelTasks(this); // Stop the listener, we don't need them any longer
if(DB != null) DB.close(); // Close the DB connection, we won't need them any longer
HandlerList.unregisterAll(this); // Stop the listener, they are no longer needed
if(updater != null) updater.waitForAsyncOperation(); // The update can download while we kill the listeners and close the DB connections
getServer().getScheduler().cancelTasks(this); // Kill all still running tasks
instance = null;
if(lang != null) log.info(lang.get("Console.Disabled"));
}