Implement Challenges protection flags.

Create a new Flag Challenges Protection (#93), that allows to define which users can complete challenges on island.
Create a new Flag Challenges Island Limitation (#95), that allows to disable check for users to be on their islands for completing challenge.
This commit is contained in:
BONNe1704 2019-02-19 17:53:03 +02:00
parent 8649409cbb
commit 9328f434cc
3 changed files with 46 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package world.bentobox.challenges;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -9,7 +10,9 @@ import java.util.Optional;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.Config; import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.hooks.VaultHook; import world.bentobox.bentobox.hooks.VaultHook;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.challenges.commands.ChallengesCommand; import world.bentobox.challenges.commands.ChallengesCommand;
import world.bentobox.challenges.commands.ChallengesUserCommand; import world.bentobox.challenges.commands.ChallengesUserCommand;
import world.bentobox.challenges.commands.admin.Challenges; import world.bentobox.challenges.commands.admin.Challenges;
@ -68,6 +71,21 @@ public class ChallengesAddon extends Addon {
*/ */
private static final String PERMISSION_PREFIX = "addon"; private static final String PERMISSION_PREFIX = "addon";
/**
* This flag allows to complete challenges in any part of the world. It will not limit
* player to their island. Useful for skygrid without protection flags.
*/
public static Flag CHALLENGES_WORLD_PROTECTION =
new Flag.Builder("CHALLENGES_WORLD_PROTECTION", Material.GRASS_BLOCK).type(Flag.Type.WORLD_SETTING).defaultSetting(true).build();
/**
* This flag allows to define which users can complete challenge. F.e. it can be set
* that only Island owner can complete challenge.
* By default it is set to Visitor.
*/
public static Flag CHALLENGES_ISLAND_PROTECTION =
new Flag.Builder("CHALLENGES_ISLAND_PROTECTION", Material.COMMAND_BLOCK).defaultRank(RanksManager.VISITOR_RANK).build();
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Methods // Section: Methods
@ -121,6 +139,9 @@ public class ChallengesAddon extends Addon {
new Challenges(this, gameModeAddon.getAdminCommand().get()); new Challenges(this, gameModeAddon.getAdminCommand().get());
this.hooked = true; this.hooked = true;
} }
CHALLENGES_WORLD_PROTECTION.addGameModeAddon(gameModeAddon);
CHALLENGES_ISLAND_PROTECTION.addGameModeAddon(gameModeAddon);
} }
}); });
@ -170,6 +191,10 @@ public class ChallengesAddon extends Addon {
this.registerListener(new ResetListener(this)); this.registerListener(new ResetListener(this));
// Register the autosave listener. // Register the autosave listener.
this.registerListener(new SaveListener(this)); this.registerListener(new SaveListener(this));
// Register Flags
this.getPlugin().getFlagsManager().registerFlag(CHALLENGES_ISLAND_PROTECTION);
this.getPlugin().getFlagsManager().registerFlag(CHALLENGES_WORLD_PROTECTION);
} else { } else {
this.logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!"); this.logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!");
this.setState(State.DISABLED); this.setState(State.DISABLED);

View File

@ -350,11 +350,20 @@ public class TryToComplete
result = EMPTY_RESULT; result = EMPTY_RESULT;
} }
// Player is not on island // Player is not on island
else if (!this.addon.getIslands().userIsOnIsland(this.user.getWorld(), this.user)) else if (ChallengesAddon.CHALLENGES_WORLD_PROTECTION.isSetForWorld(this.world) &&
!this.addon.getIslands().userIsOnIsland(this.user.getWorld(), this.user))
{ {
this.user.sendMessage("challenges.errors.not-on-island"); this.user.sendMessage("challenges.errors.not-on-island");
result = EMPTY_RESULT; result = EMPTY_RESULT;
} }
// Check player permission
else if (!this.addon.getIslands().getIslandAt(this.user.getLocation()).
map(i -> i.isAllowed(this.user, ChallengesAddon.CHALLENGES_ISLAND_PROTECTION)).
orElse(false))
{
this.user.sendMessage("challenges.errors.no-rank");
result = EMPTY_RESULT;
}
// Check if user has unlocked challenges level. // Check if user has unlocked challenges level.
else if (!this.challenge.getLevel().equals(ChallengesManager.FREE) && else if (!this.challenge.getLevel().equals(ChallengesManager.FREE) &&
!this.manager.isLevelUnlocked(this.user, this.world, this.manager.getLevel(this.challenge.getLevel()))) !this.manager.isLevelUnlocked(this.user, this.world, this.manager.getLevel(this.challenge.getLevel())))

View File

@ -307,4 +307,14 @@ challenges:
import-no-file: '&cCould not find challenges.yml file to import!' import-no-file: '&cCould not find challenges.yml file to import!'
no-load: '&cError: Could not load challenges.yml. [message]' no-load: '&cError: Could not load challenges.yml. [message]'
load-error: '&cError: Cannot load [value].' load-error: '&cError: Cannot load [value].'
version: 6 no-rank: "&cYou do not have rank to do that."
protection:
flags:
CHALLENGES_ISLAND_PROTECTION:
description: "&5&oToggle who can\n&5&ocomplete challenges"
name: "Challenges protection"
CHALLENGES_WORLD_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: 7