Implement ability to import/generate default.json file.

Remove old unnecessary methods.

Improve default.json challenges.
This commit is contained in:
BONNe 2019-05-01 12:46:54 +03:00
parent 0418e64125
commit 93da0c714c
6 changed files with 3175 additions and 100 deletions

View File

@ -220,8 +220,7 @@ public class ChallengesImportManager
* @param world Target world.
* @return <code>true</code> if everything was successful, otherwise <code>false</code>.
*/
@SuppressWarnings("unused")
private boolean loadDefaultChallenges(User user, World world)
public boolean loadDefaultChallenges(User user, World world)
{
ChallengesManager manager = this.addon.getChallengesManager();
@ -299,13 +298,45 @@ public class ChallengesImportManager
/**
* Create method that can generate default challenge file from existing challenges in given world.
* This method will create default.json file in Challenges folder.
* @param user User who calls this method.
* @param world from which challenges must be stored.
* @param overwrite indicates if existing default.json file can be overwritten.
* @return <code>true</code> if everything was successful, otherwise <code>false</code>
*/
@SuppressWarnings("unused")
private void generateDefaultChallengeFile(World world)
public boolean generateDefaultChallengeFile(User user, World world, boolean overwrite)
{
File defaultFile = new File(this.addon.getDataFolder(), "default.json");
if (defaultFile.exists())
{
if (overwrite)
{
if (user.isPlayer())
{
user.sendMessage("challenges.messages.defaults-file-overwrite");
}
else
{
this.addon.logWarning("challenges.messages.defaults-file-overwrite");
}
defaultFile.delete();
}
else
{
if (user.isPlayer())
{
user.sendMessage("challenges.errors.defaults-file-exist");
}
else
{
this.addon.logWarning("challenges.errors.defaults-file-exist");
}
return false;
}
}
try
{
if (defaultFile.createNewFile())
@ -358,9 +389,28 @@ public class ChallengesImportManager
}
catch (IOException e)
{
this.addon.logError("Could not save json file: " + e.getMessage());
}
}
if (user.isPlayer())
{
user.sendMessage("challenges.errors.defaults-file-error");
}
this.addon.logError("Could not save json file: " + e.getMessage());
return false;
}
finally
{
if (user.isPlayer())
{
user.sendMessage("challenges.messages.defaults-file-completed", "[world]", world.getName());
}
else
{
this.addon.logWarning("challenges.messages.defaults-file-completed");
}
}
return true;
}
// ---------------------------------------------------------------------
@ -392,7 +442,7 @@ public class ChallengesImportManager
builder.disableHtmlEscaping();
this.addon = addon;
this.gson = builder.create();
this.gson = builder.setPrettyPrinting().create();
}

View File

@ -8,35 +8,44 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.panel.admin.AdminGUI;
public class Challenges extends CompositeCommand {
public class Challenges extends CompositeCommand
{
/**
* Admin command for challenges
*
* @param parent
*/
public Challenges(ChallengesAddon addon, CompositeCommand parent) {
public Challenges(ChallengesAddon addon, CompositeCommand parent)
{
super(addon, parent, "challenges");
}
@Override
public void setup() {
public void setup()
{
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.commands.admin.main.parameters");
this.setDescription("challenges.commands.admin.main.description");
// Register sub commands
new ImportCommand(getAddon(), this);
// new CompleteChallenge(getAddon(), this);
new ReloadChallenges(getAddon(), this);
new ResetChallenge(getAddon(), this);
//new ShowChallenges(getAddon(), this);
//new CreateChallenge(getAddon(), this);
// Register sub commands
// This method reloads challenges addon
new ReloadChallenges(getAddon(), this);
// Import ASkyBlock Challenges
new ImportCommand(getAddon(), this);
// Defaults processing command
new DefaultsCommand(this.getAddon(), this);
}
@Override
public boolean execute(User user, String label, List<String> args) {
public boolean execute(User user, String label, List<String> args)
{
// Open up the admin challenges GUI
if (user.isPlayer()) {
if (user.isPlayer())
{
new AdminGUI((ChallengesAddon) this.getAddon(),
this.getWorld(),
user,
@ -47,5 +56,4 @@ public class Challenges extends CompositeCommand {
}
return false;
}
}

View File

@ -1,77 +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
public class CompleteChallenge extends CompositeCommand {
private ChallengesManager manager;
/**
* Admin command to complete user challenges
* @param parent
*/
public CompleteChallenge(Addon addon, CompositeCommand parent) {
super(addon, parent, "complete");
}
@Override
public void setup() {
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.commands.admin.complete.parameters");
this.setDescription("challenges.commands.admin.complete.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.admin.complete.unknown-challenge");
return false;
}
// Complete challenge
manager.setChallengeComplete(targetUUID, this.getWorld(), this.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();
}
}

View File

@ -0,0 +1,165 @@
package world.bentobox.challenges.commands.admin;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.challenges.ChallengesAddon;
/**
* This method generates default challenges file.
*/
public class DefaultsCommand extends CompositeCommand
{
/**
* Constructor that inits generate defaults command.
*
* @param addon Addon that inits this command
* @param cmd Parent command
*/
public DefaultsCommand(Addon addon, CompositeCommand cmd)
{
super(addon, cmd, "defaults");
this.addon = (ChallengesAddon) addon;
}
/**
* {@inheritDoc}
*/
@Override
public void setup()
{
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.commands.admin.defaults.parameters");
this.setDescription("challenges.commands.admin.defaults.description");
// Register sub commands
// This method reloads challenges addon
new ImportCommand(this);
// Import ASkyBlock Challenges
new GenerateCommand(this);
}
/**
* {@inheritDoc}
*/
@Override
public boolean execute(User user, String label, List<String> args)
{
return this.showHelp(this, user);
}
// ---------------------------------------------------------------------
// Section: Private Classes
// ---------------------------------------------------------------------
/**
* This class allows to process import command.
*/
private class ImportCommand extends CompositeCommand
{
/**
* Default constructor for import method.
* @param parent composite command
*/
private ImportCommand(CompositeCommand parent)
{
super(DefaultsCommand.this.addon, parent, "import");
}
/**
* {@inheritDoc}
*/
@Override
public void setup()
{
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.commands.admin.defaults-import.parameters");
this.setDescription("challenges.commands.admin.defaults-import.description");
}
/**
* {@inheritDoc}
*/
@Override
public boolean execute(User user, String label, List<String> args)
{
return DefaultsCommand.this.addon.getImportManager().loadDefaultChallenges(user, this.getWorld());
}
}
/**
* This class allows to process generate command.
*/
private class GenerateCommand extends CompositeCommand
{
/**
* Default constructor for generate method.
* @param parent composite command
*/
private GenerateCommand(CompositeCommand parent)
{
super(DefaultsCommand.this.addon, parent, "generate");
}
/**
* {@inheritDoc}
*/
@Override
public void setup()
{
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.commands.admin.defaults-generate.parameters");
this.setDescription("challenges.commands.admin.defaults-generate.description");
}
/**
* {@inheritDoc}
*/
@Override
public boolean execute(User user, String label, List<String> args)
{
return DefaultsCommand.this.addon.getImportManager().generateDefaultChallengeFile(
user,
this.getWorld(),
!args.isEmpty() && args.get(0).equalsIgnoreCase("overwrite"));
}
/**
* {@inheritDoc}
*/
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args)
{
String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : "";
return Optional.of(Util.tabLimit(Collections.singletonList("overwrite"), lastArg));
}
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* Holds challenges addon as variable.
*/
private ChallengesAddon addon;
}

File diff suppressed because one or more lines are too long

View File

@ -33,6 +33,15 @@ challenges:
show:
description: 'This method prints in chat all challenges that exist in world.'
parameters: ''
defaults:
description: 'This method shows subcommands that allows to import/export default challenges.'
parameters: '[command]'
defaults-import:
description: 'This method allows to import default challenges.'
parameters: ''
defaults-generate:
description: 'This method allows to export existing challenges into default.json file.'
parameters: '[overwrite] - allows to overwrite existing file.'
user:
description: 'This method opens Challenges GUI.'
parameters: ''
@ -299,6 +308,8 @@ challenges:
load-skipping: '"[value]" already exists - skipping'
load-overwriting: 'Overwriting "[value]"'
load-add: 'Adding new object: [value]'
defaults-file-overwrite: 'default.json exists. It will be overwritten.'
defaults-file-completed: 'defaults.json file is populated with challenges from [world]!'
errors:
no-name: '&cMissing challenge name'
unknown-challenge: '&cUnknown challenge'
@ -324,6 +335,9 @@ challenges:
load-error: '&cError: Cannot load [value].'
no-rank: "&cYou do not have rank to do that."
cannot-remove-items: '&cSome items cannot be removed from inventory!'
exist-challenges-or-levels: '&cIn your world already exist challenges. Cannot proceed!'
defaults-file-exist: '&cdefault.json already exists. Use overwrite mode to replace it!'
defaults-file-error: '&cThere was an error while creating default.json file! Check console!'
protection:
flags:
CHALLENGES_ISLAND_PROTECTION:
@ -333,4 +347,4 @@ protection:
description: "&5&oThis allows to enable/disable\n&5&orequirement for players to\n&5&obe on their island to\n&5&ocomplete a challenge."
name: "Challenges Island limitation"
hint: "No challenges outside island"
version: 10
version: 11