Skip to content

Commit

Permalink
Add ability to change Biome outside BentoBox Plugin hierarchy (#30).
Browse files Browse the repository at this point in the history
To achieve this, I implemented 3 request handlers:
1. BiomeDataRequestHandler will return all data about requested biomeId.
2. BiomeListRequestHandler will return a list of all biomes uniqueIds for requested world.
3. ChangeBiomeRequestHandler will try to change biome with requested input data and return boolean status and string reason.
  • Loading branch information
BONNe committed Jun 12, 2019
1 parent ecaaff3 commit 7a2f95b
Show file tree
Hide file tree
Showing 4 changed files with 358 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main/java/world/bentobox/biomes/BiomesAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.biomes.commands.admin.AdminCommand;
import world.bentobox.biomes.commands.user.BiomesCommand;
import world.bentobox.biomes.handlers.BiomeDataRequestHandler;
import world.bentobox.biomes.handlers.BiomeListRequestHandler;
import world.bentobox.biomes.handlers.ChangeBiomeRequestHandler;
import world.bentobox.biomes.listeners.ChangeOwnerListener;
import world.bentobox.biomes.config.Settings;
import world.bentobox.level.Level;
Expand Down Expand Up @@ -120,11 +123,14 @@ public void onEnable()
this.registerListener(new ChangeOwnerListener(this));

// Register Flags
this.getPlugin().getFlagsManager().registerFlag(BIOMES_WORLD_PROTECTION);
this.getPlugin().getFlagsManager().registerFlag(BIOMES_ISLAND_PROTECTION);
this.registerFlag(BIOMES_WORLD_PROTECTION);
this.registerFlag(BIOMES_ISLAND_PROTECTION);

// Register Request Handlers
//this.registerRequestHandler(YOUR_REQUEST_HANDLER);
this.registerRequestHandler(new BiomeDataRequestHandler(this));
this.registerRequestHandler(new BiomeListRequestHandler(this));

this.registerRequestHandler(new ChangeBiomeRequestHandler(this));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package world.bentobox.biomes.handlers;


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
import world.bentobox.biomes.BiomesAddon;
import world.bentobox.biomes.database.objects.BiomesObject;


/**
* This handler returns data for requested challenge.
*/
public class BiomeDataRequestHandler extends AddonRequestHandler
{

/**
* Constructor creates a new BiomeDataRequestHandler instance.
*
* @param addon of type BiomesAddon
*/
public BiomeDataRequestHandler(BiomesAddon addon)
{
super("biome-data");
this.addon = addon;
}


/**
* @param metaData Required meta data.
* @return Map that returns information about biome
* @see AddonRequestHandler#handle(Map<String, Object>)
*/
@Override
public Object handle(Map<String, Object> metaData)
{
/*
What we need in the metaData:
0. "biomeId" -> String
What we will return:
- Empty Map if biome is not given or not found
- Map that contains information about given biome:
- uniqueId: the same id that was passed to this handler.
- world: string that represents world name where biome operates.
- biome: string that represents Minecraft Biome name.
- name: String object of display name for biome.
- deployed: boolean object of deployment status.
- description: List of strings that represents biomes description.
- icon: ItemStack object that represents biome.
- order: Integer object of order number for given biome.
- cost: Integer that represents biomes change cost.
- level: Long that represents minimal island level for this biome to work.
- permissions: Set of strings that represents required permissions.
*/

if (metaData == null ||
metaData.isEmpty() ||
metaData.get("biomeId") == null ||
!(metaData.get("biomeId") instanceof String))
{
return Collections.emptyMap();
}

BiomesObject biome =
this.addon.getAddonManager().getBiomeFromString((String) metaData.get("biomeId"));

Map<String, Object> biomesDataMap;

if (biome == null)
{
biomesDataMap = Collections.emptyMap();
}
else
{
biomesDataMap = new HashMap<>();

biomesDataMap.put("uniqueId", biome.getUniqueId());
biomesDataMap.put("world", biome.getWorld());
biomesDataMap.put("biome", biome.getBiome().name());

biomesDataMap.put("name", biome.getFriendlyName());
biomesDataMap.put("description", biome.getDescription());
biomesDataMap.put("deployed", biome.isDeployed());

biomesDataMap.put("icon", biome.getIcon());
biomesDataMap.put("order", biome.getOrder());

biomesDataMap.put("cost", biome.getRequiredCost());
biomesDataMap.put("level", biome.getRequiredLevel());
biomesDataMap.put("permissions", biome.getRequiredPermissions());
}

return biomesDataMap;
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------


/**
* Variable stores biomes addon.
*/
private BiomesAddon addon;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package world.bentobox.biomes.handlers;


import org.bukkit.Bukkit;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
import world.bentobox.biomes.BiomesAddon;
import world.bentobox.biomes.database.objects.BiomesObject;


/**
* This handler returns all biomes uniqueIDs that is operating in given world.
*/
public class BiomeListRequestHandler extends AddonRequestHandler
{
/**
* Constructor creates a new BiomeListRequestHandler instance.
*
* @param addon of type BiomesAddon
*/
public BiomeListRequestHandler(BiomesAddon addon)
{
super("biomes-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 biomes 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 list of biomes unique IDs from given world.

return this.addon.getAddonManager().
getBiomes(Bukkit.getWorld((String) metaData.get("world-name"))).
stream().map(BiomesObject::getUniqueId).collect(Collectors.toList());
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------


/**
* Variable stores challenges addon.
*/
private BiomesAddon addon;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package world.bentobox.biomes.handlers;


import org.bukkit.Bukkit;
import org.bukkit.World;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import world.bentobox.bentobox.api.addons.request.AddonRequestHandler;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.biomes.BiomesAddon;
import world.bentobox.biomes.config.Settings;
import world.bentobox.biomes.database.objects.BiomesObject;
import world.bentobox.biomes.tasks.BiomeUpdateHelper;


/**
* This Request Handler returns if requested inputdata can be enough to change biome.
*/
public class ChangeBiomeRequestHandler extends AddonRequestHandler
{
/**
* Constructor creates a new ChangeBiomeRequestHandler instance.
*
* @param addon of type ChallengesAddon
*/
public ChangeBiomeRequestHandler(BiomesAddon addon)
{
super("biome-request-change");
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 that represents targeted player UUID.
1. "world-name" -> String that represents world name where biome must be changed
2. "biomeId" -> String that represents biome unique ID.
What you can specify more in metaData:
0. "updateMode" -> String that represents how biome is necessary to be changed.
1. "range" -> Integer that represents range of biome update change.
2. "checkRequirements" -> Boolean that represents if requirements must be checked or not. By default it is true.
3. "withdraw" -> Boolean that indicates that money will be withdraw from players account. By default it is true.
What we will return:
- Map that contains:
1. key "status" which is boolean that indicate if biome change was successful.
2. key "reason" which is string that returns errror message.
*/


Map<String, Object> returnMap = new HashMap<>(2);

if (metaData == null || metaData.isEmpty())
{
returnMap.put("status", false);
returnMap.put("reason", "Given MetaData map is not defined!");
}
else if (!metaData.containsKey("world-name") ||
!(metaData.get("world-name") instanceof String) ||
Bukkit.getWorld((String) metaData.get("world-name")) == null)
{
returnMap.put("status", false);
returnMap.put("reason", "Missing 'world-name' or it is not valid!");
}
else if (!metaData.containsKey("player") ||
!(metaData.get("player") instanceof UUID))
{
returnMap.put("status", false);
returnMap.put("reason", "Missing 'player' or it is not valid!");
}
else if (!metaData.containsKey("biomeId") ||
!(metaData.get("biomeId") instanceof String) ||
this.addon.getAddonManager().getBiomeFromString((String) metaData.get("biomeId")) == null)
{
returnMap.put("status", false);
returnMap.put("reason", "Missing 'biomeId' or it is not valid!");
}
else
{
World world = Bukkit.getWorld((String) metaData.get("world-name"));
UUID player = (UUID) metaData.get("player");
BiomesObject biome = this.addon.getAddonManager().
getBiomeFromString((String) metaData.get("biomeId"));

// Get Update Mode.

Settings.UpdateMode mode = metaData.containsKey("updateMode") &&
metaData.get("updateMode") instanceof String &&
Settings.UpdateMode.getMode((String) metaData.get("updateMode")) != null ?
Settings.UpdateMode.getMode((String) metaData.get("updateMode")) :
this.addon.getSettings().getDefaultMode();

// Get Update Range.

int range = metaData.containsKey("range") &&
metaData.get("range") instanceof Integer ? (int) metaData.get("range") :
this.addon.getSettings().getDefaultSize();

// Get Requirement Checking

boolean checkRequirements = !metaData.containsKey("checkRequirements") ||
!(metaData.get("checkRequirements") instanceof Boolean) ||
(boolean) metaData.get("checkRequirements");

// Get Withdraw value

boolean withdraw = !metaData.containsKey("withdraw") ||
!(metaData.get("withdraw") instanceof Boolean) ||
(boolean) metaData.get("withdraw");

BiomeUpdateHelper helper = new BiomeUpdateHelper(this.addon,
User.getInstance(player),
User.getInstance(player),
biome,
world,
mode,
range,
withdraw);

if (checkRequirements)
{
if (helper.canChangeBiome())
{
helper.updateIslandBiome();

returnMap.put("status", true);
returnMap.put("reason", "Biome is updated by checking all requirements!");
}
else
{
returnMap.put("status", false);
returnMap.put("reason", "Player does not met requirements for biome changing!");
}
}
else
{
helper.updateIslandBiome();

returnMap.put("status", true);
returnMap.put("reason", "Biome is updated by skipping all requirements!");
}
}

return returnMap;
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------


/**
* Variable stores biomes addon.
*/
private BiomesAddon addon;
}

0 comments on commit 7a2f95b

Please sign in to comment.