Unregister command on disable

This commit is contained in:
Eric 2020-03-18 16:04:57 +01:00
parent b55e9f2c4f
commit 7c0da7c212
2 changed files with 53 additions and 18 deletions

View File

@ -5,12 +5,15 @@ import java.io.InputStreamReader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.Map;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
@ -47,7 +50,7 @@ public class ShopChestImpl extends ShopChest {
@Override @Override
public void onLoad() { public void onLoad() {
if (!loadConfig()) { if (!loadConfigData()) {
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
return; return;
} }
@ -69,26 +72,14 @@ public class ShopChestImpl extends ShopChest {
@Override @Override
public void onDisable() { public void onDisable() {
if (configManager != null) { unregisterCommand();
try { saveConfigData();
configManager.save(); unloadDatabase();
} catch (IOException e) {
Logger.severe("Failed to save configuration file. Config may have been lost");
Logger.severe(e);
}
}
if (database != null) {
if (database instanceof SQLite) {
((SQLite) database).vacuum();
}
database.disconnect();
}
getShopManager().getShops().forEach(shop -> ((ShopImpl) shop).destroy()); getShopManager().getShops().forEach(shop -> ((ShopImpl) shop).destroy());
} }
private boolean loadConfig() { private boolean loadConfigData() {
configManager = ConfigManager.get(this); configManager = ConfigManager.get(this);
try { try {
@ -102,6 +93,17 @@ public class ShopChestImpl extends ShopChest {
return true; return true;
} }
private void saveConfigData() {
if (configManager != null) {
try {
configManager.save();
} catch (IOException e) {
Logger.severe("Failed to save configuration file. Config may have been lost");
Logger.severe(e);
}
}
}
private boolean loadEconomy() { private boolean loadEconomy() {
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class); RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) { if (rsp == null) {
@ -138,6 +140,29 @@ public class ShopChestImpl extends ShopChest {
} }
} }
private void unregisterCommand() {
try {
Command pluginCommand = ((ShopCommandImpl) command).getPluginCommand();
Field fieldCommandMap = getServer() .getClass().getDeclaredField("commandMap");
fieldCommandMap.setAccessible(true);
CommandMap commandMap = (CommandMap) fieldCommandMap.get(getServer());
pluginCommand.unregister(commandMap);
Field fieldKnownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
fieldKnownCommands.setAccessible(true);
Map<?, ?> knownCommands = (Map<?, ?>) fieldKnownCommands.get(commandMap);
knownCommands.remove("shopchest:" + command.getName());
if (pluginCommand.equals(knownCommands.get(command.getName()))) {
knownCommands.remove(command.getName());
}
} catch (ReflectiveOperationException e) {
Logger.severe("Failed to unregister shop command");
Logger.severe(e);
}
}
private void loadDatabase() { private void loadDatabase() {
if (Config.DATABASE_TYPE.get() == DatabaseType.SQLITE) { if (Config.DATABASE_TYPE.get() == DatabaseType.SQLITE) {
database = new SQLite(this); database = new SQLite(this);
@ -158,6 +183,15 @@ public class ShopChestImpl extends ShopChest {
); );
} }
private void unloadDatabase() {
if (database != null) {
if (database instanceof SQLite) {
((SQLite) database).vacuum();
}
database.disconnect();
}
}
private void checkForUpdates() { private void checkForUpdates() {
if (!Config.CORE_ENABLE_UPDATE_CHECKER.get()) { if (!Config.CORE_ENABLE_UPDATE_CHECKER.get()) {
return; return;

View File

@ -2,6 +2,7 @@ package de.epiceric.shopchest.command;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.List; import java.util.List;
import java.util.Locale;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -29,7 +30,7 @@ public class ShopCommandImpl extends ShopCommand {
@Override @Override
public String getName() { public String getName() {
return Config.CORE_MAIN_COMMAND_NAME.get(); return Config.CORE_MAIN_COMMAND_NAME.get().toLowerCase(Locale.ENGLISH).trim();
} }
/** /**