Remove unused adapters.

Updates Challenges and ChallengesLevel objects.
Add TypeMigrationAdapter that will fix issue with renamed challenge type.
This commit is contained in:
BONNe 2021-09-18 12:58:52 +03:00
parent 3ee89af5df
commit d1f15aed8b
6 changed files with 79 additions and 486 deletions

View File

@ -18,6 +18,7 @@ import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table; import world.bentobox.bentobox.database.objects.Table;
import world.bentobox.challenges.database.object.adapters.EntityCompatibilityAdapter; import world.bentobox.challenges.database.object.adapters.EntityCompatibilityAdapter;
import world.bentobox.challenges.database.object.adapters.RequirementsAdapter; import world.bentobox.challenges.database.object.adapters.RequirementsAdapter;
import world.bentobox.challenges.database.object.adapters.TypeMigrationAdapter;
import world.bentobox.challenges.database.object.requirements.Requirements; 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. * The player must have the items on them.
*/ */
INVENTORY, INVENTORY_TYPE,
/** /**
* Items or required entities have to be within x blocks of the player. * 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 type, like required money / experience or island level. This my request
* other plugins to be setup before it could work. * other plugins to be setup before it could work.
*/ */
OTHER, OTHER_TYPE,
/** /**
* Challenge based on player statistic data. * 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. * Challenge type can be INVENTORY, OTHER or ISLAND.
*/ */
@Expose @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. * 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) @JsonAdapter(RequirementsAdapter.class)
private Requirements requirements; private Requirements requirements;
// ---------------------------------------------------------------------
// Section: Deprecated Requirements
// ---------------------------------------------------------------------
@Deprecated
@Expose
private Set<String> requiredPermissions = new HashSet<>();
@Deprecated
@Expose
private Map<Material, Integer> requiredBlocks = new EnumMap<>(Material.class);
@Deprecated
@Expose
private boolean removeBlocks;
@Deprecated
@Expose
@JsonAdapter(EntityCompatibilityAdapter.class)
private Map<EntityType, Integer> requiredEntities = new EnumMap<>(EntityType.class);
@Deprecated
@Expose
private boolean removeEntities;
@Deprecated
@Expose
private List<ItemStack> 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 // Section: Rewards
@ -226,7 +171,7 @@ public class Challenge implements DataObject
* Money reward. Economy plugin or addon required for this option. * Money reward. Economy plugin or addon required for this option.
*/ */
@Expose @Expose
private int rewardMoney = 0; private double rewardMoney = 0;
/** /**
* Commands to run when the player completes the challenge for the first time. String List * 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. * Repeat money reward. Economy plugin or addon required for this option.
*/ */
@Expose @Expose
private int repeatMoneyReward; private double repeatMoneyReward;
/** /**
* Commands to run when challenge is repeated. String List. * Commands to run when challenge is repeated. String List.
@ -389,136 +334,6 @@ public class Challenge implements DataObject
} }
/**
* @return the requiredPermissions
*/
@Deprecated
public Set<String> getRequiredPermissions()
{
return requiredPermissions;
}
/**
* @return the requiredBlocks
*/
@Deprecated
public Map<Material, Integer> getRequiredBlocks()
{
return requiredBlocks;
}
/**
* @return the removeBlocks
*/
@Deprecated
public boolean isRemoveBlocks()
{
return removeBlocks;
}
/**
* @return the requiredEntities
*/
@Deprecated
public Map<EntityType, Integer> getRequiredEntities()
{
return requiredEntities;
}
/**
* @return the removeEntities
*/
@Deprecated
public boolean isRemoveEntities()
{
return removeEntities;
}
/**
* @return the requiredItems
*/
@Deprecated
public List<ItemStack> 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 * @return the rewardText
*/ */
@ -549,7 +364,7 @@ public class Challenge implements DataObject
/** /**
* @return the rewardMoney * @return the rewardMoney
*/ */
public int getRewardMoney() public double getRewardMoney()
{ {
return rewardMoney; return rewardMoney;
} }
@ -612,7 +427,7 @@ public class Challenge implements DataObject
/** /**
* @return the repeatMoneyReward * @return the repeatMoneyReward
*/ */
public int getRepeatMoneyReward() public double getRepeatMoneyReward()
{ {
return repeatMoneyReward; 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<String> requiredPermissions)
{
this.requiredPermissions = requiredPermissions;
}
/**
* This method sets the requiredBlocks value.
* @param requiredBlocks the requiredBlocks new value.
*
*/
@Deprecated
public void setRequiredBlocks(Map<Material, Integer> 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<EntityType, Integer> 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<ItemStack> 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. * This method sets the rewardText value.
* @param rewardText the rewardText new value. * @param rewardText the rewardText new value.
@ -934,7 +593,7 @@ public class Challenge implements DataObject
* @param rewardMoney the rewardMoney new value. * @param rewardMoney the rewardMoney new value.
* *
*/ */
public void setRewardMoney(int rewardMoney) public void setRewardMoney(double rewardMoney)
{ {
this.rewardMoney = rewardMoney; this.rewardMoney = rewardMoney;
} }
@ -1011,7 +670,7 @@ public class Challenge implements DataObject
* @param repeatMoneyReward the repeatMoneyReward new value. * @param repeatMoneyReward the repeatMoneyReward new value.
* *
*/ */
public void setRepeatMoneyReward(int repeatMoneyReward) public void setRepeatMoneyReward(double repeatMoneyReward)
{ {
this.repeatMoneyReward = repeatMoneyReward; this.repeatMoneyReward = repeatMoneyReward;
} }

View File

@ -100,7 +100,7 @@ public class ChallengeLevel implements DataObject, Comparable<ChallengeLevel>
@ConfigComment("") @ConfigComment("")
@ConfigComment("Money reward. Economy plugin or addon required for this option.") @ConfigComment("Money reward. Economy plugin or addon required for this option.")
@Expose @Expose
private int rewardMoney = 0; private double rewardMoney = 0;
@ConfigComment("") @ConfigComment("")
@ConfigComment("Commands to run when the player completes all challenges in current") @ConfigComment("Commands to run when the player completes all challenges in current")
@ -236,7 +236,7 @@ public class ChallengeLevel implements DataObject, Comparable<ChallengeLevel>
* This method returns the rewardMoney value. * This method returns the rewardMoney value.
* @return the value of rewardMoney. * @return the value of rewardMoney.
*/ */
public int getRewardMoney() public double getRewardMoney()
{ {
return rewardMoney; return rewardMoney;
} }
@ -395,7 +395,7 @@ public class ChallengeLevel implements DataObject, Comparable<ChallengeLevel>
* @param rewardMoney the rewardMoney new value. * @param rewardMoney the rewardMoney new value.
* *
*/ */
public void setRewardMoney(int rewardMoney) public void setRewardMoney(double rewardMoney)
{ {
this.rewardMoney = rewardMoney; this.rewardMoney = rewardMoney;
} }

View File

@ -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<ChallengeLore>, List<String>>
{
@SuppressWarnings("unchecked")
@Override
public List<ChallengeLore> deserialize(Object from)
{
List<ChallengeLore> result;
if (from instanceof List)
{
result = ((List<String>) from).stream().
map(ChallengeLore::valueOf).
collect(Collectors.toCollection(ArrayList::new));
}
else
{
result = new ArrayList<>(0);
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public List<String> serialize(Object to)
{
List<String> result;
if (to instanceof List)
{
result = ((List<ChallengeLore>) to).stream().
map(ChallengeLore::name).
collect(Collectors.toCollection(ArrayList::new));
}
else
{
result = new ArrayList<>(0);
}
return result;
}
}

View File

@ -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<LevelLore>, List<String>>
{
@SuppressWarnings("unchecked")
@Override
public List<LevelLore> deserialize(Object from)
{
List<LevelLore> result;
if (from instanceof List)
{
result = ((List<String>) from).stream().
map(LevelLore::valueOf).
collect(Collectors.toCollection(ArrayList::new));
}
else
{
result = new ArrayList<>(0);
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public List<String> serialize(Object to)
{
List<String> result;
if (to instanceof List)
{
result = ((List<LevelLore>) to).stream().
map(LevelLore::name).
collect(Collectors.toCollection(ArrayList::new));
}
else
{
result = new ArrayList<>(0);
}
return result;
}
}

View File

@ -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<Challenge.ChallengeType>, JsonDeserializer<Challenge.ChallengeType>
{
/**
* 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;
};
}
}

View File

@ -11,6 +11,8 @@ import com.google.gson.annotations.Expose;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Statistic; import org.bukkit.Statistic;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
public class StatisticRequirements extends Requirements public class StatisticRequirements extends Requirements
@ -82,6 +84,7 @@ public class StatisticRequirements extends Requirements
* *
* @return the statistic * @return the statistic
*/ */
@Nullable
public Statistic getStatistic() public Statistic getStatistic()
{ {
return statistic; return statistic;
@ -93,7 +96,7 @@ public class StatisticRequirements extends Requirements
* *
* @param statistic the statistic * @param statistic the statistic
*/ */
public void setStatistic(Statistic statistic) public void setStatistic(@NonNull Statistic statistic)
{ {
this.statistic = statistic; this.statistic = statistic;
} }
@ -104,6 +107,7 @@ public class StatisticRequirements extends Requirements
* *
* @return the entity * @return the entity
*/ */
@Nullable
public EntityType getEntity() public EntityType getEntity()
{ {
return entity; return entity;
@ -115,7 +119,7 @@ public class StatisticRequirements extends Requirements
* *
* @param entity the entity * @param entity the entity
*/ */
public void setEntity(EntityType entity) public void setEntity(@Nullable EntityType entity)
{ {
this.entity = entity; this.entity = entity;
} }
@ -126,6 +130,7 @@ public class StatisticRequirements extends Requirements
* *
* @return the material * @return the material
*/ */
@Nullable
public Material getMaterial() public Material getMaterial()
{ {
return material; return material;
@ -137,7 +142,7 @@ public class StatisticRequirements extends Requirements
* *
* @param material the material * @param material the material
*/ */
public void setMaterial(Material material) public void setMaterial(@Nullable Material material)
{ {
this.material = material; this.material = material;
} }
@ -195,18 +200,21 @@ public class StatisticRequirements extends Requirements
* Type of the statistic field. * Type of the statistic field.
*/ */
@Expose @Expose
@Nullable
private Statistic statistic; private Statistic statistic;
/** /**
* Type of entity for entity related statistics. * Type of entity for entity related statistics.
*/ */
@Expose @Expose
@Nullable
private EntityType entity; private EntityType entity;
/** /**
* Type of material for block and item related statistics. * Type of material for block and item related statistics.
*/ */
@Expose @Expose
@Nullable
private Material material; private Material material;
/** /**