Implement clone methods for Challenge and ChallengeLevel objects.

This commit is contained in:
BONNe 2019-04-25 00:08:13 +03:00
parent 5ab53d0e9d
commit e6f151ef89
2 changed files with 97 additions and 6 deletions

View File

@ -1,12 +1,8 @@
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;
import org.bukkit.World;
@ -1025,4 +1021,62 @@ public class Challenge implements DataObject
return uniqueId.equals(other.uniqueId);
}
}
/**
* Clone method that returns clone of current challenge.
* @return Challenge that is cloned from current object.
*/
@Override
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);
clone.setDescription(new ArrayList<>(this.description));
clone.setIcon(this.icon.clone());
clone.setOrder(this.order);
clone.setChallengeType(ChallengeType.valueOf(this.challengeType.name()));
clone.setEnvironment(new HashSet<>(this.environment));
clone.setLevel(this.level);
clone.setRemoveWhenCompleted(this.removeWhenCompleted);
clone.setRequiredPermissions(new HashSet<>(this.requiredPermissions));
clone.setRequiredBlocks(new HashMap<>(this.requiredBlocks));
clone.setRemoveBlocks(this.removeBlocks);
clone.setRequiredEntities(new HashMap<>(this.requiredEntities));
clone.setRemoveEntities(this.removeEntities);
clone.setRequiredItems(this.requiredItems.stream().map(ItemStack::clone).collect(Collectors.toCollection(() -> new ArrayList<>(this.requiredItems.size()))));
clone.setTakeItems(this.takeItems);
clone.setRequiredExperience(this.requiredExperience);
clone.setTakeExperience(this.takeExperience);
clone.setRequiredMoney(this.requiredMoney);
clone.setTakeMoney(this.takeMoney);
clone.setRequiredIslandLevel(this.requiredIslandLevel);
clone.setSearchRadius(this.searchRadius);
clone.setRewardText(this.rewardText);
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.setRepeatable(this.repeatable);
clone.setRepeatRewardText(this.repeatRewardText);
clone.setMaxTimes(this.maxTimes);
clone.setRepeatExperienceReward(this.repeatExperienceReward);
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));
}
return clone;
}
}

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.configuration.ConfigComment;
import world.bentobox.bentobox.database.objects.DataObject;
@ -496,4 +497,40 @@ public class ChallengeLevel implements DataObject, Comparable<ChallengeLevel>
return uniqueId.equals(other.uniqueId);
}
}
/**
* Clone method that returns clone of current challengeLevel.
* @return ChallengeLevel that is cloned from current object.
*/
@Override
protected ChallengeLevel clone()
{
ChallengeLevel clone;
try
{
clone = (ChallengeLevel) super.clone();
}
catch (CloneNotSupportedException e)
{
clone = new ChallengeLevel();
clone.setUniqueId(this.uniqueId);
clone.setFriendlyName(this.friendlyName);
clone.setIcon(this.icon.clone());
clone.setLockedIcon(this.lockedIcon != null ? this.lockedIcon.clone() : null);
clone.setWorld(this.world);
clone.setOrder(this.order);
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.setRewardExperience(this.rewardExperience);
clone.setRewardMoney(this.rewardMoney);
clone.setRewardCommands(new ArrayList<>(this.rewardCommands));
clone.setChallenges(new HashSet<>(this.challenges));
}
return clone;
}
}