From cce4a655c6bdb74be918d5497704a9bb1815d761 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 4 Jun 2023 08:55:02 -0700 Subject: [PATCH] Refactored to reduce complexity --- .../greenhouses/greenhouse/BiomeRecipe.java | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java index 998a0d5..83b5412 100644 --- a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java +++ b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java @@ -210,23 +210,29 @@ public class BiomeRecipe implements Comparable { */ private Set checkRecipeAsync(CompletableFuture> r, Greenhouse gh) { AsyncWorldCache cache = new AsyncWorldCache(addon, gh.getWorld()); - Set result = new HashSet<>(); long area = gh.getArea(); - Map blockCount = new EnumMap<>(Material.class); // Look through the greenhouse and count what is in there - for (int y = gh.getFloorHeight(); y< gh.getCeilingHeight();y++) { - for (int x = (int) (gh.getBoundingBox().getMinX()+1); x < gh.getBoundingBox().getMaxX(); x++) { - for (int z = (int) (gh.getBoundingBox().getMinZ()+1); z < gh.getBoundingBox().getMaxZ(); z++) { - Material t = cache.getBlockType(x, y, z); - if (!t.equals(Material.AIR)) { - blockCount.putIfAbsent(t, 0); - blockCount.merge(t, 1, Integer::sum); - } - } - } + Map blockCount = countBlocks(gh, cache); + + // Calculate % water, ice and lava ratios and check them + Set result = checkRatios(blockCount, area); + + // Compare to the required blocks + Map missingBlocks = requiredBlocks.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() - blockCount.getOrDefault(e.getKey(), 0))); + // Remove any entries that are 0 or less + missingBlocks.values().removeIf(v -> v <= 0); + if (!missingBlocks.isEmpty()) { + result.add(GreenhouseResult.FAIL_INSUFFICIENT_BLOCKS); + gh.setMissingBlocks(missingBlocks); } - // Calculate % water, ice and lava ratios + // Return to main thread to complete + Bukkit.getScheduler().runTask(addon.getPlugin(), () -> r.complete(result)); + return result; + } + + private Set checkRatios(Map blockCount, long area) { + Set result = new HashSet<>(); double waterRatio = (double)blockCount.getOrDefault(Material.WATER, 0)/area * 100; double lavaRatio = (double)blockCount.getOrDefault(Material.LAVA, 0)/area * 100; int ice = blockCount.entrySet().stream().filter(en -> en.getKey().equals(Material.ICE) @@ -254,19 +260,25 @@ public class BiomeRecipe implements Comparable { if (iceCoverage > 0 && iceRatio < iceCoverage) { result.add(GreenhouseResult.FAIL_INSUFFICIENT_ICE); } - // Compare to the required blocks - Map missingBlocks = requiredBlocks.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() - blockCount.getOrDefault(e.getKey(), 0))); - // Remove any entries that are 0 or less - missingBlocks.values().removeIf(v -> v <= 0); - if (!missingBlocks.isEmpty()) { - result.add(GreenhouseResult.FAIL_INSUFFICIENT_BLOCKS); - gh.setMissingBlocks(missingBlocks); - } - // Return to main thread to complete - Bukkit.getScheduler().runTask(addon.getPlugin(), () -> r.complete(result)); return result; } + private Map countBlocks(Greenhouse gh, AsyncWorldCache cache) { + Map blockCount = new EnumMap<>(Material.class); + for (int y = gh.getFloorHeight(); y< gh.getCeilingHeight();y++) { + for (int x = (int) (gh.getBoundingBox().getMinX()+1); x < gh.getBoundingBox().getMaxX(); x++) { + for (int z = (int) (gh.getBoundingBox().getMinZ()+1); z < gh.getBoundingBox().getMaxZ(); z++) { + Material t = cache.getBlockType(x, y, z); + if (!t.equals(Material.AIR)) { + blockCount.putIfAbsent(t, 0); + blockCount.merge(t, 1, Integer::sum); + } + } + } + } + return blockCount; + } + /** * Check if block should be converted * @param b - block to check