From b92d412f0a50f727c2fb350a187c1176dbd134ef Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 21 Apr 2020 17:24:30 -0700 Subject: [PATCH] Implements better value command Checks world value Fixes https://github.com/BentoBoxWorld/Level/issues/148 --- .../level/calculators/CalcIslandLevel.java | 16 ++++++-------- .../commands/island/IslandValueCommand.java | 21 +++++++++---------- .../bentobox/level/config/BlockConfig.java | 20 ++++++++++++++++++ 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java b/src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java index 89f1666..c38ba7a 100644 --- a/src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java +++ b/src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java @@ -219,17 +219,13 @@ public class CalcIslandLevel { * @return value of a material */ private int getValue(Material md) { - // Check world settings - if (addon.getBlockConfig().getWorldBlockValues().containsKey(world) && addon.getBlockConfig().getWorldBlockValues().get(world).containsKey(md)) { - return addon.getBlockConfig().getWorldBlockValues().get(world).get(md); + Integer value = addon.getBlockConfig().getValue(world, md); + if (value == null) { + // Not in config + result.ncCount.add(md); + return 0; } - // Check baseline - if (addon.getBlockConfig().getBlockValues().containsKey(md)) { - return addon.getBlockConfig().getBlockValues().get(md); - } - // Not in config - result.ncCount.add(md); - return 0; + return value; } /** diff --git a/src/main/java/world/bentobox/level/commands/island/IslandValueCommand.java b/src/main/java/world/bentobox/level/commands/island/IslandValueCommand.java index 51b04bf..be3a04f 100644 --- a/src/main/java/world/bentobox/level/commands/island/IslandValueCommand.java +++ b/src/main/java/world/bentobox/level/commands/island/IslandValueCommand.java @@ -10,11 +10,11 @@ import world.bentobox.level.Level; import java.util.List; public class IslandValueCommand extends CompositeCommand { - private final Level plugin; + private final Level addon; - public IslandValueCommand(Level plugin, CompositeCommand parent) { + public IslandValueCommand(Level addon, CompositeCommand parent) { super(parent, "value"); - this.plugin = plugin; + this.addon = addon; } @Override @@ -30,14 +30,12 @@ public class IslandValueCommand extends CompositeCommand { PlayerInventory inventory = player.getInventory(); if (!inventory.getItemInMainHand().getType().equals(Material.AIR)) { Material material = inventory.getItemInMainHand().getType(); - if (plugin.getConfig().get("blocks." + material.toString()) != null) { - int value = plugin.getConfig().getInt("blocks." + material.toString()); - user.sendMessage("island.value.success", "[value]", value + ""); - if (plugin.getConfig().get("underwater") != null) { - double underWater = plugin.getConfig().getDouble("underwater"); - if (underWater > 1.0) { - user.sendMessage("island.value.success-underwater", "[value]", (underWater * value) + ""); - } + Integer value = addon.getBlockConfig().getValue(getWorld(), material); + if (value != null) { + user.sendMessage("island.value.success", "[value]", String.valueOf(value)); + double underWater = addon.getSettings().getUnderWaterMultiplier(); + if (underWater > 1.0) { + user.sendMessage("island.value.success-underwater", "[value]", (underWater * value) + ""); } } else { user.sendMessage("island.value.no-value"); @@ -47,4 +45,5 @@ public class IslandValueCommand extends CompositeCommand { } return true; } + } diff --git a/src/main/java/world/bentobox/level/config/BlockConfig.java b/src/main/java/world/bentobox/level/config/BlockConfig.java index b19f2c2..9078af0 100644 --- a/src/main/java/world/bentobox/level/config/BlockConfig.java +++ b/src/main/java/world/bentobox/level/config/BlockConfig.java @@ -107,4 +107,24 @@ public class BlockConfig { public Map> getWorldBlockValues() { return worldBlockValues; } + + /** + * Get the value of material in world + * @param world - world + * @param md - material + * @return value or null if not configured with a value + */ + public Integer getValue(World world, Material md) { + // Check world settings + if (getWorldBlockValues().containsKey(world) && getWorldBlockValues().get(world).containsKey(md)) { + return getWorldBlockValues().get(world).get(md); + } + // Check baseline + if (getBlockValues().containsKey(md)) { + return getBlockValues().get(md); + } + return null; + } + + }