From d1f15aed8be2f86fc330d16e8a1822a8ab5d6101 Mon Sep 17 00:00:00 2001 From: BONNe Date: Sat, 18 Sep 2021 12:58:52 +0300 Subject: [PATCH] Remove unused adapters. Updates Challenges and ChallengesLevel objects. Add TypeMigrationAdapter that will fix issue with renamed challenge type. --- .../challenges/database/object/Challenge.java | 367 +----------------- .../database/object/ChallengeLevel.java | 6 +- .../object/adapters/ChallengeLoreAdapter.java | 63 --- .../object/adapters/LevelLoreAdapter.java | 63 --- .../object/adapters/TypeMigrationAdapter.java | 52 +++ .../requirements/StatisticRequirements.java | 14 +- 6 files changed, 79 insertions(+), 486 deletions(-) delete mode 100644 src/main/java/world/bentobox/challenges/database/object/adapters/ChallengeLoreAdapter.java delete mode 100644 src/main/java/world/bentobox/challenges/database/object/adapters/LevelLoreAdapter.java create mode 100644 src/main/java/world/bentobox/challenges/database/object/adapters/TypeMigrationAdapter.java diff --git a/src/main/java/world/bentobox/challenges/database/object/Challenge.java b/src/main/java/world/bentobox/challenges/database/object/Challenge.java index f80959a..83a8ce1 100644 --- a/src/main/java/world/bentobox/challenges/database/object/Challenge.java +++ b/src/main/java/world/bentobox/challenges/database/object/Challenge.java @@ -18,6 +18,7 @@ import world.bentobox.bentobox.database.objects.DataObject; import world.bentobox.bentobox.database.objects.Table; import world.bentobox.challenges.database.object.adapters.EntityCompatibilityAdapter; import world.bentobox.challenges.database.object.adapters.RequirementsAdapter; +import world.bentobox.challenges.database.object.adapters.TypeMigrationAdapter; import world.bentobox.challenges.database.object.requirements.Requirements; @@ -45,23 +46,23 @@ public class Challenge implements DataObject /** * The player must have the items on them. */ - INVENTORY, + INVENTORY_TYPE, /** * Items or required entities have to be within x blocks of the player. */ - ISLAND, + ISLAND_TYPE, /** * Other type, like required money / experience or island level. This my request * other plugins to be setup before it could work. */ - OTHER, + OTHER_TYPE, /** * Challenge based on player statistic data. */ - STATISTIC + STATISTIC_TYPE } @@ -111,7 +112,8 @@ public class Challenge implements DataObject * Challenge type can be INVENTORY, OTHER or ISLAND. */ @Expose - private ChallengeType challengeType = ChallengeType.INVENTORY; + @JsonAdapter(TypeMigrationAdapter.class) + private ChallengeType challengeType = ChallengeType.INVENTORY_TYPE; /** * List of environments where this challenge will occur: NETHER, NORMAL, THE_END. Leave blank for all. @@ -142,63 +144,6 @@ public class Challenge implements DataObject @JsonAdapter(RequirementsAdapter.class) private Requirements requirements; - // --------------------------------------------------------------------- - // Section: Deprecated Requirements - // --------------------------------------------------------------------- - - @Deprecated - @Expose - private Set requiredPermissions = new HashSet<>(); - - @Deprecated - @Expose - private Map requiredBlocks = new EnumMap<>(Material.class); - - @Deprecated - @Expose - private boolean removeBlocks; - - @Deprecated - @Expose - @JsonAdapter(EntityCompatibilityAdapter.class) - private Map requiredEntities = new EnumMap<>(EntityType.class); - - @Deprecated - @Expose - private boolean removeEntities; - - @Deprecated - @Expose - private List requiredItems = new ArrayList<>(); - - @Deprecated - @Expose - private boolean takeItems = true; - - @Deprecated - @Expose - private int requiredExperience = 0; - - @Deprecated - @Expose - private boolean takeExperience; - - @Deprecated - @Expose - private int requiredMoney = 0; - - @Deprecated - @Expose - private boolean takeMoney; - - @Deprecated - @Expose - private long requiredIslandLevel; - - @Deprecated - @Expose - private int searchRadius = 10; - // --------------------------------------------------------------------- // Section: Rewards @@ -226,7 +171,7 @@ public class Challenge implements DataObject * Money reward. Economy plugin or addon required for this option. */ @Expose - private int rewardMoney = 0; + private double rewardMoney = 0; /** * Commands to run when the player completes the challenge for the first time. String List @@ -273,7 +218,7 @@ public class Challenge implements DataObject * Repeat money reward. Economy plugin or addon required for this option. */ @Expose - private int repeatMoneyReward; + private double repeatMoneyReward; /** * Commands to run when challenge is repeated. String List. @@ -389,136 +334,6 @@ public class Challenge implements DataObject } - /** - * @return the requiredPermissions - */ - @Deprecated - public Set getRequiredPermissions() - { - return requiredPermissions; - } - - - /** - * @return the requiredBlocks - */ - @Deprecated - public Map getRequiredBlocks() - { - return requiredBlocks; - } - - - /** - * @return the removeBlocks - */ - @Deprecated - public boolean isRemoveBlocks() - { - return removeBlocks; - } - - - /** - * @return the requiredEntities - */ - @Deprecated - public Map getRequiredEntities() - { - return requiredEntities; - } - - - /** - * @return the removeEntities - */ - @Deprecated - public boolean isRemoveEntities() - { - return removeEntities; - } - - - /** - * @return the requiredItems - */ - @Deprecated - public List getRequiredItems() - { - return requiredItems; - } - - - /** - * @return the takeItems - */ - @Deprecated - public boolean isTakeItems() - { - return takeItems; - } - - - /** - * @return the requiredExperience - */ - @Deprecated - public int getRequiredExperience() - { - return requiredExperience; - } - - - /** - * @return the takeExperience - */ - @Deprecated - public boolean isTakeExperience() - { - return takeExperience; - } - - - /** - * @return the requiredMoney - */ - @Deprecated - public int getRequiredMoney() - { - return requiredMoney; - } - - - /** - * @return the takeMoney - */ - @Deprecated - public boolean isTakeMoney() - { - return takeMoney; - } - - - /** - * @return the requiredIslandLevel - */ - @Deprecated - public long getRequiredIslandLevel() - { - return requiredIslandLevel; - } - - - /** - * @return the searchRadius - */ - @Deprecated - public int getSearchRadius() - { - return searchRadius; - } - - /** * @return the rewardText */ @@ -549,7 +364,7 @@ public class Challenge implements DataObject /** * @return the rewardMoney */ - public int getRewardMoney() + public double getRewardMoney() { return rewardMoney; } @@ -612,7 +427,7 @@ public class Challenge implements DataObject /** * @return the repeatMoneyReward */ - public int getRepeatMoneyReward() + public double getRepeatMoneyReward() { return repeatMoneyReward; } @@ -740,162 +555,6 @@ public class Challenge implements DataObject } - /** - * This method sets the requiredPermissions value. - * @param requiredPermissions the requiredPermissions new value. - * - */ - @Deprecated - public void setRequiredPermissions(Set requiredPermissions) - { - this.requiredPermissions = requiredPermissions; - } - - - /** - * This method sets the requiredBlocks value. - * @param requiredBlocks the requiredBlocks new value. - * - */ - @Deprecated - public void setRequiredBlocks(Map requiredBlocks) - { - this.requiredBlocks = requiredBlocks; - } - - - /** - * This method sets the removeBlocks value. - * @param removeBlocks the removeBlocks new value. - * - */ - @Deprecated - public void setRemoveBlocks(boolean removeBlocks) - { - this.removeBlocks = removeBlocks; - } - - - /** - * This method sets the requiredEntities value. - * @param requiredEntities the requiredEntities new value. - * - */ - @Deprecated - public void setRequiredEntities(Map requiredEntities) - { - this.requiredEntities = requiredEntities; - } - - - /** - * This method sets the removeEntities value. - * @param removeEntities the removeEntities new value. - * - */ - @Deprecated - public void setRemoveEntities(boolean removeEntities) - { - this.removeEntities = removeEntities; - } - - - /** - * This method sets the requiredItems value. - * @param requiredItems the requiredItems new value. - * - */ - @Deprecated - public void setRequiredItems(List requiredItems) - { - this.requiredItems = requiredItems; - } - - - /** - * This method sets the takeItems value. - * @param takeItems the takeItems new value. - * - */ - @Deprecated - public void setTakeItems(boolean takeItems) - { - this.takeItems = takeItems; - } - - - /** - * This method sets the requiredExperience value. - * @param requiredExperience the requiredExperience new value. - * - */ - @Deprecated - public void setRequiredExperience(int requiredExperience) - { - this.requiredExperience = requiredExperience; - } - - - /** - * This method sets the takeExperience value. - * @param takeExperience the takeExperience new value. - * - */ - @Deprecated - public void setTakeExperience(boolean takeExperience) - { - this.takeExperience = takeExperience; - } - - - /** - * This method sets the requiredMoney value. - * @param requiredMoney the requiredMoney new value. - * - */ - @Deprecated - public void setRequiredMoney(int requiredMoney) - { - this.requiredMoney = requiredMoney; - } - - - /** - * This method sets the takeMoney value. - * @param takeMoney the takeMoney new value. - * - */ - @Deprecated - public void setTakeMoney(boolean takeMoney) - { - this.takeMoney = takeMoney; - } - - - /** - * This method sets the requiredIslandLevel value. - * @param requiredIslandLevel the requiredIslandLevel new value. - * - */ - @Deprecated - public void setRequiredIslandLevel(long requiredIslandLevel) - { - this.requiredIslandLevel = requiredIslandLevel; - } - - - /** - * This method sets the searchRadius value. - * @param searchRadius the searchRadius new value. - * - */ - @Deprecated - public void setSearchRadius(int searchRadius) - { - this.searchRadius = searchRadius; - } - - /** * This method sets the rewardText value. * @param rewardText the rewardText new value. @@ -934,7 +593,7 @@ public class Challenge implements DataObject * @param rewardMoney the rewardMoney new value. * */ - public void setRewardMoney(int rewardMoney) + public void setRewardMoney(double rewardMoney) { this.rewardMoney = rewardMoney; } @@ -1011,7 +670,7 @@ public class Challenge implements DataObject * @param repeatMoneyReward the repeatMoneyReward new value. * */ - public void setRepeatMoneyReward(int repeatMoneyReward) + public void setRepeatMoneyReward(double repeatMoneyReward) { this.repeatMoneyReward = repeatMoneyReward; } diff --git a/src/main/java/world/bentobox/challenges/database/object/ChallengeLevel.java b/src/main/java/world/bentobox/challenges/database/object/ChallengeLevel.java index de772ad..b89caee 100644 --- a/src/main/java/world/bentobox/challenges/database/object/ChallengeLevel.java +++ b/src/main/java/world/bentobox/challenges/database/object/ChallengeLevel.java @@ -100,7 +100,7 @@ public class ChallengeLevel implements DataObject, Comparable @ConfigComment("") @ConfigComment("Money reward. Economy plugin or addon required for this option.") @Expose - private int rewardMoney = 0; + private double rewardMoney = 0; @ConfigComment("") @ConfigComment("Commands to run when the player completes all challenges in current") @@ -236,7 +236,7 @@ public class ChallengeLevel implements DataObject, Comparable * This method returns the rewardMoney value. * @return the value of rewardMoney. */ - public int getRewardMoney() + public double getRewardMoney() { return rewardMoney; } @@ -395,7 +395,7 @@ public class ChallengeLevel implements DataObject, Comparable * @param rewardMoney the rewardMoney new value. * */ - public void setRewardMoney(int rewardMoney) + public void setRewardMoney(double rewardMoney) { this.rewardMoney = rewardMoney; } diff --git a/src/main/java/world/bentobox/challenges/database/object/adapters/ChallengeLoreAdapter.java b/src/main/java/world/bentobox/challenges/database/object/adapters/ChallengeLoreAdapter.java deleted file mode 100644 index 450ca72..0000000 --- a/src/main/java/world/bentobox/challenges/database/object/adapters/ChallengeLoreAdapter.java +++ /dev/null @@ -1,63 +0,0 @@ -// -// Created by BONNe -// Copyright - 2019 -// - - -package world.bentobox.challenges.database.object.adapters; - - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -import world.bentobox.bentobox.database.objects.adapters.AdapterInterface; -import world.bentobox.challenges.config.SettingsUtils.ChallengeLore; - - -/** - * This adapter allows to serialize and deserialize ChallengeLore object. - */ -public class ChallengeLoreAdapter implements AdapterInterface, List> -{ - @SuppressWarnings("unchecked") - @Override - public List deserialize(Object from) - { - List result; - - if (from instanceof List) - { - result = ((List) from).stream(). - map(ChallengeLore::valueOf). - collect(Collectors.toCollection(ArrayList::new)); - } - else - { - result = new ArrayList<>(0); - } - - return result; - } - - - @SuppressWarnings("unchecked") - @Override - public List serialize(Object to) - { - List result; - - if (to instanceof List) - { - result = ((List) to).stream(). - map(ChallengeLore::name). - collect(Collectors.toCollection(ArrayList::new)); - } - else - { - result = new ArrayList<>(0); - } - - return result; - } -} diff --git a/src/main/java/world/bentobox/challenges/database/object/adapters/LevelLoreAdapter.java b/src/main/java/world/bentobox/challenges/database/object/adapters/LevelLoreAdapter.java deleted file mode 100644 index 6c60245..0000000 --- a/src/main/java/world/bentobox/challenges/database/object/adapters/LevelLoreAdapter.java +++ /dev/null @@ -1,63 +0,0 @@ -// -// Created by BONNe -// Copyright - 2019 -// - - -package world.bentobox.challenges.database.object.adapters; - - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -import world.bentobox.bentobox.database.objects.adapters.AdapterInterface; -import world.bentobox.challenges.config.SettingsUtils.LevelLore; - - -/** - * This adapter allows to serialize and deserialize LevelLore object. - */ -public class LevelLoreAdapter implements AdapterInterface, List> -{ - @SuppressWarnings("unchecked") - @Override - public List deserialize(Object from) - { - List result; - - if (from instanceof List) - { - result = ((List) from).stream(). - map(LevelLore::valueOf). - collect(Collectors.toCollection(ArrayList::new)); - } - else - { - result = new ArrayList<>(0); - } - - return result; - } - - - @SuppressWarnings("unchecked") - @Override - public List serialize(Object to) - { - List result; - - if (to instanceof List) - { - result = ((List) to).stream(). - map(LevelLore::name). - collect(Collectors.toCollection(ArrayList::new)); - } - else - { - result = new ArrayList<>(0); - } - - return result; - } -} diff --git a/src/main/java/world/bentobox/challenges/database/object/adapters/TypeMigrationAdapter.java b/src/main/java/world/bentobox/challenges/database/object/adapters/TypeMigrationAdapter.java new file mode 100644 index 0000000..48699f6 --- /dev/null +++ b/src/main/java/world/bentobox/challenges/database/object/adapters/TypeMigrationAdapter.java @@ -0,0 +1,52 @@ +// +// Created by BONNe +// Copyright - 2019 +// + + +package world.bentobox.challenges.database.object.adapters; + + +import com.google.gson.*; +import java.lang.reflect.Type; + +import world.bentobox.challenges.database.object.Challenge; + + +/** + * This is a generic JSON serializer and deserializer for abstract classes. + * It store target class in class object, and instance variables in variables object. + */ +public class TypeMigrationAdapter implements JsonSerializer, JsonDeserializer +{ + /** + * Use default enum name serialization. + */ + @Override + public JsonElement serialize(Challenge.ChallengeType src, Type typeOfSrc, JsonSerializationContext context) + { + return new JsonPrimitive(src.name()); + } + + + /** + * Deserialize enum with old type format. + */ + @Override + public Challenge.ChallengeType deserialize(JsonElement json, + Type typeOfT, + JsonDeserializationContext context) + throws JsonParseException + { + JsonPrimitive primitive = json.getAsJsonPrimitive(); + + return switch (primitive.getAsString()) + { + case "ISLAND", "ISLAND_TYPE" -> Challenge.ChallengeType.ISLAND_TYPE; + case "INVENTORY", "INVENTORY_TYPE" -> Challenge.ChallengeType.INVENTORY_TYPE; + case "OTHER", "OTHER_TYPE" -> Challenge.ChallengeType.OTHER_TYPE; + case "STATISTIC", "STATISTIC_TYPE" -> Challenge.ChallengeType.STATISTIC_TYPE; + default -> Challenge.ChallengeType.ISLAND_TYPE; + }; + } +} \ No newline at end of file diff --git a/src/main/java/world/bentobox/challenges/database/object/requirements/StatisticRequirements.java b/src/main/java/world/bentobox/challenges/database/object/requirements/StatisticRequirements.java index e676164..289f4f1 100644 --- a/src/main/java/world/bentobox/challenges/database/object/requirements/StatisticRequirements.java +++ b/src/main/java/world/bentobox/challenges/database/object/requirements/StatisticRequirements.java @@ -11,6 +11,8 @@ import com.google.gson.annotations.Expose; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.entity.EntityType; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; public class StatisticRequirements extends Requirements @@ -82,6 +84,7 @@ public class StatisticRequirements extends Requirements * * @return the statistic */ + @Nullable public Statistic getStatistic() { return statistic; @@ -93,7 +96,7 @@ public class StatisticRequirements extends Requirements * * @param statistic the statistic */ - public void setStatistic(Statistic statistic) + public void setStatistic(@NonNull Statistic statistic) { this.statistic = statistic; } @@ -104,6 +107,7 @@ public class StatisticRequirements extends Requirements * * @return the entity */ + @Nullable public EntityType getEntity() { return entity; @@ -115,7 +119,7 @@ public class StatisticRequirements extends Requirements * * @param entity the entity */ - public void setEntity(EntityType entity) + public void setEntity(@Nullable EntityType entity) { this.entity = entity; } @@ -126,6 +130,7 @@ public class StatisticRequirements extends Requirements * * @return the material */ + @Nullable public Material getMaterial() { return material; @@ -137,7 +142,7 @@ public class StatisticRequirements extends Requirements * * @param material the material */ - public void setMaterial(Material material) + public void setMaterial(@Nullable Material material) { this.material = material; } @@ -195,18 +200,21 @@ public class StatisticRequirements extends Requirements * Type of the statistic field. */ @Expose + @Nullable private Statistic statistic; /** * Type of entity for entity related statistics. */ @Expose + @Nullable private EntityType entity; /** * Type of material for block and item related statistics. */ @Expose + @Nullable private Material material; /**