Skip to content

Commit

Permalink
Rework Challenge Object Data storing.
Browse files Browse the repository at this point in the history
Requires MIGRATION if upgrade from older version.
  • Loading branch information
BONNe committed Sep 8, 2019
1 parent 09036a2 commit 4e6d37c
Show file tree
Hide file tree
Showing 16 changed files with 3,867 additions and 2,791 deletions.
29 changes: 26 additions & 3 deletions src/main/java/world/bentobox/challenges/ChallengesAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType;
import world.bentobox.bentobox.hooks.VaultHook;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.challenges.commands.ChallengesCommand;
import world.bentobox.challenges.commands.ChallengesUserCommand;
import world.bentobox.challenges.commands.admin.Challenges;
import world.bentobox.challenges.commands.admin.ChallengesAdminCommand;
import world.bentobox.challenges.config.Settings;
import world.bentobox.challenges.handlers.*;
import world.bentobox.challenges.handlers.ChallengeDataRequestHandler;
import world.bentobox.challenges.handlers.ChallengeListRequestHandler;
import world.bentobox.challenges.handlers.CompletedChallengesRequestHandler;
import world.bentobox.challenges.handlers.LevelDataRequestHandler;
import world.bentobox.challenges.handlers.LevelListRequestHandler;
import world.bentobox.challenges.listeners.ResetListener;
import world.bentobox.challenges.listeners.SaveListener;
import world.bentobox.challenges.web.WebManager;
Expand Down Expand Up @@ -127,15 +132,23 @@ public void onLoad() {
public void onEnable() {
// Check if it is enabled - it might be loaded, but not enabled.
if (this.getPlugin() == null || !this.getPlugin().isEnabled()) {
Bukkit.getLogger().severe("BentoBox is not available or disabled!");
this.logError("BentoBox is not available or disabled!");
this.setState(State.DISABLED);
return;
}

// Check if addon is not disabled before.
if (this.getState().equals(State.DISABLED))
{
Bukkit.getLogger().severe("Challenges Addon is not available or disabled!");
this.logError("Challenges Addon is not available or disabled!");
return;
}

if (this.isInCompatibleDatabase())
{
this.logError("BentoBox database is not compatible with Challenges Addon.");
this.logError("Please use JSON based database type.");
this.setState(State.DISABLED);
return;
}

Expand Down Expand Up @@ -294,6 +307,16 @@ private void loadSettings() {
}


/**
* This method checks if database is compatible with Challenges addon.
* @return {@code true} if database type is YAML, {@code false} - otherwise.
*/
private boolean isInCompatibleDatabase()
{
return this.getPlugin().getSettings().getDatabaseType().equals(DatabaseType.YAML);
}


// ---------------------------------------------------------------------
// Section: Getters
// ---------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import java.io.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.bukkit.World;
Expand All @@ -28,7 +37,7 @@
public class ChallengesImportManager
{
/**
* Import challenges from challenges.yml
* Import challenges from default.json
* @param challengesAddon
*/
public ChallengesImportManager(ChallengesAddon challengesAddon)
Expand Down Expand Up @@ -328,7 +337,7 @@ public boolean generateDefaultChallengeFile(User user, World world, boolean over
/**
* This Class allows to load default challenges and their levels as objects much easier.
*/
private final class DefaultJSONHandler
private static final class DefaultJSONHandler
{
/**
* This constructor inits JSON builder that will be used to parse challenges.
Expand Down Expand Up @@ -375,19 +384,24 @@ DefaultDataHolder loadObject()
{
File defaultFile = new File(this.addon.getDataFolder(), "default.json");

StringBuilder builder = new StringBuilder();
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(defaultFile), StandardCharsets.UTF_8))
{
DefaultDataHolder object = this.gson.fromJson(reader, DefaultDataHolder.class);

reader.close(); // NOSONAR Required to keep OS file handlers low and not rely on GC

try
return object;
}
catch (FileNotFoundException e)
{
Files.readAllLines(defaultFile.toPath()).forEach(builder::append);
this.addon.logError("Could not load file '" + defaultFile.getName() + "': File not found.");
}
catch (IOException e)
catch (Exception e)
{
e.printStackTrace();
this.addon.logError("Could not load objects " + defaultFile.getName() + " " + e.getMessage());
}


return this.gson.fromJson(builder.toString(), DefaultDataHolder.class);
return null;
}


Expand Down Expand Up @@ -422,7 +436,7 @@ DefaultDataHolder loadWebObject(String downloadedObject)
* This is simple object that will allow to store all current challenges and levels
* in single file.
*/
private final class DefaultDataHolder implements DataObject
private static final class DefaultDataHolder implements DataObject
{
/**
* Default constructor. Creates object with empty lists.
Expand All @@ -439,7 +453,7 @@ private final class DefaultDataHolder implements DataObject
* This method returns stored challenge list.
* @return list that contains default challenges.
*/
public List<Challenge> getChallengeList()
List<Challenge> getChallengeList()
{
return challengeList;
}
Expand All @@ -449,7 +463,7 @@ public List<Challenge> getChallengeList()
* This method sets given list as default challenge list.
* @param challengeList new default challenge list.
*/
public void setChallengeList(List<Challenge> challengeList)
void setChallengeList(List<Challenge> challengeList)
{
this.challengeList = challengeList;
}
Expand All @@ -459,7 +473,7 @@ public void setChallengeList(List<Challenge> challengeList)
* This method returns list of default challenge levels.
* @return List that contains default challenge levels.
*/
public List<ChallengeLevel> getLevelList()
List<ChallengeLevel> getLevelList()
{
return challengeLevelList;
}
Expand All @@ -469,7 +483,7 @@ public List<ChallengeLevel> getLevelList()
* This method sets given list as default challenge level list.
* @param levelList new default challenge level list.
*/
public void setLevelList(List<ChallengeLevel> levelList)
void setLevelList(List<ChallengeLevel> levelList)
{
this.challengeLevelList = levelList;
}
Expand Down
52 changes: 51 additions & 1 deletion src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
import world.bentobox.challenges.database.object.Challenge;
import world.bentobox.challenges.database.object.ChallengeLevel;
import world.bentobox.challenges.database.object.ChallengesPlayerData;
import world.bentobox.challenges.database.object.requirements.InventoryRequirements;
import world.bentobox.challenges.database.object.requirements.IslandRequirements;
import world.bentobox.challenges.database.object.requirements.OtherRequirements;
import world.bentobox.challenges.database.object.requirements.Requirements;
import world.bentobox.challenges.events.ChallengeCompletedEvent;
import world.bentobox.challenges.events.ChallengeResetAllEvent;
import world.bentobox.challenges.events.ChallengeResetEvent;
Expand Down Expand Up @@ -731,6 +735,49 @@ private boolean migrateChallenges(World world)
this.challengeDatabase.saveObject(challenge);
this.challengeCacheData.put(challenge.getUniqueId(), challenge);
}

// Migrate Requirements.
if (challenge.getRequirements() == null)
{
switch (challenge.getChallengeType())
{
case INVENTORY:
InventoryRequirements inventoryRequirements = new InventoryRequirements();
inventoryRequirements.setRequiredItems(challenge.getRequiredItems());
inventoryRequirements.setTakeItems(challenge.isTakeItems());

inventoryRequirements.setRequiredPermissions(challenge.getRequiredPermissions());
challenge.setRequirements(inventoryRequirements);
break;
case ISLAND:
IslandRequirements islandRequirements = new IslandRequirements();
islandRequirements.setRemoveBlocks(challenge.isRemoveBlocks());
islandRequirements.setRemoveEntities(challenge.isRemoveEntities());
islandRequirements.setRequiredBlocks(challenge.getRequiredBlocks());
islandRequirements.setRequiredEntities(challenge.getRequiredEntities());
islandRequirements.setSearchRadius(challenge.getSearchRadius());

islandRequirements.setRequiredPermissions(challenge.getRequiredPermissions());
challenge.setRequirements(islandRequirements);
break;
case OTHER:
OtherRequirements otherRequirements = new OtherRequirements();
otherRequirements.setRequiredExperience(challenge.getRequiredExperience());
otherRequirements.setRequiredIslandLevel(challenge.getRequiredIslandLevel());
otherRequirements.setRequiredMoney(challenge.getRequiredMoney());
otherRequirements.setTakeExperience(challenge.isTakeExperience());
otherRequirements.setTakeMoney(challenge.isTakeMoney());

otherRequirements.setRequiredPermissions(challenge.getRequiredPermissions());
challenge.setRequirements(otherRequirements);
break;
}

// This save should not involve any upgrades in other parts.

this.challengeDatabase.saveObject(challenge);
this.challengeCacheData.put(challenge.getUniqueId(), challenge);
}
}

return updated;
Expand Down Expand Up @@ -1642,14 +1689,17 @@ public boolean containsChallenge(String name)
/**
* This method creates and returns new challenge with given uniqueID.
* @param uniqueID - new ID for challenge.
* @param requirements - requirements object, as it is not changeable anymore.
* @return Challenge that is currently created.
*/
public Challenge createChallenge(String uniqueID)
public Challenge createChallenge(String uniqueID, Challenge.ChallengeType type, Requirements requirements)
{
if (!this.containsChallenge(uniqueID))
{
Challenge challenge = new Challenge();
challenge.setUniqueId(uniqueID);
challenge.setRequirements(requirements);
challenge.setChallengeType(type);

this.saveChallenge(challenge);
this.loadChallenge(challenge);
Expand Down

0 comments on commit 4e6d37c

Please sign in to comment.