diff --git a/src/main/java/world/bentobox/level/Level.java b/src/main/java/world/bentobox/level/Level.java index 7b052fd..f74a052 100644 --- a/src/main/java/world/bentobox/level/Level.java +++ b/src/main/java/world/bentobox/level/Level.java @@ -192,7 +192,6 @@ public class Level extends Addon implements Listener { // Disable logError("Level blockconfig.yml settings could not load! Addon disabled."); setState(State.DISABLED); - return; } } diff --git a/src/main/java/world/bentobox/level/LevelsManager.java b/src/main/java/world/bentobox/level/LevelsManager.java index 79e576b..cac2994 100644 --- a/src/main/java/world/bentobox/level/LevelsManager.java +++ b/src/main/java/world/bentobox/level/LevelsManager.java @@ -455,7 +455,7 @@ public class LevelsManager { * @param owner - owner of the island * @param r - results of the calculation */ - private void setIslandResults(World world, @Nullable UUID owner, Results r) { + private void setIslandResults(World world, @NonNull UUID owner, Results r) { LevelsData ld = levelsCache.computeIfAbsent(owner, LevelsData::new); ld.setLevel(world, r.getLevel()); ld.setUwCount(world, r.getUwCount()); diff --git a/src/main/java/world/bentobox/level/config/BlockConfig.java b/src/main/java/world/bentobox/level/config/BlockConfig.java index 9078af0..2180048 100644 --- a/src/main/java/world/bentobox/level/config/BlockConfig.java +++ b/src/main/java/world/bentobox/level/config/BlockConfig.java @@ -3,6 +3,7 @@ package world.bentobox.level.config; import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.EnumMap; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -15,67 +16,93 @@ import org.bukkit.configuration.file.YamlConfiguration; import world.bentobox.level.Level; +/** + * Contains all the block values, limits and world differences + * @author tastybento + * + */ public class BlockConfig { - private Map blockLimits = new HashMap<>(); - private Map blockValues = new HashMap<>(); + private Map blockLimits = new EnumMap<>(Material.class); + private Map blockValues = new EnumMap<>(Material.class); private final Map> worldBlockValues = new HashMap<>(); + private Level addon; - public BlockConfig(Level level, YamlConfiguration blockValues, File file) throws IOException { - + /** + * Loads block limits, values and world settings and then saves them again + * @param addon - addon + * @param blockValues - yaml configuration file containing the block values + * @param file - the file representing the yaml config. Will be saved after readong. + * @throws IOException - if there is an error + */ + public BlockConfig(Level addon, YamlConfiguration blockValues, File file) throws IOException { + this.addon = addon; if (blockValues.isConfigurationSection("limits")) { - HashMap bl = new HashMap<>(); - ConfigurationSection limits = Objects.requireNonNull(blockValues.getConfigurationSection("limits")); - for (String material : limits.getKeys(false)) { - try { - Material mat = Material.valueOf(material); - bl.put(mat, limits.getInt(material, 0)); - } catch (Exception e) { - level.logWarning("Unknown material (" + material + ") in blockconfig.yml Limits section. Skipping..."); - } - } - setBlockLimits(bl); + setBlockLimits(loadBlockLimits(blockValues)); } if (blockValues.isConfigurationSection("blocks")) { - ConfigurationSection blocks = Objects.requireNonNull(blockValues.getConfigurationSection("blocks")); - Map bv = new HashMap<>(); - // Update blockvalues to latest settings - Arrays.stream(Material.values()).filter(Material::isBlock) - .filter(m -> !m.name().startsWith("LEGACY_")) - .filter(m -> !m.isAir()) - .filter(m -> !m.equals(Material.WATER)) - .forEach(m -> { - if (!blocks.contains(m.name(), true)) { - blocks.set(m.name(), 1); - } - bv.put(m, blocks.getInt(m.name(), 1)); - }); - setBlockValues(bv); + setBlockValues(loadBlockValues(blockValues)); } else { - level.logWarning("No block values in blockconfig.yml! All island levels will be zero!"); + addon.logWarning("No block values in blockconfig.yml! All island levels will be zero!"); } // Worlds if (blockValues.isConfigurationSection("worlds")) { - ConfigurationSection worlds = Objects.requireNonNull(blockValues.getConfigurationSection("worlds")); - for (String world : Objects.requireNonNull(worlds).getKeys(false)) { - World bWorld = Bukkit.getWorld(world); - if (bWorld != null) { - ConfigurationSection worldValues = worlds.getConfigurationSection(world); - for (String material : Objects.requireNonNull(worldValues).getKeys(false)) { - Material mat = Material.valueOf(material); - Map values = worldBlockValues.getOrDefault(bWorld, new HashMap<>()); - values.put(mat, worldValues.getInt(material)); - worldBlockValues.put(bWorld, values); - } - } else { - level.logWarning("Level Addon: No such world in blockconfig.yml : " + world); - } - } + loadWorlds(blockValues); } // All done blockValues.save(file); } + private void loadWorlds(YamlConfiguration blockValues2) { + ConfigurationSection worlds = Objects.requireNonNull(blockValues2.getConfigurationSection("worlds")); + for (String world : Objects.requireNonNull(worlds).getKeys(false)) { + World bWorld = Bukkit.getWorld(world); + if (bWorld != null) { + ConfigurationSection worldValues = worlds.getConfigurationSection(world); + for (String material : Objects.requireNonNull(worldValues).getKeys(false)) { + Material mat = Material.valueOf(material); + Map values = worldBlockValues.getOrDefault(bWorld, new EnumMap<>(Material.class)); + values.put(mat, worldValues.getInt(material)); + worldBlockValues.put(bWorld, values); + } + } else { + addon.logWarning("Level Addon: No such world in blockconfig.yml : " + world); + } + } + + } + + private Map loadBlockValues(YamlConfiguration blockValues2) { + ConfigurationSection blocks = Objects.requireNonNull(blockValues2.getConfigurationSection("blocks")); + Map bv = new EnumMap<>(Material.class); + // Update blockvalues to latest settings + Arrays.stream(Material.values()).filter(Material::isBlock) + .filter(m -> !m.name().startsWith("LEGACY_")) + .filter(m -> !m.isAir()) + .filter(m -> !m.equals(Material.WATER)) + .forEach(m -> { + if (!blocks.contains(m.name(), true)) { + blocks.set(m.name(), 1); + } + bv.put(m, blocks.getInt(m.name(), 1)); + }); + return bv; + } + + private Map loadBlockLimits(YamlConfiguration blockValues2) { + Map bl = new EnumMap<>(Material.class); + ConfigurationSection limits = Objects.requireNonNull(blockValues2.getConfigurationSection("limits")); + for (String material : limits.getKeys(false)) { + try { + Material mat = Material.valueOf(material); + bl.put(mat, limits.getInt(material, 0)); + } catch (Exception e) { + addon.logWarning("Unknown material (" + material + ") in blockconfig.yml Limits section. Skipping..."); + } + } + return bl; + } + /** * @return the blockLimits */ @@ -83,10 +110,10 @@ public class BlockConfig { return blockLimits; } /** - * @param blockLimits2 the blockLimits to set + * @param bl the blockLimits to set */ - private void setBlockLimits(HashMap blockLimits2) { - this.blockLimits = blockLimits2; + private void setBlockLimits(Map bl) { + this.blockLimits = bl; } /** * @return the blockValues diff --git a/src/main/java/world/bentobox/level/objects/LevelsData.java b/src/main/java/world/bentobox/level/objects/LevelsData.java index 71d3858..dfdfe00 100644 --- a/src/main/java/world/bentobox/level/objects/LevelsData.java +++ b/src/main/java/world/bentobox/level/objects/LevelsData.java @@ -1,6 +1,7 @@ package world.bentobox.level.objects; import java.util.Collections; +import java.util.EnumMap; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -213,7 +214,7 @@ public class LevelsData implements DataObject { */ public void setUwCount(World world, Multiset uwCount) { if (this.uwCount == null) this.uwCount = new HashMap<>(); - Map count = new HashMap<>(); + Map count = new EnumMap<>(Material.class); uwCount.forEach(m -> count.put(m, uwCount.count(m))); this.uwCount.put(world.getName(), count); @@ -224,7 +225,7 @@ public class LevelsData implements DataObject { */ public void setMdCount(World world, Multiset mdCount) { if (this.mdCount == null) this.mdCount = new HashMap<>(); - Map count = new HashMap<>(); + Map count = new EnumMap<>(Material.class); mdCount.forEach(m -> count.put(m, mdCount.count(m))); this.mdCount.put(world.getName(), count); diff --git a/src/main/java/world/bentobox/level/panels/DetailsGUITab.java b/src/main/java/world/bentobox/level/panels/DetailsGUITab.java index 0c7eef9..000cdd4 100644 --- a/src/main/java/world/bentobox/level/panels/DetailsGUITab.java +++ b/src/main/java/world/bentobox/level/panels/DetailsGUITab.java @@ -5,7 +5,7 @@ package world.bentobox.level.panels; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; +import java.util.EnumMap; import java.util.List; import java.util.Map; @@ -47,7 +47,7 @@ public class DetailsGUITab implements Tab, ClickHandler { */ private static final Map M2I; static { - Map m2i = new HashMap<>(); + Map m2i = new EnumMap<>(Material.class); m2i.put(Material.WATER, Material.WATER_BUCKET); m2i.put(Material.LAVA, Material.LAVA_BUCKET); m2i.put(Material.AIR, Material.BLACK_STAINED_GLASS_PANE); @@ -133,7 +133,7 @@ public class DetailsGUITab implements Tab, ClickHandler { items = new ArrayList<>(); LevelsData ld = addon.getManager().getLevelsData(island.getOwner()); // Get the items from the report - Map sumTotal = new HashMap<>(); + Map sumTotal = new EnumMap<>(Material.class); sumTotal.putAll(ld.getMdCount(world)); sumTotal.putAll(ld.getUwCount(world)); switch(type) { diff --git a/src/main/java/world/bentobox/level/requests/LevelRequestHandler.java b/src/main/java/world/bentobox/level/requests/LevelRequestHandler.java index dc257fe..b2eb390 100644 --- a/src/main/java/world/bentobox/level/requests/LevelRequestHandler.java +++ b/src/main/java/world/bentobox/level/requests/LevelRequestHandler.java @@ -10,6 +10,8 @@ import world.bentobox.level.Level; public class LevelRequestHandler extends AddonRequestHandler { + private static final Object WORLD_NAME = "world-name"; + private static final Object PLAYER = "player"; private final Level addon; public LevelRequestHandler(Level addon) { @@ -32,12 +34,12 @@ public class LevelRequestHandler extends AddonRequestHandler { */ if (map == null || map.isEmpty() - || map.get("world-name") == null || !(map.get("world-name") instanceof String) - || map.get("player") == null || !(map.get("player") instanceof UUID) - || Bukkit.getWorld((String) map.get("world-name")) == null) { + || map.get(WORLD_NAME) == null || !(map.get(WORLD_NAME) instanceof String) + || map.get(PLAYER) == null || !(map.get(PLAYER) instanceof UUID) + || Bukkit.getWorld((String) map.get(WORLD_NAME)) == null) { return 0L; } - return addon.getManager().getIslandLevel(Bukkit.getWorld((String) map.get("world-name")), (UUID) map.get("player")); + return addon.getManager().getIslandLevel(Bukkit.getWorld((String) map.get(WORLD_NAME)), (UUID) map.get(PLAYER)); } }