Skip to content

Commit

Permalink
Merge pull request #374 from BentoBoxWorld/permission_waiver
Browse files Browse the repository at this point in the history
Add permission [gamemode].challenges.waiver-add.x to addon
  • Loading branch information
tastybento authored Feb 12, 2025
2 parents dc7309b + 005fc33 commit 0fcdb06
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public ChallengeLevel()

@ConfigComment("")
@ConfigComment("The number of undone challenges that can be left on this level before")
@ConfigComment("unlocking next level.")
@ConfigComment("unlocking next level. Give players more with the permission")
@ConfigComment("[gamemode].challenges.waiver-add.x where x is a number")
@Expose
private int waiverAmount = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1121,8 +1121,9 @@ private void resetAllChallenges(@NonNull String storageDataID, @NonNull String g
* @return Level status - how many challenges still to do on which level
*/
@NonNull
private List<LevelStatus> getAllChallengeLevelStatus(String storageDataID, String gameMode)
private List<LevelStatus> getAllChallengeLevelStatus(User user, World world, String gameMode)
{
String storageDataID = this.getDataUniqueID(user, Util.getWorld(world));
this.addPlayerData(storageDataID);
ChallengesPlayerData playerData = this.playerCacheData.get(storageDataID);

Expand All @@ -1135,6 +1136,14 @@ private List<LevelStatus> getAllChallengeLevelStatus(String storageDataID, Strin
int doneChallengeCount = 0;
boolean previousUnlocked = true;

// Get user's waiver add from permission
// Get per-user waiver amount
int waiverAdd = user
.getPermissionValue(addon.getPlugin().getIWM().getPermissionPrefix(world) + "challenges.waiver-add", 0);
if (waiverAdd < 0) {
waiverAdd = 0;
}

// For each challenge level, check how many the storageDataID has done
for (ChallengeLevel level : challengeLevelList)
{
Expand All @@ -1147,7 +1156,7 @@ private List<LevelStatus> getAllChallengeLevelStatus(String storageDataID, Strin
this.getLevelChallenges(previousLevel);

int challengesToDo = previousLevel == null ? 0 :
(previousChallengeList.size() - doneChallengeCount - previousLevel.getWaiverAmount());
(previousChallengeList.size() - doneChallengeCount - (previousLevel.getWaiverAmount() + waiverAdd));

List<Challenge> challengeList = this.getLevelChallenges(level);

Expand Down Expand Up @@ -1177,14 +1186,15 @@ private List<LevelStatus> getAllChallengeLevelStatus(String storageDataID, Strin

/**
* This method returns LevelStatus object for given challenge level.
* @param storageDataID User which level status must be acquired.
* @param user User which level status must be acquired.
* @param world World where level is living.
* @param level Level which status must be calculated.
* @return LevelStatus of given level.
*/
@Nullable
private LevelStatus getChallengeLevelStatus(@NonNull String storageDataID, World world, @NonNull ChallengeLevel level)
private LevelStatus getChallengeLevelStatus(User user, World world, @NonNull ChallengeLevel level)
{
String storageDataID = this.getDataUniqueID(user.getUniqueId(), Util.getWorld(world));
this.addPlayerData(storageDataID);
ChallengesPlayerData playerData = this.playerCacheData.get(storageDataID);

Expand Down Expand Up @@ -1651,11 +1661,9 @@ public boolean isLevelCompleted(User user, World world, ChallengeLevel level)
*/
public boolean isLevelUnlocked(User user, World world, ChallengeLevel level)
{
String storageDataID = this.getDataUniqueID(user, Util.getWorld(world));
this.addPlayerData(storageDataID);

return this.islandWorldManager.getAddon(world).filter(gameMode -> this.getAllChallengeLevelStatus(storageDataID, gameMode.getDescription().getName()).
stream().
return this.islandWorldManager.getAddon(world).filter(gameMode -> this
.getAllChallengeLevelStatus(user, world, gameMode.getDescription().getName())
.stream().
filter(LevelStatus::isUnlocked).
anyMatch(lv -> lv.getLevel().equals(level))).
isPresent();
Expand Down Expand Up @@ -1709,7 +1717,8 @@ public boolean validateLevelCompletion(User user, World world, ChallengeLevel le
@Nullable
public LevelStatus getChallengeLevelStatus(UUID uniqueId, World world, ChallengeLevel level)
{
return this.getChallengeLevelStatus(this.getDataUniqueID(uniqueId, Util.getWorld(world)), world, level);
User user = User.getInstance(uniqueId);
return this.getChallengeLevelStatus(user, world, level);
}


Expand All @@ -1725,9 +1734,8 @@ public List<LevelStatus> getAllChallengeLevelStatus(User user, World world)
{
return this.islandWorldManager.getAddon(world).map(gameMode ->
this.getAllChallengeLevelStatus(
this.getDataUniqueID(user, Util.getWorld(world)),
gameMode.getDescription().getName())).
orElse(Collections.emptyList());
user, world, gameMode.getDescription().getName()))
.orElse(Collections.emptyList());
}


Expand Down
19 changes: 16 additions & 3 deletions src/main/java/world/bentobox/challenges/panel/CommonPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.stream.Collectors;

import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;
Expand Down Expand Up @@ -743,10 +742,17 @@ protected List<String> generateLevelDescription(ChallengeLevel level) {
// my eye :)
// Get status in single string
String status = "";
// Get per-user waiver amount
int waiverAdd = user.getPermissionValue(
addon.getPlugin().getIWM().getPermissionPrefix(world) + "challenges.waiver-add", 0);
if (waiverAdd < 0) {
waiverAdd = 0;
}
waiverAdd += level.getWaiverAmount();
// Get requirements in single string
String waiver = this.manager.isLastLevel(level, this.world) ? ""
: this.user.getTranslationOrNothing(reference + "waiver", "[number]",
String.valueOf(level.getWaiverAmount()));
String.valueOf(waiverAdd));
// Get rewards in single string
String rewards = this.generateReward(level);

Expand Down Expand Up @@ -776,11 +782,18 @@ protected List<String> generateLevelDescription(LevelStatus levelStatus, User us
// my eye :)
// Get status in single string
String status = this.generateLevelStatus(levelStatus);
// Get per-user waiver amount
int waiverAdd = user
.getPermissionValue(addon.getPlugin().getIWM().getPermissionPrefix(world) + "challenges.waiver-add", 0);
if (waiverAdd < 0) {
waiverAdd = 0;
}
waiverAdd += level.getWaiverAmount();
// Get requirements in single string
String waiver = this.manager.isLastLevel(level, this.world) || !levelStatus.isUnlocked()
|| levelStatus.isComplete() ? ""
: this.user.getTranslationOrNothing(reference + "waiver", "[number]",
String.valueOf(level.getWaiverAmount()));
String.valueOf(waiverAdd));
// Get rewards in single string
String rewards = !levelStatus.isUnlocked() ? "" : this.generateReward(level);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ private PanelItem createButton(Button button)
}
case WAIVER_AMOUNT -> {
description.add(this.user.getTranslation(reference + "value",
Constants.PARAMETER_NUMBER, String.valueOf(this.challengeLevel.getWaiverAmount())));
Constants.PARAMETER_NUMBER, String.valueOf(this.challengeLevel.getWaiverAmount())));

icon = new ItemStack(Material.HOPPER, Math.max(1, this.challengeLevel.getWaiverAmount()));
clickHandler = (panel, user, clickType, i) -> {
Expand Down

0 comments on commit 0fcdb06

Please sign in to comment.