-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to change Biome outside BentoBox Plugin hierarchy (#30).
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
Showing
4 changed files
with
358 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/world/bentobox/biomes/handlers/BiomeDataRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/world/bentobox/biomes/handlers/BiomeListRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
166 changes: 166 additions & 0 deletions
166
src/main/java/world/bentobox/biomes/handlers/ChangeBiomeRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |