Skip to content

Commit

Permalink
Implement completed challenge storage by island (#55).
Browse files Browse the repository at this point in the history
Rework methods that access to ChallengesPlayerData object. Methods that access to this object will be private and use UUID and String where possible.
Add public methods that could use previously mentioned methods to access ChallengesPlayerData object.
Implement these methods in all places.

Add ability to enable/disable island storage in config (no-gui for now).
Disable challenge GUI opening, if user does not have island.
  • Loading branch information
BONNe committed Feb 17, 2019
1 parent f9b9410 commit 4db59d6
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 220 deletions.
494 changes: 326 additions & 168 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion src/main/java/world/bentobox/challenges/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class Settings implements DataObject
@ConfigEntry(path = "free-challenges-first")
private boolean freeChallengesFirst = true;

@ConfigComment("")
@ConfigComment("This indicate if challenges data will be stored per island (true) or per player (false).")
@ConfigEntry(path = "store-island-data")
private boolean storeAsIslandData = false;

@ConfigComment("")
@ConfigComment("This allows to change lore description line length. By default it is 25, but some server")
@ConfigComment("owners may like it to be larger.")
Expand Down Expand Up @@ -105,7 +110,7 @@ public class Settings implements DataObject
* Configuration version
*/
@ConfigComment("")
private String configVersion = "v1.1";
private String configVersion = "v1.2";

// ---------------------------------------------------------------------
// Section: Methods
Expand Down Expand Up @@ -213,6 +218,16 @@ public String getLevelLoreMessage()
}


/**
* This method returns the storeAsIslandData object.
* @return the storeAsIslandData object.
*/
public boolean isStoreAsIslandData()
{
return storeAsIslandData;
}


/**
* This method sets the configVersion object value.
* @param configVersion the configVersion object new value.
Expand Down Expand Up @@ -312,4 +327,14 @@ public void setLoreLineLength(int loreLineLength)
{
this.loreLineLength = loreLineLength;
}


/**
* This method sets the storeAsIslandData object value.
* @param storeAsIslandData the storeAsIslandData object new value.
*/
public void setStoreAsIslandData(boolean storeAsIslandData)
{
this.storeAsIslandData = storeAsIslandData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,49 @@
import world.bentobox.challenges.panel.user.ChallengesGUI;


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

public ChallengesCommand(ChallengesAddon addon, CompositeCommand cmd) {

public ChallengesCommand(ChallengesAddon addon, CompositeCommand cmd)
{
super(addon, cmd, CHALLENGE_COMMAND);
}


@Override
public boolean execute(User user, String label, List<String> args) {
public boolean execute(User user, String label, List<String> args)
{
// Open up the challenges GUI
if (user.isPlayer()) {
new ChallengesGUI((ChallengesAddon) this.getAddon(),
this.getWorld(),
user,
this.getTopLabel(),
this.getPermissionPrefix()).build();
return true;
if (user.isPlayer())
{
if (this.getPlugin().getIslands().getIsland(this.getWorld(), user.getUniqueId()) != null)
{
new ChallengesGUI((ChallengesAddon) this.getAddon(),
this.getWorld(),
user,
this.getTopLabel(),
this.getPermissionPrefix()).build();
return true;
}
else
{
user.sendMessage("general.errors.no-island");
return false;
}
}
// Show help
showHelp(this, user);
return false;
}


@Override
public void setup() {
public void setup()
{
this.setPermission(CHALLENGE_COMMAND);
this.setParametersHelp("challenges.commands.user.parameters");
this.setParametersHelp("challen/ges.commands.user.parameters");
this.setDescription("challenges.commands.user.description");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public boolean execute(User user, String label, List<String> args) {
return false;
}
// Complete challenge
User target = User.getInstance(targetUUID);
manager.setChallengeComplete(target, this.manager.getChallenge(args.get(1)));
manager.setChallengeComplete(targetUUID, this.getWorld(), this.manager.getChallenge(args.get(1)));
user.sendMessage("general.success");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public boolean execute(User user, String label, List<String> args) {
return false;
}
// Complete challenge
User target = User.getInstance(targetUUID);
manager.resetChallenge(target, manager.getChallenge(args.get(1)));
manager.resetChallenge(targetUUID, this.getWorld(), manager.getChallenge(args.get(1)));
user.sendMessage("general.success");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@


import com.google.gson.annotations.Expose;
import org.bukkit.World;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.util.Util;

/**
* Stores the player's challenge situation
Expand Down Expand Up @@ -172,11 +167,10 @@ public void setLevelsDone(Set<String> levelsDone)
/**
* Resets all challenges and levels in world for this player
*
* @param world world which challenges must be reset.
* @param worldName world which challenges must be reset.
*/
public void reset(World world)
public void reset(String worldName)
{
String worldName = Util.getWorld(world).getName();
challengeStatus.keySet().removeIf(n -> n.startsWith(worldName));
challengesTimestamp.keySet().removeIf(n -> n.startsWith(worldName));
levelsDone.removeIf(n -> n.startsWith(worldName));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/world/bentobox/challenges/panel/CommonGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ protected List<String> generateChallengeDescription(Challenge challenge, Player
// Some values to avoid overchecking.
ChallengesManager manager = this.addon.getChallengesManager();

final boolean isCompletedOnce = manager.isChallengeComplete(user.getUniqueId(), challenge);
final boolean isCompletedOnce = manager.isChallengeComplete(user.getUniqueId(), world, challenge);
final long doneTimes = challenge.isRepeatable() ?
manager.getChallengeTimes(this.user, challenge) :
manager.getChallengeTimes(this.user, this.world, challenge) :
isCompletedOnce ? 0 : 1;
boolean isCompletedAll = isCompletedOnce && challenge.isRepeatable() &&
challenge.getMaxTimes() > 0 && doneTimes >= challenge.getMaxTimes();
Expand Down Expand Up @@ -677,7 +677,7 @@ protected List<String> generateLevelDescription(ChallengeLevel level, Player use
List<String> result = new ArrayList<>();

ChallengesManager manager = this.addon.getChallengesManager();
LevelStatus status = manager.getChallengeLevelStatus(user.getUniqueId(), level);
LevelStatus status = manager.getChallengeLevelStatus(user.getUniqueId(), this.world, level);

// Used to know if blocks, entities, items should be added after requirements and rewards.
char prevChar = ' ';
Expand All @@ -699,7 +699,7 @@ protected List<String> generateLevelDescription(ChallengeLevel level, Player use
if (!status.isComplete())
{
int doneChallengeCount = (int) level.getChallenges().stream().
filter(challenge -> this.addon.getChallengesManager().isChallengeComplete(user.getUniqueId(), challenge)).
filter(challenge -> this.addon.getChallengesManager().isChallengeComplete(user.getUniqueId(), world, challenge)).
count();

result.add(this.user.getTranslation("challenges.gui.level-description.completed-challenges-of",
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/world/bentobox/challenges/panel/TryToComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,15 @@ public ChallengeResult build()
}

// Mark as complete
this.manager.setChallengeComplete(this.user, this.challenge);
this.manager.setChallengeComplete(this.user, this.world, this.challenge);

if (!result.repeat)
{
ChallengeLevel level = this.manager.getLevel(this.challenge);

if (!this.manager.isLevelCompleted(this.user, level))
if (!this.manager.isLevelCompleted(this.user, this.world, level))
{
if (this.manager.validateLevelCompletion(this.user, level))
if (this.manager.validateLevelCompletion(this.user, this.world, level))
{
// Item rewards
for (ItemStack reward : level.getRewardItems())
Expand Down Expand Up @@ -318,7 +318,7 @@ public ChallengeResult build()
}
}

this.manager.setLevelComplete(this.user, level);
this.manager.setLevelComplete(this.user, this.world, level);
}
}
}
Expand Down Expand Up @@ -357,20 +357,20 @@ else if (!this.addon.getIslands().userIsOnIsland(this.user.getWorld(), this.user
}
// Check if user has unlocked challenges level.
else if (!this.challenge.getLevel().equals(ChallengesManager.FREE) &&
!this.manager.isLevelUnlocked(this.user, this.manager.getLevel(this.challenge.getLevel())))
!this.manager.isLevelUnlocked(this.user, this.world, this.manager.getLevel(this.challenge.getLevel())))
{
this.user.sendMessage("challenges.errors.challenge-level-not-available");
result = EMPTY_RESULT;
}
// Check max times
else if (this.challenge.isRepeatable() && this.challenge.getMaxTimes() > 0 &&
this.manager.getChallengeTimes(this.user, this.challenge) >= this.challenge.getMaxTimes())
this.manager.getChallengeTimes(this.user, this.world, this.challenge) >= this.challenge.getMaxTimes())
{
this.user.sendMessage("challenges.errors.not-repeatable");
result = EMPTY_RESULT;
}
// Check repeatability
else if (!this.challenge.isRepeatable() && this.manager.isChallengeComplete(this.user, this.challenge))
else if (!this.challenge.isRepeatable() && this.manager.isChallengeComplete(this.user, this.world, this.challenge))
{
this.user.sendMessage("challenges.errors.not-repeatable");
result = EMPTY_RESULT;
Expand Down Expand Up @@ -546,7 +546,7 @@ private ChallengeResult checkInventory()

// Return the result
return new ChallengeResult().setMeetsRequirements().setRepeat(
this.manager.isChallengeComplete(this.user, this.challenge));
this.manager.isChallengeComplete(this.user, this.world, this.challenge));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ private PanelItem createPlayerIcon(Player player)
{
int lineLength = this.addon.getChallengesSettings().getLoreLineLength();

if (this.addon.getIslands().hasIsland(this.world, player.getUniqueId()) ||
this.addon.getIslands().inTeam(this.world, player.getUniqueId()))
if (this.addon.getIslands().getIsland(this.world, player.getUniqueId()) != null)
{
return new PanelItemBuilder().name(player.getName()).icon(player.getName()).clickHandler(
(panel, user1, clickType, slot) -> {
Expand All @@ -190,7 +189,7 @@ private PanelItem createPlayerIcon(Player player)

for (Challenge challenge : manager.getAllChallenges(this.world))
{
if (!manager.isChallengeComplete(player.getUniqueId(), challenge))
if (!manager.isChallengeComplete(player.getUniqueId(), this.world, challenge))
{
challengeDescriptionMap.put(challenge, this.generateChallengeDescription(challenge, player));
}
Expand All @@ -199,7 +198,7 @@ private PanelItem createPlayerIcon(Player player)
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> {
if (status)
{
valueSet.forEach(challenge -> manager.setChallengeComplete(player.getUniqueId(), challenge));
valueSet.forEach(challenge -> manager.setChallengeComplete(player.getUniqueId(), this.world, challenge));
}

this.build();
Expand All @@ -210,7 +209,7 @@ private PanelItem createPlayerIcon(Player player)

for (Challenge challenge : manager.getAllChallenges(this.world))
{
if (manager.isChallengeComplete(player.getUniqueId(), challenge))
if (manager.isChallengeComplete(player.getUniqueId(), this.world, challenge))
{
challengeDescriptionMap.put(challenge, this.generateChallengeDescription(challenge, player));
}
Expand All @@ -219,7 +218,7 @@ private PanelItem createPlayerIcon(Player player)
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> {
if (status)
{
valueSet.forEach(challenge -> manager.resetChallenge(player.getUniqueId(), challenge));
valueSet.forEach(challenge -> manager.resetChallenge(player.getUniqueId(), this.world, challenge));
}

this.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ChallengesGUI(ChallengesAddon addon,
super(addon, world, user, topLabel, permissionPrefix);
this.challengesManager = this.addon.getChallengesManager();

this.levelStatusList = this.challengesManager.getChallengeLevelStatus(this.user, this.world);
this.levelStatusList = this.challengesManager.getAllChallengeLevelStatus(this.user, this.world);

for (LevelStatus levelStatus : this.levelStatusList)
{
Expand Down Expand Up @@ -148,7 +148,7 @@ private void addFreeChallenges(PanelBuilder panelBuilder, int firstItemIndex)
if (this.addon.getChallengesSettings().isRemoveCompleteOneTimeChallenges())
{
freeChallenges.removeIf(challenge -> !challenge.isRepeatable() &&
this.challengesManager.isChallengeComplete(this.user, challenge));
this.challengesManager.isChallengeComplete(this.user, this.world, challenge));
}

final int freeChallengesCount = freeChallenges.size();
Expand Down Expand Up @@ -218,7 +218,7 @@ private void addChallenges(PanelBuilder panelBuilder, int firstItemIndex)
if (this.addon.getChallengesSettings().isRemoveCompleteOneTimeChallenges())
{
challenges.removeIf(challenge -> !challenge.isRepeatable() &&
this.challengesManager.isChallengeComplete(this.user, challenge));
this.challengesManager.isChallengeComplete(this.user, this.world, challenge));
}

final int challengesCount = challenges.size();
Expand Down Expand Up @@ -366,7 +366,7 @@ private PanelItem getChallengeButton(Challenge challenge)
return true;
}).
glow(this.addon.getChallengesSettings().isAddCompletedGlow() &&
this.challengesManager.isChallengeComplete(this.user, challenge)).
this.challengesManager.isChallengeComplete(this.user, this.world, challenge)).
build();
}

Expand Down Expand Up @@ -413,7 +413,7 @@ else if (level.isUnlocked())
return true;
};
glow = this.addon.getChallengesSettings().isAddCompletedGlow() &&
this.challengesManager.isLevelCompleted(this.user, level.getLevel());
this.challengesManager.isLevelCompleted(this.user, this.world, level.getLevel());
}
else
{
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ add-completed-glow: true
# This indicate if free challenges must be at the start (true) or at the end (false) of list.
free-challenges-first: true
#
# This indicate if challenges data will be stored per island (true) or per player (false).
store-island-data: true
#
# This allows to change lore description line length. By default it is 25, but some server
# owners may like it to be larger.
lore-length: 25
Expand Down Expand Up @@ -65,4 +68,4 @@ disabled-gamemodes: []
#
uniqueId: config
#
configVersion: v1.1
configVersion: v1.2

0 comments on commit 4db59d6

Please sign in to comment.