Skip to content

Commit

Permalink
Small code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Apr 25, 2019
1 parent 5a5d2d7 commit 188e86d
Showing 1 changed file with 94 additions and 105 deletions.
199 changes: 94 additions & 105 deletions src/main/java/world/bentobox/challenges/ChallengesImportManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,7 @@ private boolean loadDefaultChallenges(User user, World world)
challengeHandler.loadObjects().forEach(challenge -> manager.loadChallenge(challenge, false, user, false));
levelHandler.loadObjects().forEach(level -> manager.loadLevel(level, false, user, false));
}
catch (NoSuchMethodException |
ClassNotFoundException |
IllegalAccessException |
IntrospectionException |
InvocationTargetException |
InstantiationException e)
catch (Exception e)
{
e.printStackTrace();
return false;
Expand All @@ -277,6 +272,94 @@ private boolean loadDefaultChallenges(User user, World world)
}


// ---------------------------------------------------------------------
// Section: Default generation
// ---------------------------------------------------------------------


/**
* Create method that can generate default challenge file from existing challenges in given world.
* This method will create default.json file in Challenges folder.
* @param world from which challenges must be stored.
*/
@SuppressWarnings("unused")
private void generateDefaultChallengeFile(World world)
{
File defaultFile = new File(BentoBox.getInstance().getDataFolder() + "/addons/Challenges", "default.json");

try
{
if (defaultFile.createNewFile())
{
String replacementString = Util.getWorld(world).getName() + "_";
ChallengesManager manager = this.addon.getChallengesManager();

// Store all challenges to single file
List<Challenge> challengeList = manager.getAllChallenges(world);

CustomJSONHandler<Challenge> generateChallengeJSON =
new CustomJSONHandler<>(BentoBox.getInstance(), Challenge.class, new CustomJSONConnector());

StringBuilder outputString = new StringBuilder();
outputString.append("{\"challenges\": [");

// Populate all challenges
for (int i = 0, size = challengeList.size(); i < size; i++)
{
Challenge clone = challengeList.get(i).clone();

clone.setUniqueId(clone.getUniqueId().replaceFirst(replacementString, ""));
clone.setLevel(clone.getLevel().replaceFirst(replacementString, ""));

if (i != 0)
{
outputString.append(",");
}

outputString.append(generateChallengeJSON.toJsonString(clone));
}

outputString.append("],\"levels\": [");

// Populate all levels
List<ChallengeLevel> levelList = manager.getLevels(world);
CustomJSONHandler<ChallengeLevel> generateLevelJSON =
new CustomJSONHandler<>(BentoBox.getInstance(), ChallengeLevel.class, new CustomJSONConnector());

for (int i = 0, size = levelList.size(); i < size; i++)
{
ChallengeLevel clone = levelList.get(i).clone();
clone.setUniqueId(clone.getUniqueId().replaceFirst(replacementString, ""));
clone.getChallenges().forEach(challenge -> challenge = challenge.replaceFirst(replacementString, ""));
clone.setWorld("");

if (i != 0)
{
outputString.append(",");
}

outputString.append(generateLevelJSON.toJsonString(clone));
}

// Add version string
outputString.append("],\"version\": \"").append(this.addon.getDescription().getVersion()).append("\"}");

// Store to file.
new FileWriter(defaultFile).write(outputString.toString());
}
}
catch (IOException e)
{
this.addon.logError("Could not save json file: " + e.getMessage());
}
}


// ---------------------------------------------------------------------
// Section: Private classes for default challegnes
// ---------------------------------------------------------------------


/**
* This Class allows to load default challenges and their levels as objects much easier.
*/
Expand All @@ -295,13 +378,7 @@ private final class CustomJSONHandler<T> extends AbstractJSONDatabaseHandler<T>
* {@inheritDoc}
*/
@Override
public List<T> loadObjects() throws
InstantiationException,
IllegalAccessException,
InvocationTargetException,
ClassNotFoundException,
IntrospectionException,
NoSuchMethodException
public List<T> loadObjects()
{
return Collections.emptyList();
}
Expand All @@ -311,13 +388,7 @@ public List<T> loadObjects() throws
* {@inheritDoc}
*/
@Override
public T loadObject(@NonNull String s) throws
InstantiationException,
IllegalAccessException,
InvocationTargetException,
ClassNotFoundException,
IntrospectionException,
NoSuchMethodException
public T loadObject(@NonNull String s)
{
return null;
}
Expand All @@ -328,7 +399,6 @@ public T loadObject(@NonNull String s) throws
*/
@Override
public void saveObject(T t)
throws IllegalAccessException, InvocationTargetException, IntrospectionException
{
// Will not be used in default challenge loading process.
}
Expand All @@ -339,7 +409,6 @@ public void saveObject(T t)
*/
@Override
public void deleteObject(T t)
throws IllegalAccessException, InvocationTargetException, IntrospectionException
{
// Will not be used in default challenge loading process.
}
Expand Down Expand Up @@ -402,7 +471,9 @@ public String toJsonString(T instance)


/**
* Simple custom connector used for importing default challenges
* Empty JSON connector. It is used just because it is necessary for AbstractJSONDatabaseHandler.
* UniqueIDs will always be unique, as default challenges will be implemented only if there
* does not exist any challenge in targeted world.
*/
private final static class CustomJSONConnector implements DatabaseConnector
{
Expand Down Expand Up @@ -454,86 +525,4 @@ public boolean uniqueIdExists(String s, String s1)
return false;
}
}


// ---------------------------------------------------------------------
// Section: Default generation
// ---------------------------------------------------------------------


/**
* Create method that can generate default challenge file from existing challenges in given world.
* This method will create default.json file in Challenges folder.
* @param world from which challenges must be stored.
*/
public void generateDefaultChallengeFile(World world)
{
File defaultFile = new File(BentoBox.getInstance().getDataFolder() + "/addons/Challenges", "default.json");

try
{
if (defaultFile.createNewFile())
{
String replacementString = Util.getWorld(world).getName() + "_";
ChallengesManager manager = this.addon.getChallengesManager();

// Store all challenges to single file
List<Challenge> challengeList = manager.getAllChallenges(world);

CustomJSONHandler<Challenge> generateChallengeJSON =
new CustomJSONHandler<>(BentoBox.getInstance(), Challenge.class, new CustomJSONConnector());

StringBuilder outputString = new StringBuilder();
outputString.append("{\"challenges\": [");

// Populate all challenges
for (int i = 0, size = challengeList.size(); i < size; i++)
{
Challenge clone = challengeList.get(i).clone();

clone.setUniqueId(clone.getUniqueId().replaceFirst(replacementString, ""));
clone.setLevel(clone.getLevel().replaceFirst(replacementString, ""));

if (i != 0)
{
outputString.append(",");
}

outputString.append(generateChallengeJSON.toJsonString(clone));
}

outputString.append("],\"levels\": [");

// Populate all levels
List<ChallengeLevel> levelList = manager.getLevels(world);
CustomJSONHandler<ChallengeLevel> generateLevelJSON =
new CustomJSONHandler<>(BentoBox.getInstance(), ChallengeLevel.class, new CustomJSONConnector());

for (int i = 0, size = levelList.size(); i < size; i++)
{
ChallengeLevel clone = levelList.get(i).clone();
clone.setUniqueId(clone.getUniqueId().replaceFirst(replacementString, ""));
clone.getChallenges().forEach(challenge -> challenge = challenge.replaceFirst(replacementString, ""));
clone.setWorld("");

if (i != 0)
{
outputString.append(",");
}

outputString.append(generateLevelJSON.toJsonString(clone));
}

// Add version string
outputString.append("],\"version\": \"").append(this.addon.getDescription().getVersion()).append("\"}");

// Store to file.
new FileWriter(defaultFile).write(outputString.toString());
}
}
catch (IOException e)
{
this.addon.logError("Could not save json file: " + e.getMessage());
}
}
}

0 comments on commit 188e86d

Please sign in to comment.