Skip to content

Commit

Permalink
Fixes biome "unlocking" on purchase.
Browse files Browse the repository at this point in the history
The bug was in "buy" command, especially with a custom gui's where players get biome with `buy` command. In situations, where biome was added after player re-logged, it was not marked as "unlocked", because validation happens only on login and build-in gui opening. This change will add a new validation that happens inside 'buy' command, and will unlock biome if it is possible.

Fixes #105
  • Loading branch information
BONNe committed Aug 22, 2022
1 parent 026b970 commit 9c9bbd0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,43 @@ public boolean execute(User user, String label, List<String> args)
return false;
}

if (!data.isUnlocked(biomesObject))
{
// Check biome unlock status.
if (addonManager.canUnlockBiome(data, island, biomesObject))
{
// Unlock biome as it is marked as valid.
addonManager.unlockBiome(data, user, island, biomesObject);
}
else
{
// Do not allow to buy non-unlocked biomes.
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "biome-not-unlocked",
Constants.PARAMETER_BIOME, biomesObject.getFriendlyName()));
return false;
}
}

if (!addonManager.hasPriceSet(biomesObject))
{
// If price is not set check if addon should send notification to the user.
if (!this.<BiomesAddon>getAddon().getSettings().isNotifyUnlockedBiomes())
{
// Notify user that biome is available if notify on unlock is disabled.
Utils.sendUnlockMessage(user.getUniqueId(),
island,
biomesObject,
this.getAddon(),
true);
}

return true;
}

if (addonManager.canPurchaseBiome(user, island, data, biomesObject))
{
// Purchase biome.
addonManager.purchaseBiome(user, island, data, biomesObject);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,41 +708,54 @@ else if (islandData.getIslandBundle() != null &&


/**
* This method checks if given user can apply given biome.
* This method checks if biome can be unlocked based on biome object and island data.
* This method is silent, it does not report missing stuff.
*
* @param user User who will pay for activating.
* @param islandData Data that stores island biomes.
* @param biomesObject Biome that need to be changed.
* @return {@code true} if can apply, {@code false} otherwise.
* @param dataObject DataObject where all data will be saved.
* @param island Island that need to unlock Biome.
* @param biomesObject Biome that must be unlocked.
*
* @return {@code true} if biome can be unlocked, {@code false} otherwise.
*/
public boolean canApplyBiome(@NotNull User user,
@NotNull BiomesIslandDataObject islandData,
@NotNull BiomesObject biomesObject)
public boolean canUnlockBiome(BiomesIslandDataObject dataObject, Island island, BiomesObject biomesObject)
{
if (!islandData.getUnlockedBiomes().contains(biomesObject.getUniqueId()))
if (!biomesObject.isValid() || !biomesObject.isDeployed())
{
// Generator is not unlocked. Return false.
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "biome-not-unlocked",
Constants.PARAMETER_BIOME, biomesObject.getFriendlyName()));

// Fast exit. Biome object that is not valid and is not deployed cannot be unlocked.
return false;
}
else if (!islandData.getPurchasedBiomes().contains(biomesObject.getUniqueId()) &&
this.addon.isEconomyProvided() &&
biomesObject.getUnlockCost() > 0)

// Update owner bundle, as it may influence island generators.
this.updateOwnerBundle(island, dataObject);

// Check if biome is part of available biomes.
if (!this.getIslandBiomes(island.getWorld(), dataObject).contains(biomesObject))
{
// Generator is not purchased. Return false.
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "biome-not-purchased",
Constants.PARAMETER_BIOME, biomesObject.getFriendlyName()));
// Biome is not in island bundle list.
return false;
}

// Get island level from the addon.
final long islandLevel = this.getIslandLevel(island);
final User owner = island.getOwner() == null ? null : User.getInstance(island.getOwner());

if (biomesObject.getUnlockLevel() > islandLevel)
{
// Not allowed by level.
return false;
}
else

if (!biomesObject.getUnlockPermissions().isEmpty() &&
(owner == null ||
!owner.isOnline() ||
!Utils.matchAllPermissions(owner, biomesObject.getUnlockPermissions())))
{
return true;
// If permissions are set, then biome unlock status can be validated only if owner is online.
return false;
}

// More unlocking stuff can be added here.
return true;
}


Expand Down Expand Up @@ -981,7 +994,7 @@ public void purchaseBiome(@NotNull User user,
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "biome-purchased",
Constants.PARAMETER_BIOME, biomesObject.getFriendlyName()));
islandData.getPurchasedBiomes().add(biomesObject.getUniqueId());
islandData.purchaseBiome(biomesObject.getUniqueId());

// Save object.
this.saveIslandData(islandData);
Expand Down Expand Up @@ -1485,9 +1498,22 @@ public long getIslandLevel(User user)
public boolean isPurchased(BiomesIslandDataObject islandData, BiomesObject biomesObject)
{
return islandData.isPurchased(biomesObject) ||
islandData.isUnlocked(biomesObject) &&
biomesObject.getUnlockItems().isEmpty() &&
(!this.addon.isEconomyProvided() || biomesObject.getUnlockCost() == 0);
islandData.isUnlocked(biomesObject) && !this.hasPriceSet(biomesObject);
}


/**
* This method returns if biome has price set or it should be given for free.
* @param biomesObject Biome object that need to be checked.
* @return {@code true} if biome has price set, {@code false} otherwise.
*/
public boolean hasPriceSet(BiomesObject biomesObject)
{
// If unlock items are not set and/or there are no unlock cost associated to the biome, then
// it is purchased by default.

return !biomesObject.getUnlockItems().isEmpty() ||
this.addon.isEconomyProvided() && biomesObject.getUnlockCost() != 0;
}


Expand Down

0 comments on commit 9c9bbd0

Please sign in to comment.