Skip to content

Commit

Permalink
Update all commands.
Browse files Browse the repository at this point in the history
Commands now will have an option to change their call values.
  • Loading branch information
BONNe committed Sep 19, 2021
1 parent 5da823c commit 670513e
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 244 deletions.
8 changes: 2 additions & 6 deletions src/main/java/world/bentobox/challenges/ChallengesAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,8 @@ public void onEnable() {

if (this.settings.isUseCommonGUI())
{
new ChallengesGlobalPlayerCommand(this,
this.settings.getGlobalUserCommand(),
hookedGameModes);
new ChallengesGlobalAdminCommand(this,
this.settings.getGlobalAdminCommand(),
hookedGameModes);
new ChallengesGlobalPlayerCommand(this, hookedGameModes);
new ChallengesGlobalAdminCommand(this, hookedGameModes);
}

// Register the reset listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.config.SettingsUtils.GuiMode;
import world.bentobox.challenges.panel.user.GameModePanel;
import world.bentobox.challenges.utils.Utils;


/**
* This class provides all necessary thing to implement challenges user command
*/
public class ChallengesUserCommand extends CompositeCommand
public class ChallengesGlobalPlayerCommand extends CompositeCommand
{
/**
* Constructor that inits command with given string.
* @param addon Challenges Addon
* @param commands String that contains main command and its alias separated via whitespace.
* @param gameModeAddons List with GameModes where challenges addon operates.
*/
public ChallengesUserCommand(ChallengesAddon addon, String commands, List<GameModeAddon> gameModeAddons)
public ChallengesGlobalPlayerCommand(ChallengesAddon addon, List<GameModeAddon> gameModeAddons)
{
super(addon,
commands.split(" ")[0],
commands.split(" "));
addon.getChallengesSettings().getPlayerGlobalCommand().split(" ")[0],
addon.getChallengesSettings().getPlayerGlobalCommand().split(" "));
this.gameModeAddons = gameModeAddons;
this.addon = addon;
}


Expand Down Expand Up @@ -56,26 +55,30 @@ public boolean execute(User user, String label, List<String> args)
if (this.gameModeAddons.size() == 1)
{
this.gameModeAddons.get(0).getPlayerCommand().ifPresent(compositeCommand ->
user.performCommand(compositeCommand.getTopLabel() + " challenges"));
user.performCommand(compositeCommand.getTopLabel() + " " +
this.<ChallengesAddon>getAddon().getChallengesSettings().getPlayerMainCommand().split(" ")[0]));
return true;
}
else if (this.addon.getChallengesSettings().getUserGuiMode() == GuiMode.CURRENT_WORLD)
else if (this.<ChallengesAddon>getAddon().getChallengesSettings().getUserGuiMode() == GuiMode.CURRENT_WORLD)
{
// Find GameMode and run command
for (GameModeAddon addon : this.gameModeAddons)
{
if (addon.inWorld(user.getWorld()))
{
addon.getPlayerCommand().ifPresent(compositeCommand ->
user.performCommand(compositeCommand.getTopLabel() + " challenges"));
user.performCommand(compositeCommand.getTopLabel() + " " +
this.<ChallengesAddon>getAddon().getChallengesSettings().getPlayerMainCommand().split(" ")[0]));

return true;
}
}

Utils.sendMessage(user, user.getTranslation("general.errors.wrong-world"));
}
else if (this.addon.getChallengesSettings().getUserGuiMode() == GuiMode.GAMEMODE_LIST)
else if (this.<ChallengesAddon>getAddon().getChallengesSettings().getUserGuiMode() == GuiMode.GAMEMODE_LIST)
{
GameModePanel.open(this.addon,
GameModePanel.open(this.getAddon(),
this.getWorld(),
user,
this.gameModeAddons,
Expand All @@ -96,9 +99,4 @@ else if (this.addon.getChallengesSettings().getUserGuiMode() == GuiMode.GAMEMODE
* List with hooked GameMode addons.
*/
private final List<GameModeAddon> gameModeAddons;

/**
* Challenges addon for easier operations.
*/
private final ChallengesAddon addon;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import world.bentobox.challenges.panel.user.ChallengesPanel;


public class ChallengesCommand extends CompositeCommand
public class ChallengesPlayerCommand extends CompositeCommand
{
public static final String CHALLENGE_COMMAND = "challenges";


public ChallengesCommand(ChallengesAddon addon, CompositeCommand cmd)
public ChallengesPlayerCommand(ChallengesAddon addon, CompositeCommand cmd)
{
super(addon, cmd, CHALLENGE_COMMAND);
super(addon,
cmd,
addon.getChallengesSettings().getPlayerMainCommand().split(" ")[0],
addon.getChallengesSettings().getPlayerMainCommand().split(" "));
}


Expand All @@ -40,9 +40,10 @@ public boolean canExecute(User user, String label, List<String> args)
// Show admin better explanation.
if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges"))
{
String topLabel = getIWM().getAddon(this.getWorld())
.map(GameModeAddon::getAdminCommand)
.map(optionalAdminCommand -> optionalAdminCommand.map(ac -> ac.getTopLabel()).orElse(this.getTopLabel())).orElse(this.getTopLabel());
String topLabel = this.getIWM().getAddon(this.getWorld()).
map(GameModeAddon::getAdminCommand).
map(optionalAdminCommand -> optionalAdminCommand.map(CompositeCommand::getTopLabel).orElse(this.getTopLabel())).
orElse(this.getTopLabel());
user.sendMessage("challenges.errors.no-challenges-admin", "[command]", topLabel + " challenges");
}
else
Expand Down Expand Up @@ -87,7 +88,7 @@ public boolean execute(User user, String label, List<String> args)
@Override
public void setup()
{
this.setPermission(CHALLENGE_COMMAND);
this.setPermission("challenges");
this.setParametersHelp("challenges.commands.user.main.parameters");
this.setDescription("challenges.commands.user.main.description");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Optional;
import java.util.stream.Collectors;

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;
Expand All @@ -26,10 +25,12 @@ public class CompleteChallengeCommand extends CompositeCommand
* @param addon Challenges addon.
* @param cmd Parent Command.
*/
public CompleteChallengeCommand(Addon addon, CompositeCommand cmd)
public CompleteChallengeCommand(ChallengesAddon addon, CompositeCommand cmd)
{
super(addon, cmd, "complete");
this.addon = (ChallengesAddon) addon;
super(addon,
cmd,
addon.getChallengesSettings().getPlayerCompleteCommand().split(" ")[0],
addon.getChallengesSettings().getPlayerCompleteCommand().split(" "));
}


Expand Down Expand Up @@ -62,11 +63,11 @@ public boolean execute(User user, String label, List<String> args)
{
// Add world name back at the start
String challengeName = Utils.getGameMode(this.getWorld()) + "_" + args.get(0);
Challenge challenge = this.addon.getChallengesManager().getChallenge(challengeName);
Challenge challenge = this.<ChallengesAddon>getAddon().getChallengesManager().getChallenge(challengeName);

if (challenge != null)
{
int count = args.size() == 2 ? Integer.valueOf(args.get(1)) : 1;
int count = args.size() == 2 ? Integer.parseInt(args.get(1)) : 1;

boolean canMultipleTimes =
user.hasPermission(this.getPermission() + ".multiple");
Expand All @@ -77,7 +78,7 @@ public boolean execute(User user, String label, List<String> args)
count = 1;
}

return TryToComplete.complete(this.addon,
return TryToComplete.complete(this.getAddon(),
user,
challenge,
this.getWorld(),
Expand Down Expand Up @@ -113,10 +114,12 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
case 3:

// Create suggestions with all challenges that is available for users.
returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
filter(challenge -> challenge.startsWith(Utils.getGameMode(this.getWorld()) + "_")).
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).
collect(Collectors.toList()));
returnList.addAll(this.<ChallengesAddon>getAddon().getChallengesManager().getAllChallengesNames(this.getWorld()).
stream().
filter(challenge -> challenge.startsWith(Utils.getGameMode(this.getWorld()) + "_") ||
challenge.startsWith(Utils.getGameMode(this.getWorld()).toLowerCase() + "_")).
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).
collect(Collectors.toList()));
break;
case 4:
// Suggest a number of completions.
Expand All @@ -135,14 +138,4 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>

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


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

/**
* Variable that holds challenge addon. Single casting.
*/
private ChallengesAddon addon;
}

This file was deleted.

0 comments on commit 670513e

Please sign in to comment.