Skip to content

Commit

Permalink
Add admin reset command, that allows to reset player challenges (#141)
Browse files Browse the repository at this point in the history
Fix some bugs in complete command that did not display challenge list.
Add new lang parameters in en-US and lv-LV.
  • Loading branch information
BONNe authored and BuildTools committed Jun 23, 2019
1 parent d4bcc1e commit 5e0f051
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ public CompleteChallengeCommand(Addon addon, CompositeCommand cmd)
{
super(addon, cmd, "complete");
this.addon = (ChallengesAddon) addon;

if (this.addon.getChallengesManager().hasAnyChallengeData(this.getWorld()))
{
// Strip world name from all challenges
this.challenges = this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")).
collect(Collectors.toList());
}
}


Expand Down Expand Up @@ -109,28 +101,27 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
{
case 3:
// Create suggestions with all challenges that is available for users.

this.challenges.forEach(challenge -> {
returnList.addAll(Util.tabLimit(Collections.singletonList(challenge), lastString));
});
returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")).
collect(Collectors.toList()));

break;
case 4:
// Suggest a number of completions.
if (lastString.isEmpty() || lastString.matches("[0-9]*"))
{
returnList.addAll(Util.tabLimit(Collections.singletonList("<number>"), lastString));
returnList.add("<number>");
}

break;
default:
{
returnList.addAll(Util.tabLimit(Collections.singletonList("help"), lastString));
returnList.add("help");
break;
}
}

return Optional.of(returnList);
return Optional.of(Util.tabLimit(returnList, lastString));
}


Expand All @@ -142,9 +133,4 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
* Variable that holds challenge addon. Single casting.
*/
private ChallengesAddon addon;

/**
* This list contains all challenge IDs without a world name.
*/
private List<String> challenges;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public void setup()
// Defaults processing command
new DefaultsCommand(this.getAddon(), this);

// Defaults processing command
// Complete challenge command
new CompleteCommand(this.getAddon(), this);

// Reset challenge command
new ResetCommand(this.getAddon(), this);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ public CompleteCommand(Addon addon, CompositeCommand cmd)
{
super(addon, cmd, "complete");
this.addon = (ChallengesAddon) addon;

if (this.addon.getChallengesManager().hasAnyChallengeData(this.getWorld()))
{
// Strip world name from all challenges
this.challenges = this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")).
collect(Collectors.toList());
}
}


Expand Down Expand Up @@ -176,9 +168,9 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
break;
case 4:
// Create suggestions with all challenges that is available for users.
this.challenges.forEach(challenge -> {
returnList.addAll(Collections.singletonList(challenge));
});
returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")).
collect(Collectors.toList()));

break;
default:
Expand All @@ -200,9 +192,4 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
* Variable that holds challenge addon. Single casting.
*/
private ChallengesAddon addon;

/**
* This list contains all challenge IDs without a world name.
*/
private List<String> challenges;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package world.bentobox.challenges.commands.admin;


import java.util.*;
import java.util.stream.Collectors;

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;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.database.object.Challenge;
import world.bentobox.challenges.tasks.TryToComplete;


/**
* This command allows to reset challenges without a gui.
*/
public class ResetCommand extends CompositeCommand
{
/**
* Default constructor for Composite Command.
* @param addon Challenges addon.
* @param cmd Parent Command.
*/
public ResetCommand(Addon addon, CompositeCommand cmd)
{
super(addon, cmd, "reset");
this.addon = (ChallengesAddon) addon;
}


/**
* {@inheritDoc}
*/
@Override
public void setup()
{
this.setPermission("reset");
this.setParametersHelp("challenges.commands.admin.reset.parameters");
this.setDescription("challenges.commands.admin.reset.description");
}


/**
* {@inheritDoc}
*/
@Override
public boolean execute(User user, String label, List<String> args)
{
if (args.isEmpty())
{
if (user.isPlayer())
{
user.sendMessage("challenges.errors.no-name");
}
else
{
this.addon.logError("Missing parameters");
}
}
else if (args.size() < 2)
{
if (user.isPlayer())
{
user.sendMessage("challenges.errors.missing-arguments");
}
else
{
this.addon.logError("Missing parameters");
}
}
else if (!args.get(1).isEmpty())
{
UUID targetUUID = this.getPlayers().getUUID(args.get(0));

if (targetUUID == null)
{
if (user.isPlayer())
{
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
}
else
{
this.addon.logError("Unknonw player name " + args.get(0));
}

return false;
}

// Add world name back at the start

if (args.get(1).equals("all"))
{
this.addon.getChallengesManager().resetAllChallenges(targetUUID, this.getWorld(), user.getUniqueId());

if (user.isPlayer())
{
user.sendMessage("challenges.messages.admin.reset-all",
"[player]", User.getInstance(targetUUID).getName());
}
else
{
this.addon.log("All challenges for user " +
User.getInstance(targetUUID).getName() + " was reset!");
}

return true;
}
else
{
String challengeName = Util.getWorld(this.getWorld()).getName() + "_" + args.get(1);
Challenge challenge = this.addon.getChallengesManager().getChallenge(challengeName);

if (challenge != null)
{
if (this.addon.getChallengesManager().isChallengeComplete(targetUUID, this.getWorld(), challenge))
{
this.addon.getChallengesManager().resetChallenge(targetUUID, this.getWorld(), challenge, user.getUniqueId());

if (user.isPlayer())
{
user.sendMessage("challenges.messages.admin.reset",
"[name]", challenge.getFriendlyName(),
"[player]", User.getInstance(targetUUID).getName());
}
else
{
this.addon.log("Challenge " + challenge.getFriendlyName() + " was reset for player " +
User.getInstance(targetUUID).getName());
}
}
else
{
if (user.isPlayer())
{
user.sendMessage("challenges.messages.admin.not-completed");
}
else
{
this.addon.log("Challenge is not completed yet");
}
}

return true;
}
else
{
if (user.isPlayer())
{
user.sendMessage("challenges.errors.unknown-challenge");
}
else
{
this.addon.logError("Unknown challenge " + args.get(1));
}

return false;
}
}
}

this.showHelp(this, user);
return false;
}


/**
* {@inheritDoc}
*/
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args)
{
String lastString = args.get(args.size() - 1);

final List<String> returnList = new ArrayList<>();
final int size = args.size();

switch (size)
{
case 3:
// Create suggestions with all challenges that is available for users.

returnList.addAll(Util.getOnlinePlayerList(user));
break;
case 4:
// Create suggestions with all challenges that is available for users.
returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")).
collect(Collectors.toList()));

returnList.add("all");

break;
default:
{
returnList.addAll(Collections.singletonList("help"));
break;
}
}

return Optional.of(Util.tabLimit(returnList, lastString));
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------

/**
* Variable that holds challenge addon. Single casting.
*/
private ChallengesAddon addon;
}
8 changes: 7 additions & 1 deletion src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ challenges:
complete:
description: 'This command allows to complete challenge for player without GUI.'
parameters: '<player> <challenge_id>'
reset:
description: 'This command allows to reset challenge for player without GUI. If "challenge_id" is set to "all", then it will reset all challenges.'
parameters: '<player> <challenge_id>'
user:
main:
description: 'This method opens Challenges GUI.'
Expand Down Expand Up @@ -333,8 +336,11 @@ challenges:
you-added: 'You added one [thing] to the challenge'
challenge-created: '[challenge]&r created!'
complete-wipe: '&cHope you have backups, as you just empty all Challenges Addon databases!'
completed: '&2You complted challenge [name] for [player]!'
completed: '&2You completed challenge [name] for [player]!'
already-completed: '&2This challenge was already completed!'
reset: '&2You reset challenge [name] for [player]!'
reset-all: '&2All [player] challenges were reset!'
not-completed: '&2This challenge is not completed yet!'
you-completed-challenge: '&2You completed the [value] &r&2challenge!'
you-repeated-challenge: '&2You repeated the [value] &r&2challenge!'
you-repeated-challenge-multiple: '&2You repeated the [value] &r&2challenge [count] times!'
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/locales/lv-LV.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ challenges:
complete:
description: 'Šī komanda ļauj pabeigt uzdevumu spēlētājam.'
parameters: '<player> <challenge_id>'
reset:
description: 'Šī komanda ļauj atiestatīt uzdevumu spēlētājam. Ja "challenge_id" aizstāj ar "all", tad tiek atiestatīti visi uzdevumi.'
parameters: '<player> <challenge_id>'
user:
main:
description: 'Šī metode atver Uzdevumu logu.'
Expand Down Expand Up @@ -333,6 +336,9 @@ challenges:
complete-wipe: '&cCerams, ka tev ir saglabātas rezerves kopijas, jo tu tikko iztīrīji visas šī papildinājuma datubāzes!'
completed: '&2Tu pabeidzi uzdevumu [name] [player] vietā!'
already-completed: '&2Šīs uzdevums jau bija izpildīts!'
reset: '&2Tu atiestatīji uzdevumu [name] priekš [player]!'
reset-all: '&2Visi [player] uzdevumi ir atiesatīti!'
not-completed: '&2Šis uzdevums vēl nav izpildīts!'
you-completed-challenge: '&2Tu izpildīji [value] &r&2uzdevumu!'
you-repeated-challenge: '&2Tu atkārtoji [value] &r&2uzdevumu!'
you-repeated-challenge-multiple: '&2Tu atkārtoji [value] &r&2uzdevumu [count] reizes!'
Expand Down

0 comments on commit 5e0f051

Please sign in to comment.