Skip to content

Commit

Permalink
Add validation methods to challenge and challengeLevel.
Browse files Browse the repository at this point in the history
Do not load into local cache invalid data. Add error warnings about it.
  • Loading branch information
BONNe committed Jul 26, 2020
1 parent 7060799 commit 504c0b4
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 30 deletions.
22 changes: 22 additions & 0 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ public boolean loadChallenge(@NonNull Challenge challenge,
return false;
}

if (!challenge.isValid())
{
if (!silent)
{
user.sendMessage("challenges.errors.invalid-challenge", "[challenge]", challenge.getUniqueId());
}

this.addon.logWarning("Data for challenge `" + challenge.getUniqueId() + "` is not valid. It could be NULL element in item-stack!");
return false;
}

if (this.challengeCacheData.containsKey(challenge.getUniqueId()))
{
if (!overwrite)
Expand Down Expand Up @@ -352,6 +363,17 @@ public boolean loadLevel(@NonNull ChallengeLevel level,
return false;
}

if (!level.isValid())
{
if (!silent)
{
user.sendMessage("challenges.errors.invalid-level", "[level]", level.getUniqueId());
}

this.addon.logWarning("Data for level `" + level.getUniqueId() + "` is not valid. It could be NULL element in item-stack!");
return false;
}

if (!this.isValidLevel(level))
{
if (user != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package world.bentobox.challenges.database.object;


import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import org.bukkit.Material;
Expand All @@ -18,6 +13,7 @@
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.JsonAdapter;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;
import world.bentobox.challenges.database.object.adapters.EntityCompatibilityAdapter;
Expand Down Expand Up @@ -1104,6 +1100,32 @@ public boolean equals(Object obj)
}


/**
* This method checks if variable values are valid for current level.
* @return {@code true} if all object values are valid, {@code false} otherwise.
*/
public boolean isValid()
{
return this.uniqueId != null &&
!this.uniqueId.isEmpty() &&
this.friendlyName != null &&
this.description != null &&
this.icon != null &&
this.challengeType != null &&
this.environment != null &&
this.level != null &&

this.requirements.isValid() &&

this.rewardText != null &&
this.rewardItems.stream().noneMatch(Objects::isNull) &&
this.rewardCommands != null &&

this.repeatRewardText != null &&
this.repeatItemReward.stream().noneMatch(Objects::isNull) &&
this.repeatRewardCommands != null;
}

/**
* Clone method that returns clone of current challenge.
* @return Challenge that is cloned from current object.
Expand All @@ -1114,13 +1136,8 @@ public Challenge clone()
Challenge clone;

try
{
clone = (Challenge) super.clone();
}
catch (CloneNotSupportedException e)
{
clone = new Challenge();

clone.setUniqueId(this.uniqueId);
clone.setFriendlyName(this.friendlyName);
clone.setDeployed(this.deployed);
Expand All @@ -1133,7 +1150,9 @@ public Challenge clone()
clone.setRemoveWhenCompleted(this.removeWhenCompleted);
clone.setRequirements(this.requirements.clone());
clone.setRewardText(this.rewardText);
clone.setRewardItems(this.rewardItems.stream().map(ItemStack::clone).
clone.setRewardItems(
this.rewardItems.stream().
map(ItemStack::clone).
collect(Collectors.toCollection(() -> new ArrayList<>(this.rewardItems.size()))));
clone.setRewardExperience(this.rewardExperience);
clone.setRewardMoney(this.rewardMoney);
Expand All @@ -1142,11 +1161,20 @@ public Challenge clone()
clone.setRepeatRewardText(this.repeatRewardText);
clone.setMaxTimes(this.maxTimes);
clone.setRepeatExperienceReward(this.repeatExperienceReward);
clone.setRepeatItemReward(this.repeatItemReward.stream().map(ItemStack::clone).
clone.setRepeatItemReward(
this.repeatItemReward.stream().
map(ItemStack::clone).
collect(Collectors.toCollection(() -> new ArrayList<>(this.repeatItemReward.size()))));
clone.setRepeatMoneyReward(this.repeatMoneyReward);
clone.setRepeatRewardCommands(new ArrayList<>(this.repeatRewardCommands));
}
catch (Exception e)
{
BentoBox.getInstance().logError("Failed to clone Challenge " + this.uniqueId);
BentoBox.getInstance().logStacktrace(e);
clone = this;
this.deployed = false;
}

return clone;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package world.bentobox.challenges.database.object;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import com.google.gson.annotations.Expose;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.ConfigComment;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;
import world.bentobox.challenges.ChallengesManager;


/**
* Represent a challenge level
* @author tastybento
Expand Down Expand Up @@ -520,22 +519,36 @@ public boolean equals(Object obj)
}


/**
* This method checks if variable values are valid for current level.
* @return {@code true} if all object values are valid, {@code false} otherwise.
*/
public boolean isValid()
{
return this.uniqueId != null &&
!this.uniqueId.isEmpty() &&
this.friendlyName != null &&
this.challenges != null &&
this.icon != null &&
this.world != null &&
this.unlockMessage != null &&
this.rewardText != null &&
this.rewardItems.stream().noneMatch(Objects::isNull) &&
this.rewardCommands != null;
}


/**
* Clone method that returns clone of current challengeLevel.
* @return ChallengeLevel that is cloned from current object.
*/
@Override
public ChallengeLevel clone()
{
ChallengeLevel clone;
ChallengeLevel clone = new ChallengeLevel();

try
{
clone = (ChallengeLevel) super.clone();
}
catch (CloneNotSupportedException e)
{
clone = new ChallengeLevel();
clone.setUniqueId(this.uniqueId);
clone.setFriendlyName(this.friendlyName);
clone.setIcon(this.icon.clone());
Expand All @@ -545,12 +558,21 @@ public ChallengeLevel clone()
clone.setWaiverAmount(this.waiverAmount);
clone.setUnlockMessage(this.unlockMessage);
clone.setRewardText(this.rewardText);
clone.setRewardItems(this.rewardItems.stream().map(ItemStack::clone).collect(Collectors.toCollection(() -> new ArrayList<>(this.rewardItems.size()))));
clone.setRewardItems(
this.rewardItems.stream().
map(ItemStack::clone).
collect(Collectors.toCollection(() -> new ArrayList<>(this.rewardItems.size()))));
clone.setRewardExperience(this.rewardExperience);
clone.setRewardMoney(this.rewardMoney);
clone.setRewardCommands(new ArrayList<>(this.rewardCommands));
clone.setChallenges(new HashSet<>(this.challenges));
}
catch (Exception e)
{
BentoBox.getInstance().logError("Failed to clone ChallengeLevel " + this.uniqueId);
BentoBox.getInstance().logStacktrace(e);
clone = this;
}

return clone;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -85,6 +86,19 @@ public void setTakeItems(boolean takeItems)
// ---------------------------------------------------------------------


/**
* Method isValid returns if given requirement data is valid or not.
*
* @return {@code true} if data is valid, {@code false} otherwise.
*/
@Override
public boolean isValid()
{
return super.isValid() &&
this.requiredItems != null && this.requiredItems.stream().noneMatch(Objects::isNull);
}


/**
* Method Requirements#clone allows to clone Requirements object, to avoid changing content when it is necessary
* to use it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
package world.bentobox.challenges.database.object.requirements;


import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.*;

import org.bukkit.Material;
import org.bukkit.entity.EntityType;
Expand Down Expand Up @@ -153,6 +150,20 @@ public void setSearchRadius(int searchRadius)
// ---------------------------------------------------------------------


/**
* Method isValid returns if given requirement data is valid or not.
*
* @return {@code true} if data is valid, {@code false} otherwise.
*/
@Override
public boolean isValid()
{
return super.isValid() &&
this.requiredBlocks != null && this.requiredBlocks.keySet().stream().noneMatch(Objects::isNull) &&
this.requiredEntities != null && this.requiredEntities.keySet().stream().noneMatch(Objects::isNull);
}


/**
* Method Requirements#clone allows to clone Requirements object, to avoid changing content when it is necessary
* to use it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public void setRequiredPermissions(Set<String> requiredPermissions)
// ---------------------------------------------------------------------


/**
* Method isValid returns if given requirement data is valid or not.
* @return {@code true} if data is valid, {@code false} otherwise.
*/
public boolean isValid()
{
return this.requiredPermissions != null;
}


/**
* Method Requirements#clone allows to clone Requirements object, to avoid changing content when it is necessary
* to use it.
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/world/bentobox/challenges/panel/CommonGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1006,11 +1006,13 @@ else if (meta instanceof SpawnEggMeta)
}
else if (meta instanceof TropicalFishBucketMeta)
{
result.add(this.user.getTranslation("challenges.gui.item-description.fish-meta",
if (((TropicalFishBucketMeta) meta).hasVariant())
{
result.add(this.user.getTranslation("challenges.gui.item-description.fish-meta",
"[pattern]", Util.prettifyText(((TropicalFishBucketMeta) meta).getPattern().name()),
"[pattern-color]", Util.prettifyText(((TropicalFishBucketMeta) meta).getPatternColor().name()),
"[body-color]", Util.prettifyText(((TropicalFishBucketMeta) meta).getBodyColor().name())));
// parse ne
}
}

if (meta.hasEnchants())
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ challenges:
missing-level: '&c Challenge Level [level] is not defined in the database. It may cause errors!'
missing-arguments: '&c Command is missing arguments.'
no-multiple-permission: "&c You do not have permission to complete this challenge multiple times at once."
invalid-level: "&c Level [level] contains invalid data. It will not be loaded from database!"
invalid-challenge: "&c Challenge [challenge] contains invalid data. It will not be loaded from database!"
protection:
flags:
CHALLENGES_ISLAND_PROTECTION:
Expand Down

0 comments on commit 504c0b4

Please sign in to comment.