Skip to content

Commit

Permalink
Start to implement default challenge loading.
Browse files Browse the repository at this point in the history
Work in progress.
  • Loading branch information
BONNe committed Apr 24, 2019
1 parent f5cf5f5 commit 4b233e2
Showing 1 changed file with 232 additions and 0 deletions.
232 changes: 232 additions & 0 deletions src/main/java/world/bentobox/challenges/ChallengesImportManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package world.bentobox.challenges;

import java.beans.IntrospectionException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

import org.bukkit.Material;
Expand All @@ -11,7 +13,11 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.DatabaseConnector;
import world.bentobox.bentobox.database.json.AbstractJSONDatabaseHandler;
import world.bentobox.bentobox.util.ItemParser;
import world.bentobox.challenges.database.object.ChallengeLevel;
import world.bentobox.challenges.database.object.Challenge;
Expand Down Expand Up @@ -198,4 +204,230 @@ private List<ItemStack> parseItems(String reqList) {
}
return result;
}


// ---------------------------------------------------------------------
// Section: Default Challenge Loader
// ---------------------------------------------------------------------


/**
* This method loads default challenges into memory.
* @param user User who calls default challenge loading
* @param world Target world.
* @return <code>true</code> if everything was successful, otherwise <code>false</code>.
*/
private boolean loadDefaultChallenges(User user, World world)
{
ChallengesManager manager = this.addon.getChallengesManager();

// If exist any challenge or level that is bound to current world, then do not load default challenges.
if (manager.hasAnyChallengeData(world.getName()))
{
if (user.isPlayer())
{
user.sendMessage("challenges.errors.exist-challenges-or-levels");
}
else
{
this.addon.logWarning("challenges.errors.exist-challenges-or-levels");
}

return false;
}

// Safe json configuration to Challenges folder.
this.addon.saveResource("default.json", true);

// Init Connector
CustomJSONConnector connector = new CustomJSONConnector();

// Load challenges
CustomJSONHandler<Challenge> challengeHandler =
new CustomJSONHandler<>(BentoBox.getInstance(), Challenge.class, connector);

// Load levels
CustomJSONHandler<ChallengeLevel> levelHandler =
new CustomJSONHandler<>(BentoBox.getInstance(), ChallengeLevel.class, connector);

try
{
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)
{
e.printStackTrace();
return false;
}

this.addon.getChallengesManager().save();

// Remove default.yml file from resources to avoid interacting with it.
new File(this.addon.getDataFolder(), "default.json").delete();

return true;
}


/**
* This Class allows to load default challenges and their levels as objects much easier.
*/
private final class CustomJSONHandler<T> extends AbstractJSONDatabaseHandler<T>
{
/**
* {@inheritDoc}
*/
CustomJSONHandler(BentoBox plugin, Class<T> type, DatabaseConnector databaseConnector)
{
super(plugin, type, databaseConnector);
}


/**
* {@inheritDoc}
*/
@Override
public List<T> loadObjects() throws
InstantiationException,
IllegalAccessException,
InvocationTargetException,
ClassNotFoundException,
IntrospectionException,
NoSuchMethodException
{
return Collections.emptyList();
}


/**
* {@inheritDoc}
*/
@Override
public T loadObject(@NonNull String s) throws
InstantiationException,
IllegalAccessException,
InvocationTargetException,
ClassNotFoundException,
IntrospectionException,
NoSuchMethodException
{
return null;
}


/**
* {@inheritDoc}
*/
@Override
public void saveObject(T t)
throws IllegalAccessException, InvocationTargetException, IntrospectionException
{
// Will not be used in default challenge loading process.
}


/**
* {@inheritDoc}
*/
@Override
public void deleteObject(T t)
throws IllegalAccessException, InvocationTargetException, IntrospectionException
{
// Will not be used in default challenge loading process.
}


/**
* {@inheritDoc}
*/
@Override
public boolean objectExists(String s)
{
// Will not be used in default challenge loading process.
return false;
}


/**
* {@inheritDoc}
*/
@Override
public void close()
{
// Will not be used in default challenge loading process.
}


/**
* {@inheritDoc}
*/
@Override
public void deleteID(String s)
{
// Will not be used in default challenge loading process.
}
}


/**
* Simple custom connector used for importing default challenges
*/
private final class CustomJSONConnector implements DatabaseConnector
{
/**
* {@inheritDoc}
*/
@Override
public Object createConnection()
{
return null;
}



/**
* {@inheritDoc}
*/
@Override
public void closeConnection()
{

}


/**
* {@inheritDoc}
*/
@Override
public String getConnectionUrl()
{
return null;
}


/**
* {@inheritDoc}
*/
@Override
public String getUniqueId(String s)
{
return null;
}


/**
* {@inheritDoc}
*/
@Override
public boolean uniqueIdExists(String s, String s1)
{
return false;
}
}
}

0 comments on commit 4b233e2

Please sign in to comment.