Improve addon reload method.

Now it will call also manager.reload() that clears cache and resets database links.
Improve ReloadCommand. Now it will be able to reload both ways (soft way that clears only cache, and hard way that reassigns database connection) with reload command.
This commit is contained in:
BONNe 2019-05-01 17:10:05 +03:00
parent 93da0c714c
commit f0b156de31
4 changed files with 60 additions and 97 deletions

View File

@ -225,10 +225,12 @@ public class ChallengesAddon extends Addon {
@Override
public void onReload()
{
if (this.hooked) {
this.challengesManager.save();
super.onReload();
if (this.hooked)
{
this.loadSettings();
this.challengesManager.reload();
this.getLogger().info("Challenges addon reloaded.");
}
}

View File

@ -127,6 +127,13 @@ public class ChallengesManager
{
this.challengeCacheData.clear();
this.levelCacheData.clear();
if (!this.playerCacheData.isEmpty())
{
// store player data before cleaning.
this.savePlayersData();
}
this.playerCacheData.clear();
this.addon.getLogger().info("Loading challenges...");
@ -143,6 +150,12 @@ public class ChallengesManager
*/
public void reload()
{
if (!this.playerCacheData.isEmpty())
{
// store player data before cleaning.
this.savePlayersData();
}
this.addon.getLogger().info("Reloading challenges...");
this.challengeDatabase = new Database<>(addon, Challenge.class);

View File

@ -8,36 +8,65 @@ import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
public class ReloadChallenges extends CompositeCommand {
private ChallengesManager manager;
/**
* This class allows to reload challenges addon.
*/
public class ReloadChallenges extends CompositeCommand
{
/**
* Admin command to complete user challenges
* Admin command to reloads challenges addon.
* @param parent
*/
public ReloadChallenges(Addon addon, CompositeCommand parent) {
public ReloadChallenges(Addon addon, CompositeCommand parent)
{
super(addon, parent, "reload");
this.manager = ((ChallengesAddon) getAddon()).getChallengesManager();
}
/**
* {@inheritDoc}
*/
@Override
public void setup() {
public void setup()
{
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.commands.admin.reload.parameters");
this.setDescription("challenges.commands.admin.reload.description");
manager = ((ChallengesAddon)getAddon()).getChallengesManager();
}
/**
* {@inheritDoc}
*/
@Override
public boolean execute(User user, String label, List<String> args) {
if (!args.isEmpty()) {
// Show help
showHelp(this, user);
public boolean execute(User user, String label, List<String> args)
{
if (args.isEmpty())
{
this.manager.load();
user.sendMessage("general.success");
return true;
}
else if (args.get(0).equalsIgnoreCase("hard"))
{
this.manager.reload();
user.sendMessage("general.success");
return true;
}
else
{
this.showHelp(this, user);
return false;
}
manager.load();
user.sendMessage("general.success");
return true;
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
private ChallengesManager manager;
}

View File

@ -1,81 +0,0 @@
package world.bentobox.challenges.commands.admin;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.ChallengesManager;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
/**
* @deprecated Challenges can be reset via GUI.
*/
@Deprecated
public class ResetChallenge extends CompositeCommand {
private ChallengesManager manager;
/**
* Admin command to complete user challenges
* @param parent
*/
public ResetChallenge(Addon addon, CompositeCommand parent) {
super(addon, parent, "reset");
}
@Override
public void setup() {
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.commands.admin.reset.parameters");
this.setDescription("challenges.commands.admin.reset.description");
manager = ((ChallengesAddon)getAddon()).getChallengesManager();
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.size() != 2) {
// Show help
showHelp(this, user);
return false;
}
// Get target player
UUID targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
if (!getPlugin().getIslands().hasIsland(getWorld(), targetUUID)) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
// Check for valid challenge name
if (!manager.containsChallenge(args.get(1))) {
user.sendMessage("challenges.errors.unknown-challenge");
return false;
}
// Complete challenge
manager.resetChallenge(targetUUID, this.getWorld(), manager.getChallenge(args.get(1)), user.getUniqueId());
user.sendMessage("general.success");
return true;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (args.size() == 3) {
// Online players
return Optional.of(Util.tabLimit(new ArrayList<>(Util.getOnlinePlayerList(user)), lastArg));
} else if (args.size() == 4) {
// Challenges in this world
return Optional.of(Util.tabLimit(manager.getAllChallengesNames(getWorld()), lastArg));
}
return Optional.empty();
}
}