Create Challenges Addon Request Handlers.

- Challenge Data Request handler - returns data map about requested challenge;
- Level Data Request handler - returns data map about requested level;
- Challenge List Request handler - returns list of challenges that operates in requested world;
- Level List Request handler - returns list of levels that operates in requested world;
- Completed Challenges Request handler - returns set of completed challenges for requested user in requested world.
This commit is contained in:
BONNe1704 2019-02-22 12:56:47 +02:00
parent ddd43d27e6
commit dd1e689ace
5 changed files with 417 additions and 0 deletions

View File

@ -0,0 +1,106 @@
package world.bentobox.challenges.handlers;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.database.object.Challenge;
/**
* This handler returns data for requested challenge.
*/
public class ChallengeDataRequestHandler extends AddonRequestHandler
{
/**
* Constructor creates a new ChallengesDataRequestHandler instance.
*
* @param addon of type ChallengesAddon
*/
public ChallengeDataRequestHandler(ChallengesAddon addon)
{
super("challenge-data");
this.addon = addon;
}
/**
* @param metaData Required meta data.
* @return Map that returns information about challenges
* @see AddonRequestHandler#handle(Map<String, Object>)
*/
@Override
public Object handle(Map<String, Object> metaData)
{
/*
What we need in the metaData:
0. "challenge-name" -> String
What we will return:
- Empty Map if challenge is not given or not found
- Map that contains information about given challenge:
- uniqueId: the same id that was passed to this handler.
- name: String object of display name for challenge.
- icon: ItemStack object that represents challenge.
- levelId: String object of levelId that this challenge is linked.
- order: Integer object of order number for given challenge.
- deployed: boolean object of deployment status.
- description: List of strings that represents challenges description.
- type: String object that represents challenges type.
- repeatable: boolean object of repeatable option.
- maxTimes: Integer object that represents how many times challenge can be completed.
*/
if (metaData == null ||
metaData.isEmpty() ||
metaData.get("challenge-name") == null ||
!(metaData.get("challenge-name") instanceof String))
{
return Collections.emptyMap();
}
Challenge challenge = this.addon.getChallengesManager().getChallenge((String) metaData.get("challenge-name"));
Map<String, Object> challengeDataMap;
if (challenge == null)
{
challengeDataMap = Collections.emptyMap();
}
else
{
challengeDataMap = new HashMap<>();
challengeDataMap.put("uniqueId", challenge.getUniqueId());
challengeDataMap.put("name", challenge.getFriendlyName());
challengeDataMap.put("icon", challenge.getIcon());
challengeDataMap.put("levelId", challenge.getLevel());
challengeDataMap.put("order", challenge.getOrder());
challengeDataMap.put("deployed", challenge.isDeployed());
challengeDataMap.put("description", challenge.getDescription());
challengeDataMap.put("type", challenge.getChallengeType().toString());
challengeDataMap.put("repeatable", challenge.isRepeatable());
challengeDataMap.put("maxTimes", challenge.isRepeatable() ? challenge.getMaxTimes() : 1);
}
return challengeDataMap;
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* Variable stores challenges addon.
*/
private ChallengesAddon addon;
}

View File

@ -0,0 +1,66 @@
package world.bentobox.challenges.handlers;
import org.bukkit.Bukkit;
import java.util.Collections;
import java.util.Map;
import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
import world.bentobox.challenges.ChallengesAddon;
/**
* This handler returns all challenges that is operating in given world.
*/
public class ChallengeListRequestHandler extends AddonRequestHandler
{
/**
* Constructor creates a new CompletedChallengesRequestHandler instance.
*
* @param addon of type ChallengesAddon
*/
public ChallengeListRequestHandler(ChallengesAddon addon)
{
super("challenge-list");
this.addon = addon;
}
/**
* @param metaData Required meta data.
* @return Set of strings that contains completed challenges.
* @see AddonRequestHandler#handle(Map <String, Object>)
*/
@Override
public Object handle(Map<String, Object> metaData)
{
/*
What we need in the metaData:
0. "world-name" -> String
What we will return:
- List of challenges in given world.
*/
if (metaData == null ||
metaData.isEmpty() ||
metaData.get("world-name") == null ||
!(metaData.get("world-name") instanceof String) ||
Bukkit.getWorld((String) metaData.get("world-name")) == null)
{
return Collections.emptyList();
}
return this.addon.getChallengesManager().getAllChallengesNames(Bukkit.getWorld((String) metaData.get("world-name")));
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* Variable stores challenges addon.
*/
private ChallengesAddon addon;
}

View File

@ -0,0 +1,80 @@
package world.bentobox.challenges.handlers;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.util.*;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.ChallengesManager;
/**
* This Request Handler returns completed challenges for user in given world.
*/
public class CompletedChallengesRequestHandler extends AddonRequestHandler
{
/**
* Constructor creates a new CompletedChallengesRequestHandler instance.
*
* @param addon of type ChallengesAddon
*/
public CompletedChallengesRequestHandler(ChallengesAddon addon)
{
super("completed-challenges");
this.addon = addon;
}
/**
* @param metaData Required meta data.
* @return Set of strings that contains completed challenges.
* @see AddonRequestHandler#handle(Map<String, Object>)
*/
@Override
public Object handle(Map<String, Object> metaData)
{
/*
What we need in the metaData:
0. "player" -> UUID
1. "world-name" -> String
What we will return:
- Empty Set if player or given world is not valid.
- Set of completed challenges in given world (or empty list if user haven't completed any challenge)
*/
if (metaData == null ||
metaData.isEmpty() ||
metaData.get("world-name") == null ||
!(metaData.get("world-name") instanceof String) ||
metaData.get("player") == null ||
!(metaData.get("player") instanceof UUID) ||
Bukkit.getWorld((String) metaData.get("world-name")) == null)
{
return Collections.emptySet();
}
World world = Bukkit.getWorld((String) metaData.get("world-name"));
UUID player = (UUID) metaData.get("player");
ChallengesManager manager = this.addon.getChallengesManager();
return manager.getAllChallengesNames(world).stream().
filter(challenge -> manager.isChallengeComplete(player, world, challenge)).
collect(Collectors.toSet());
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* Variable stores challenges addon.
*/
private ChallengesAddon addon;
}

View File

@ -0,0 +1,98 @@
package world.bentobox.challenges.handlers;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.database.object.ChallengeLevel;
/**
* This handler returns Data map of requested level.
*/
public class LevelDataRequestHandler extends AddonRequestHandler
{
/**
* Constructor creates a new LevelDataRequestHandler instance.
*
* @param addon of type ChallengesAddon
*/
public LevelDataRequestHandler(ChallengesAddon addon)
{
super("level-data");
this.addon = addon;
}
/**
* @param metaData Required meta data.
* @return Map that returns information about level
* @see AddonRequestHandler#handle(Map <String, Object>)
*/
@Override
public Object handle(Map<String, Object> metaData)
{
/*
What we need in the metaData:
0. "level-name" -> String
What we will return:
- Empty Map if level is not given or not found
- Map that contains information about given level:
- uniqueId: the same id that was passed to this handler.
- name: String object of display name for level.
- icon: ItemStack object that represents level.
- order: Integer object of order number for given level.
- message: String object that represents level unlock message.
- world: String object that represents world name where level operates.
- waiveramount: Integer object of waiver amount for given level.
- challenges: List of strings that represents challenges that is owned by given level.
*/
if (metaData == null ||
metaData.isEmpty() ||
metaData.get("level-name") == null ||
!(metaData.get("level-name") instanceof String))
{
return Collections.emptyMap();
}
ChallengeLevel level = this.addon.getChallengesManager().getLevel((String) metaData.get("level-name"));
Map<String, Object> levelDataMap;
if (level == null)
{
levelDataMap = Collections.emptyMap();
}
else
{
levelDataMap = new HashMap<>();
levelDataMap.put("uniqueId", level.getUniqueId());
levelDataMap.put("name", level.getFriendlyName());
levelDataMap.put("icon", level.getIcon());
levelDataMap.put("order", level.getOrder());
levelDataMap.put("message", level.getUnlockMessage());
levelDataMap.put("world", level.getWorld());
levelDataMap.put("challenges", level.getChallenges());
levelDataMap.put("waiveramount", level.getWaiverAmount());
}
return levelDataMap;
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* Variable stores challenges addon.
*/
private ChallengesAddon addon;
}

View File

@ -0,0 +1,67 @@
package world.bentobox.challenges.handlers;
import org.bukkit.Bukkit;
import java.util.Collections;
import java.util.Map;
import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
import world.bentobox.challenges.ChallengesAddon;
/**
* This handler returns all levels that is working in given world.
*/
public class LevelListRequestHandler extends AddonRequestHandler
{
/**
* Constructor creates a new CompletedChallengesRequestHandler instance.
*
* @param addon of type ChallengesAddon
*/
public LevelListRequestHandler(ChallengesAddon addon)
{
super("level-list");
this.addon = addon;
}
/**
* @param metaData Required meta data.
* @return List of strings that contains levels in given world
* @see AddonRequestHandler#handle(Map <String, Object>)
*/
@Override
public Object handle(Map<String, Object> metaData)
{
/*
What we need in the metaData:
0. "world-name" -> String
What we will return:
- List of levels in given world.
*/
if (metaData == null ||
metaData.isEmpty() ||
metaData.get("world-name") == null ||
!(metaData.get("world-name") instanceof String) ||
Bukkit.getWorld((String) metaData.get("world-name")) == null)
{
return Collections.emptyList();
}
return this.addon.getChallengesManager().getLevels(Bukkit.getWorld((String) metaData.get("world-name")));
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* Variable stores challenges addon.
*/
private ChallengesAddon addon;
}