From 31bc36c470e5d774fd9cf7bf764e6966b90d34bd Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 12 Oct 2019 09:31:51 -0700 Subject: [PATCH 1/3] Adds recipe option to make command Completes missing block check. https://github.com/BentoBoxWorld/Greenhouses/issues/25 --- .../greenhouses/greenhouse/BiomeRecipe.java | 10 +---- .../greenhouses/ui/user/MakeCommand.java | 44 ++++++++++++++++++- src/main/resources/locales/en-US.yml | 3 ++ .../greenhouse/BiomeRecipeTest.java | 42 ++++++++++++++++-- 4 files changed, 86 insertions(+), 13 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java index 32d56d7..2065eea 100644 --- a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java +++ b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java @@ -57,7 +57,7 @@ public class BiomeRecipe implements Comparable { private String permission = ""; private final Random random = new Random(); - + public BiomeRecipe() {} @@ -154,7 +154,6 @@ public class BiomeRecipe implements Comparable { } } } - blockCount.forEach((k,v) -> System.out.println(k + " " + v)); // Calculate % water, ice and lava ratios double waterRatio = (double)blockCount.getOrDefault(Material.WATER, 0)/area * 100; double lavaRatio = (double)blockCount.getOrDefault(Material.LAVA, 0)/area * 100; @@ -181,17 +180,12 @@ public class BiomeRecipe implements Comparable { result.add(GreenhouseResult.FAIL_INSUFFICIENT_LAVA); } if (iceCoverage > 0 && iceRatio < iceCoverage) { - System.out.println("Ice coverage = " + iceCoverage + " and ice ratio = " + iceRatio); result.add(GreenhouseResult.FAIL_INSUFFICIENT_ICE); } - requiredBlocks.forEach((k,v) -> System.out.println("Req " + k + " " + v)); // Compare to the required blocks Map missingBlocks = requiredBlocks.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() - blockCount.getOrDefault(e.getKey(), 0))); - missingBlocks.forEach((k,v) -> System.out.println("Missing " + k + " " + v)); - // Remove any entries that are 0 or less missingBlocks.values().removeIf(v -> v <= 0); - missingBlocks.forEach((k,v) -> System.out.println("Missing after " + k + " " + v)); if (!missingBlocks.isEmpty()) { result.add(GreenhouseResult.FAIL_INSUFFICIENT_BLOCKS); gh.setMissingBlocks(missingBlocks); @@ -352,7 +346,7 @@ public class BiomeRecipe implements Comparable { bl.getRelative(BlockFace.UP).setBlockData(dataTop, false); } } else { - bl.setBlockData(dataBottom, false); + bl.setBlockData(dataBottom, false); } bl.getWorld().spawnParticle(Particle.SNOWBALL, bl.getLocation(), 10, 2, 2, 2); return true; diff --git a/src/main/java/world/bentobox/greenhouses/ui/user/MakeCommand.java b/src/main/java/world/bentobox/greenhouses/ui/user/MakeCommand.java index cdf6f65..ca08b1c 100644 --- a/src/main/java/world/bentobox/greenhouses/ui/user/MakeCommand.java +++ b/src/main/java/world/bentobox/greenhouses/ui/user/MakeCommand.java @@ -1,6 +1,10 @@ package world.bentobox.greenhouses.ui.user; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -8,7 +12,9 @@ import org.bukkit.Material; import org.bukkit.util.Vector; import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.util.Util; import world.bentobox.greenhouses.Greenhouses; import world.bentobox.greenhouses.greenhouse.BiomeRecipe; import world.bentobox.greenhouses.managers.GreenhouseManager.GhResult; @@ -49,7 +55,35 @@ class MakeCommand extends CompositeCommand { new Panel((Greenhouses)this.getAddon()).ShowPanel(user); return true; } - return makeGreenhouse(user, null); + // Check recipe given matches + BiomeRecipe br = getRecipe(user, args.get(0)); + if (br == null) { + user.sendMessage("greenhouses.commands.user.make.unknown-recipe"); + user.sendMessage("greenhouses.commands.user.make.try-these"); + getRecipes(user).forEach((k,v) -> user.sendMessage("greenhouses.commands.user.make.recipe-format", TextVariables.NAME, v.getName())); + return false; + } + return makeGreenhouse(user, br); + } + + /** + * Get a recipe for user + * @param user - user + * @param arg - given string + * @return recipe or null if unknown + */ + private BiomeRecipe getRecipe(User user, String arg) { + return getRecipes(user).get(arg); + } + /** + * Get a string list of recipies the player has permission to use + * @param user - user + * @return list + */ + private Map getRecipes(User user) { + return ((Greenhouses)getAddon()).getRecipes().getBiomeRecipes().stream() + .filter(br -> user.hasPermission(br.getPermission())) + .collect(Collectors.toMap(br -> br.getName(), br -> br)); } private boolean makeGreenhouse(User user, BiomeRecipe br) { @@ -77,7 +111,15 @@ class MakeCommand extends CompositeCommand { result.getFinder().getRedGlass().forEach(rg -> user.getPlayer().sendBlockChange(rg, Material.RED_STAINED_GLASS.createBlockData())); Bukkit.getScheduler().runTaskLater(getPlugin(), () -> result.getFinder().getRedGlass().forEach(rg -> user.getPlayer().sendBlockChange(rg, rg.getBlock().getBlockData())), 120L); } + if (br != null && result.getResults().contains(GreenhouseResult.FAIL_INSUFFICIENT_BLOCKS)) { + result.getFinder().getGh().getMissingBlocks().forEach((k,v) -> user.sendMessage("greenhouses.commands.user.make.missing-blocks", "[material]", Util.prettifyText(k.toString()), TextVariables.NUMBER, String.valueOf(v))); + } return true; } + @Override + public Optional> tabComplete(User user, String alias, List args) { + return Optional.of(new ArrayList(this.getRecipes(user).keySet())); + } + } diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index d662f7e..4240129 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -67,6 +67,9 @@ greenhouses: FAIL_INSUFFICIENT_BLOCKS: "&cMore blocks are required to make this recipe!" success: "&2You successfully made a [biome] biome greenhouse! Biome will sync at next teleport or login." missing-blocks: "&cMissing [material] x [number]" + unknown-recipe: "&cUnknown recipe" + try-these: "&cTry one of these:" + recipe-format: "&3[name]" info: title: "&A[How To Build A Greenhouse]" instructions: | diff --git a/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java b/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java index 4790e3a..ad5953a 100644 --- a/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java +++ b/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java @@ -16,6 +16,7 @@ import org.bukkit.block.Block; import org.bukkit.util.BoundingBox; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -34,7 +35,7 @@ import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult; @RunWith(PowerMockRunner.class) @PrepareForTest({Bukkit.class, BentoBox.class}) public class BiomeRecipeTest { - + private BiomeRecipe br; @Mock private Greenhouses addon; @@ -60,7 +61,7 @@ public class BiomeRecipeTest { when(gh.getFloorHeight()).thenReturn(100); when(gh.getCeilingHeight()).thenReturn(120); bb = new BoundingBox(10, 100, 10, 20, 120, 20); - when(gh.getBoundingBox()).thenReturn(bb); + when(gh.getBoundingBox()).thenReturn(bb); when(gh.getWorld()).thenReturn(world); when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block); when(block.getType()).thenReturn(Material.AIR, @@ -87,6 +88,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addConvBlocks(org.bukkit.Material, org.bukkit.Material, double, org.bukkit.Material)}. */ + @Ignore @Test public void testAddConvBlocks() { fail("Not yet implemented"); @@ -95,6 +97,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addMobs(org.bukkit.entity.EntityType, int, org.bukkit.Material)}. */ + @Ignore @Test public void testAddMobs() { fail("Not yet implemented"); @@ -103,6 +106,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addPlants(org.bukkit.Material, int, org.bukkit.Material)}. */ + @Ignore @Test public void testAddPlants() { fail("Not yet implemented"); @@ -111,6 +115,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addReqBlocks(org.bukkit.Material, int)}. */ + @Ignore @Test public void testAddReqBlocks() { fail("Not yet implemented"); @@ -124,7 +129,7 @@ public class BiomeRecipeTest { Set result = br.checkRecipe(gh); assertTrue(result.isEmpty()); } - + /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#checkRecipe(world.bentobox.greenhouses.data.Greenhouse)}. */ @@ -133,12 +138,13 @@ public class BiomeRecipeTest { br.addReqBlocks(Material.ACACIA_LEAVES, 3); Set result = br.checkRecipe(gh); assertFalse(result.isEmpty()); - + } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#convertBlock(org.bukkit.block.Block)}. */ + @Ignore @Test public void testConvertBlock() { fail("Not yet implemented"); @@ -147,6 +153,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getBiome()}. */ + @Ignore @Test public void testGetBiome() { fail("Not yet implemented"); @@ -155,6 +162,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getBlockConvert()}. */ + @Ignore @Test public void testGetBlockConvert() { fail("Not yet implemented"); @@ -163,6 +171,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getFriendlyName()}. */ + @Ignore @Test public void testGetFriendlyName() { fail("Not yet implemented"); @@ -171,6 +180,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getIceCoverage()}. */ + @Ignore @Test public void testGetIceCoverage() { fail("Not yet implemented"); @@ -179,6 +189,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getIcon()}. */ + @Ignore @Test public void testGetIcon() { fail("Not yet implemented"); @@ -187,6 +198,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getLavaCoverage()}. */ + @Ignore @Test public void testGetLavaCoverage() { fail("Not yet implemented"); @@ -195,6 +207,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMobLimit()}. */ + @Ignore @Test public void testGetMobLimit() { fail("Not yet implemented"); @@ -203,6 +216,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getName()}. */ + @Ignore @Test public void testGetName() { fail("Not yet implemented"); @@ -211,6 +225,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getPermission()}. */ + @Ignore @Test public void testGetPermission() { fail("Not yet implemented"); @@ -219,6 +234,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#spawnMob(org.bukkit.block.Block)}. */ + @Ignore @Test public void testSpawnMob() { fail("Not yet implemented"); @@ -227,6 +243,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getRecipeBlocks()}. */ + @Ignore @Test public void testGetRecipeBlocks() { fail("Not yet implemented"); @@ -235,6 +252,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getWaterCoverage()}. */ + @Ignore @Test public void testGetWaterCoverage() { fail("Not yet implemented"); @@ -243,6 +261,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#growPlant(org.bukkit.block.Block)}. */ + @Ignore @Test public void testGrowPlant() { fail("Not yet implemented"); @@ -251,6 +270,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setFriendlyName(java.lang.String)}. */ + @Ignore @Test public void testSetFriendlyName() { fail("Not yet implemented"); @@ -259,6 +279,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setIcecoverage(int)}. */ + @Ignore @Test public void testSetIcecoverage() { fail("Not yet implemented"); @@ -267,6 +288,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setIcon(org.bukkit.Material)}. */ + @Ignore @Test public void testSetIcon() { fail("Not yet implemented"); @@ -275,6 +297,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setLavacoverage(int)}. */ + @Ignore @Test public void testSetLavacoverage() { fail("Not yet implemented"); @@ -283,6 +306,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setMobLimit(int)}. */ + @Ignore @Test public void testSetMobLimit() { fail("Not yet implemented"); @@ -291,6 +315,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setName(java.lang.String)}. */ + @Ignore @Test public void testSetName() { fail("Not yet implemented"); @@ -299,6 +324,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setPermission(java.lang.String)}. */ + @Ignore @Test public void testSetPermission() { fail("Not yet implemented"); @@ -307,6 +333,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setPriority(int)}. */ + @Ignore @Test public void testSetPriority() { fail("Not yet implemented"); @@ -315,14 +342,17 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setType(org.bukkit.block.Biome)}. */ + @Ignore @Test public void testSetType() { fail("Not yet implemented"); } /** + * * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setWatercoverage(int)}. */ + @Ignore @Test public void testSetWatercoverage() { fail("Not yet implemented"); @@ -331,6 +361,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMissingBlocks()}. */ + @Ignore @Test public void testGetMissingBlocks() { fail("Not yet implemented"); @@ -339,6 +370,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#compareTo(world.bentobox.greenhouses.greenhouse.BiomeRecipe)}. */ + @Ignore @Test public void testCompareTo() { fail("Not yet implemented"); @@ -347,6 +379,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#noMobs()}. */ + @Ignore @Test public void testNoMobs() { fail("Not yet implemented"); @@ -355,6 +388,7 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMobTypes()}. */ + @Ignore @Test public void testGetMobTypes() { fail("Not yet implemented"); From 30c693c6e01d022a1ee507af88df28a09d261af7 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 12 Oct 2019 17:09:38 -0700 Subject: [PATCH 2/3] Added BiomeRecipe test class --- .../greenhouses/greenhouse/BiomeRecipe.java | 14 +- .../greenhouse/BiomeRecipeTest.java | 414 ++++++++++++++---- 2 files changed, 328 insertions(+), 100 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java index 2065eea..d3ab6f9 100644 --- a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java +++ b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java @@ -89,8 +89,9 @@ public class BiomeRecipe implements Comparable { * @param mobType - entity type * @param mobProbability - reltive probability * @param mobSpawnOn - material to spawn on + * @return true if add is successful */ - public void addMobs(EntityType mobType, int mobProbability, Material mobSpawnOn) { + public boolean addMobs(EntityType mobType, int mobProbability, Material mobSpawnOn) { addon.log(" " + mobProbability + "% chance for " + Util.prettifyText(mobType.toString()) + " to spawn on " + Util.prettifyText(mobSpawnOn.toString())+ "."); double probability = ((double)mobProbability/100); double lastProb = mobTree.isEmpty() ? 0D : mobTree.lastKey(); @@ -98,8 +99,10 @@ public class BiomeRecipe implements Comparable { if ((1D - lastProb) >= probability) { // Add to probability tree mobTree.put(lastProb + probability, new GreenhouseMob(mobType, mobSpawnOn)); + return true; } else { addon.logError("Mob chances add up to > 100% in " + type.toString() + " biome recipe! Skipping " + mobType.toString()); + return false; } } @@ -109,8 +112,9 @@ public class BiomeRecipe implements Comparable { * @param plantMaterial - plant type * @param plantProbability - probability of growing * @param plantGrowOn - material on which it must grow + * @return true if add is successful */ - public void addPlants(Material plantMaterial, int plantProbability, Material plantGrowOn) { + public boolean addPlants(Material plantMaterial, int plantProbability, Material plantGrowOn) { double probability = ((double)plantProbability/100); // Add up all the probabilities in the list so far double lastProb = plantTree.isEmpty() ? 0D : plantTree.lastKey(); @@ -119,8 +123,10 @@ public class BiomeRecipe implements Comparable { plantTree.put(lastProb + probability, new GreenhousePlant(plantMaterial, plantGrowOn)); } else { addon.logError("Plant chances add up to > 100% in " + type.toString() + " biome recipe! Skipping " + plantMaterial.toString()); + return false; } addon.log(" " + plantProbability + "% chance for " + Util.prettifyText(plantMaterial.toString()) + " to grow on " + Util.prettifyText(plantGrowOn.toString())); + return true; } /** @@ -275,7 +281,7 @@ public class BiomeRecipe implements Comparable { /** * @return the priority */ - private int getPriority() { + public int getPriority() { return priority; } @@ -344,6 +350,8 @@ public class BiomeRecipe implements Comparable { if (bl.getRelative(BlockFace.UP).getType().equals(Material.AIR)) { bl.setBlockData(dataBottom, false); bl.getRelative(BlockFace.UP).setBlockData(dataTop, false); + } else { + return false; // No room } } else { bl.setBlockData(dataBottom, false); diff --git a/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java b/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java index ad5953a..de9311f 100644 --- a/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java +++ b/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java @@ -1,25 +1,40 @@ package world.bentobox.greenhouses.greenhouse; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Set; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.Particle; import org.bukkit.World; import org.bukkit.block.Biome; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.data.Bisected; +import org.bukkit.block.data.Bisected.Half; +import org.bukkit.block.data.BlockData; +import org.bukkit.entity.Cat; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.util.BoundingBox; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -49,12 +64,18 @@ public class BiomeRecipeTest { private World world; @Mock private Block block; + @Mock + private Location location; + @Mock + private BlockData bd; /** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.createBlockData(any(Material.class))).thenReturn(bd); type = Biome.BADLANDS; // Greenhouse when(gh.getArea()).thenReturn(100); @@ -64,18 +85,25 @@ public class BiomeRecipeTest { when(gh.getBoundingBox()).thenReturn(bb); when(gh.getWorld()).thenReturn(world); when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block); + // Block when(block.getType()).thenReturn(Material.AIR, Material.GRASS_BLOCK, Material.GRASS_BLOCK, Material.WATER, Material.BLUE_ICE, Material.PACKED_ICE, Material.ICE, Material.LAVA, Material.AIR); + when(block.getWorld()).thenReturn(world); + when(block.getLocation()).thenReturn(location); // Set up default recipe br = new BiomeRecipe(addon, type, 0); br.setIcecoverage(2); // 1% br.setLavacoverage(1); // 1% br.setWatercoverage(1); // 1% br.addReqBlocks(Material.GRASS_BLOCK, 2); + br.setFriendlyName("name"); + br.setName("name2"); + br.setIcon(Material.ACACIA_BOAT); + br.setPermission("perm"); } /** @@ -88,37 +116,76 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addConvBlocks(org.bukkit.Material, org.bukkit.Material, double, org.bukkit.Material)}. */ - @Ignore @Test public void testAddConvBlocks() { - fail("Not yet implemented"); + Material oldMaterial = Material.SAND; + Material newMaterial = Material.CLAY; + double convChance = 100D; + Material localMaterial = Material.WATER; + br.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial); + verify(addon).log(eq(" 100.0% chance for Sand to convert to Clay")); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addMobs(org.bukkit.entity.EntityType, int, org.bukkit.Material)}. */ - @Ignore @Test public void testAddMobs() { - fail("Not yet implemented"); + EntityType mobType = EntityType.CAT; + int mobProbability = 50; + Material mobSpawnOn = Material.GRASS_PATH; + br.addMobs(mobType, mobProbability, mobSpawnOn); + verify(addon).log(eq(" 50% chance for Cat to spawn on Grass Path.")); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addMobs(org.bukkit.entity.EntityType, int, org.bukkit.Material)}. + */ + @Test + public void testAddMobsOver100Percent() { + EntityType mobType = EntityType.CAT; + int mobProbability = 50; + Material mobSpawnOn = Material.GRASS_PATH; + br.addMobs(mobType, mobProbability, mobSpawnOn); + br.addMobs(mobType, mobProbability, mobSpawnOn); + br.addMobs(mobType, mobProbability, mobSpawnOn); + verify(addon).logError(eq("Mob chances add up to > 100% in BADLANDS biome recipe! Skipping CAT")); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addPlants(org.bukkit.Material, int, org.bukkit.Material)}. */ - @Ignore @Test public void testAddPlants() { - fail("Not yet implemented"); + Material plantMaterial = Material.JUNGLE_SAPLING; + int plantProbability = 20; + Material plantGrowOn = Material.DIRT; + br.addPlants(plantMaterial, plantProbability, plantGrowOn); + verify(addon).log(eq(" 20% chance for Jungle Sapling to grow on Dirt")); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addPlants(org.bukkit.Material, int, org.bukkit.Material)}. + */ + @Test + public void testAddPlantsOver100Percent() { + Material plantMaterial = Material.JUNGLE_SAPLING; + int plantProbability = 60; + Material plantGrowOn = Material.DIRT; + br.addPlants(plantMaterial, plantProbability, plantGrowOn); + br.addPlants(plantMaterial, plantProbability, plantGrowOn); + verify(addon).logError(eq("Plant chances add up to > 100% in BADLANDS biome recipe! Skipping JUNGLE_SAPLING")); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addReqBlocks(org.bukkit.Material, int)}. */ - @Ignore @Test public void testAddReqBlocks() { - fail("Not yet implemented"); + Material blockMaterial = Material.BLACK_CONCRETE; + int blockQty = 30; + br.addReqBlocks(blockMaterial, blockQty); + verify(addon).log(eq(" BLACK_CONCRETE x 30")); } /** @@ -144,254 +211,407 @@ public class BiomeRecipeTest { /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#convertBlock(org.bukkit.block.Block)}. */ - @Ignore @Test public void testConvertBlock() { - fail("Not yet implemented"); + // Setup + this.testAddConvBlocks(); + // Mock + Block b = mock(Block.class); + when(b.getType()).thenReturn(Material.SAND); + Block ab = mock(Block.class); + when(ab.getType()).thenReturn(Material.WATER); + when(b.getRelative(any())).thenReturn(ab); + br.convertBlock(b); + verify(b).setType(Material.CLAY); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#convertBlock(org.bukkit.block.Block)}. + */ + @Test + public void testConvertBlockNoWater() { + // Setup + this.testAddConvBlocks(); + // Mock + Block b = mock(Block.class); + when(b.getType()).thenReturn(Material.SAND); + Block ab = mock(Block.class); + when(ab.getType()).thenReturn(Material.SAND); + when(b.getRelative(any())).thenReturn(ab); + br.convertBlock(b); + verify(b, never()).setType(Material.CLAY); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#convertBlock(org.bukkit.block.Block)}. + */ + @Test + public void testConvertBlockNoConverts() { + // Mock + Block b = mock(Block.class); + when(b.getType()).thenReturn(Material.SAND); + br.convertBlock(b); + verify(b, never()).setType(Material.CLAY); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#convertBlock(org.bukkit.block.Block)}. + */ + @Test + public void testConvertBlockNoProbability() { + // Setup + Material oldMaterial = Material.SAND; + Material newMaterial = Material.CLAY; + double convChance = 0D; + Material localMaterial = Material.WATER; + br.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial); + + // Mock + Block b = mock(Block.class); + when(b.getType()).thenReturn(Material.SAND); + Block ab = mock(Block.class); + when(ab.getType()).thenReturn(Material.WATER); + when(b.getRelative(any())).thenReturn(ab); + br.convertBlock(b); + verify(b, never()).setType(Material.CLAY); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getBiome()}. */ - @Ignore @Test public void testGetBiome() { - fail("Not yet implemented"); + assertEquals(Biome.BADLANDS, br.getBiome()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getBlockConvert()}. */ - @Ignore @Test public void testGetBlockConvert() { - fail("Not yet implemented"); + assertFalse(br.getBlockConvert()); + this.testAddConvBlocks(); + assertTrue(br.getBlockConvert()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getFriendlyName()}. */ - @Ignore @Test public void testGetFriendlyName() { - fail("Not yet implemented"); + assertEquals("name", br.getFriendlyName()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getIceCoverage()}. */ - @Ignore @Test public void testGetIceCoverage() { - fail("Not yet implemented"); + assertEquals(2, br.getIceCoverage()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getIcon()}. */ - @Ignore @Test public void testGetIcon() { - fail("Not yet implemented"); + assertEquals(Material.ACACIA_BOAT, br.getIcon()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getLavaCoverage()}. */ - @Ignore @Test public void testGetLavaCoverage() { - fail("Not yet implemented"); + assertEquals(1, br.getLavaCoverage()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMobLimit()}. */ - @Ignore @Test public void testGetMobLimit() { - fail("Not yet implemented"); + assertEquals(9, br.getMobLimit()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getName()}. */ - @Ignore @Test public void testGetName() { - fail("Not yet implemented"); + assertEquals("name2", br.getName()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getPermission()}. */ - @Ignore @Test public void testGetPermission() { - fail("Not yet implemented"); + assertEquals("perm", br.getPermission()); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#spawnMob(org.bukkit.block.Block)}. + */ + @Test + public void testSpawnMobyZero() { + when(block.getY()).thenReturn(0); + assertFalse(br.spawnMob(block)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#spawnMob(org.bukkit.block.Block)}. + */ + @Test + public void testSpawnNoMobs() { + when(block.getY()).thenReturn(10); + assertFalse(br.spawnMob(block)); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#spawnMob(org.bukkit.block.Block)}. */ - @Ignore @Test public void testSpawnMob() { - fail("Not yet implemented"); + when(block.getY()).thenReturn(10); + when(block.getType()).thenReturn(Material.GRASS_PATH); + when(block.getRelative(any())).thenReturn(block); + + EntityType mobType = EntityType.CAT; + int mobProbability = 100; + Material mobSpawnOn = Material.GRASS_PATH; + + Entity cat = mock(Cat.class); + when(world.spawnEntity(any(), any())).thenReturn(cat); + + + br.addMobs(mobType, mobProbability, mobSpawnOn); + assertTrue(br.spawnMob(block)); + verify(world).spawnEntity(eq(location), eq(EntityType.CAT)); } + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#spawnMob(org.bukkit.block.Block)}. + */ + @Test + public void testSpawnMobWrongSurface() { + when(block.getY()).thenReturn(10); + when(block.getType()).thenReturn(Material.GRASS_BLOCK); + when(block.getRelative(any())).thenReturn(block); + + EntityType mobType = EntityType.CAT; + int mobProbability = 100; + Material mobSpawnOn = Material.GRASS_PATH; + + Entity cat = mock(Cat.class); + when(world.spawnEntity(any(), any())).thenReturn(cat); + + + br.addMobs(mobType, mobProbability, mobSpawnOn); + assertFalse(br.spawnMob(block)); + verify(world, never()).spawnEntity(eq(location), eq(EntityType.CAT)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#spawnMob(org.bukkit.block.Block)}. + */ + @Test + public void testSpawnMobFailToSpawn() { + when(block.getY()).thenReturn(10); + when(block.getType()).thenReturn(Material.GRASS_PATH); + when(block.getRelative(any())).thenReturn(block); + + EntityType mobType = EntityType.CAT; + int mobProbability = 100; + Material mobSpawnOn = Material.GRASS_PATH; + + br.addMobs(mobType, mobProbability, mobSpawnOn); + assertFalse(br.spawnMob(block)); + verify(world).spawnEntity(eq(location), eq(EntityType.CAT)); + } + + /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getRecipeBlocks()}. */ - @Ignore @Test public void testGetRecipeBlocks() { - fail("Not yet implemented"); + assertEquals(1, br.getRecipeBlocks().size()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getWaterCoverage()}. */ - @Ignore @Test public void testGetWaterCoverage() { - fail("Not yet implemented"); + assertEquals(1, br.getWaterCoverage()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#growPlant(org.bukkit.block.Block)}. */ - @Ignore @Test - public void testGrowPlant() { - fail("Not yet implemented"); + public void testGrowPlantNotAir() { + when(block.getType()).thenReturn(Material.SOUL_SAND); + assertFalse(br.growPlant(block)); } /** - * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setFriendlyName(java.lang.String)}. + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#growPlant(org.bukkit.block.Block)}. */ - @Ignore @Test - public void testSetFriendlyName() { - fail("Not yet implemented"); + public void testGrowPlantNoPlants() { + when(block.getType()).thenReturn(Material.AIR); + assertFalse(br.growPlant(block)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#growPlant(org.bukkit.block.Block)}. + */ + @Test + public void testGrowPlantPlantsYZero() { + when(block.getY()).thenReturn(0); + when(block.getType()).thenReturn(Material.AIR); + assertTrue(br.addPlants(Material.BAMBOO_SAPLING, 100, Material.GRASS_BLOCK)); + assertFalse(br.growPlant(block)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#growPlant(org.bukkit.block.Block)}. + */ + @Test + public void testGrowPlantPlants() { + when(block.getY()).thenReturn(10); + when(block.getType()).thenReturn(Material.AIR); + Block ob = mock(Block.class); + when(ob.getType()).thenReturn(Material.GRASS_BLOCK); + when(block.getRelative(any())).thenReturn(ob); + assertTrue(br.addPlants(Material.BAMBOO_SAPLING, 100, Material.GRASS_BLOCK)); + assertTrue(br.growPlant(block)); + verify(world).spawnParticle(eq(Particle.SNOWBALL), any(Location.class), anyInt(), anyDouble(), anyDouble(), anyDouble()); + verify(block).setBlockData(eq(bd), eq(false)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#growPlant(org.bukkit.block.Block)}. + */ + @Test + public void testGrowPlantPlantsDoublePlant() { + Bisected bisected = mock(Bisected.class); + when(Bukkit.createBlockData(any(Material.class))).thenReturn(bisected); + when(block.getY()).thenReturn(10); + when(block.getType()).thenReturn(Material.AIR); + Block ob = mock(Block.class); + when(ob.getType()).thenReturn(Material.GRASS_BLOCK); + when(block.getRelative(eq(BlockFace.DOWN))).thenReturn(ob); + when(block.getRelative(eq(BlockFace.UP))).thenReturn(block); + assertTrue(br.addPlants(Material.SUNFLOWER, 100, Material.GRASS_BLOCK)); + assertTrue(br.growPlant(block)); + verify(world).spawnParticle(eq(Particle.SNOWBALL), any(Location.class), anyInt(), anyDouble(), anyDouble(), anyDouble()); + verify(bisected).setHalf(eq(Half.BOTTOM)); + verify(bisected).setHalf(eq(Half.TOP)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#growPlant(org.bukkit.block.Block)}. + */ + @Test + public void testGrowPlantPlantsDoublePlantNoRoom() { + Bisected bisected = mock(Bisected.class); + when(Bukkit.createBlockData(any(Material.class))).thenReturn(bisected); + when(block.getY()).thenReturn(10); + when(block.getType()).thenReturn(Material.AIR); + Block ob = mock(Block.class); + when(ob.getType()).thenReturn(Material.GRASS_BLOCK); + when(block.getRelative(eq(BlockFace.DOWN))).thenReturn(ob); + when(block.getRelative(eq(BlockFace.UP))).thenReturn(ob); + assertTrue(br.addPlants(Material.SUNFLOWER, 100, Material.GRASS_BLOCK)); + assertFalse(br.growPlant(block)); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setIcecoverage(int)}. */ - @Ignore @Test public void testSetIcecoverage() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setIcon(org.bukkit.Material)}. - */ - @Ignore - @Test - public void testSetIcon() { - fail("Not yet implemented"); + verify(addon).log(" Ice > 2%"); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setLavacoverage(int)}. */ - @Ignore @Test public void testSetLavacoverage() { - fail("Not yet implemented"); + verify(addon).log(" Lava > 1%"); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setMobLimit(int)}. */ - @Ignore @Test public void testSetMobLimit() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setName(java.lang.String)}. - */ - @Ignore - @Test - public void testSetName() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setPermission(java.lang.String)}. - */ - @Ignore - @Test - public void testSetPermission() { - fail("Not yet implemented"); + br.setMobLimit(0); + assertEquals(0, br.getMobLimit()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setPriority(int)}. */ - @Ignore @Test public void testSetPriority() { - fail("Not yet implemented"); + br.setPriority(20); + assertEquals(20, br.getPriority()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setType(org.bukkit.block.Biome)}. */ - @Ignore @Test public void testSetType() { - fail("Not yet implemented"); + br.setType(Biome.BADLANDS_PLATEAU); + assertEquals(Biome.BADLANDS_PLATEAU, br.getBiome()); } /** * * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setWatercoverage(int)}. */ - @Ignore @Test public void testSetWatercoverage() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMissingBlocks()}. - */ - @Ignore - @Test - public void testGetMissingBlocks() { - fail("Not yet implemented"); + verify(addon).log(" Water > 1%"); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#compareTo(world.bentobox.greenhouses.greenhouse.BiomeRecipe)}. + * Only priorty is compared */ - @Ignore @Test public void testCompareTo() { - fail("Not yet implemented"); + assertEquals(0, br.compareTo(br)); + BiomeRecipe a = new BiomeRecipe(); + a.setPriority(20); + assertEquals(1, br.compareTo(a)); + assertEquals(-1, a.compareTo(br)); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#noMobs()}. */ - @Ignore @Test public void testNoMobs() { - fail("Not yet implemented"); + assertTrue(br.noMobs()); + this.testAddMobs(); + assertFalse(br.noMobs()); } /** * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMobTypes()}. */ - @Ignore @Test public void testGetMobTypes() { - fail("Not yet implemented"); + assertTrue(br.getMobTypes().isEmpty()); + this.testAddMobs(); + assertFalse(br.getMobTypes().isEmpty()); } } From 460e012b626b9b809b71e6ebffbe01db8cb2ef75 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 13 Oct 2019 16:17:20 -0700 Subject: [PATCH 3/3] Fixed snowing. BoundingBox coords wrong. https://github.com/BentoBoxWorld/Greenhouses/issues/27 --- .../greenhouses/listeners/SnowTracker.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/listeners/SnowTracker.java b/src/main/java/world/bentobox/greenhouses/listeners/SnowTracker.java index 32ada45..6ac8e0c 100644 --- a/src/main/java/world/bentobox/greenhouses/listeners/SnowTracker.java +++ b/src/main/java/world/bentobox/greenhouses/listeners/SnowTracker.java @@ -39,22 +39,25 @@ public class SnowTracker implements Listener { } - private void getAirBlocks(Greenhouse gh) { + private boolean getAirBlocks(Greenhouse gh) { + boolean createdSnow = false; List waterBlocks = new ArrayList<>(); for (int x = (int)gh.getBoundingBox().getMinX() + 1; x < (int)gh.getBoundingBox().getMaxX(); x++) { - for (int z = (int)gh.getBoundingBox().getMinY() + 1; z < (int)gh.getBoundingBox().getMaxY(); z++) { - for (int y = gh.getCeilingHeight() - 1; y >= gh.getFloorHeight(); y--) { + for (int z = (int)gh.getBoundingBox().getMinZ() + 1; z < (int)gh.getBoundingBox().getMaxZ(); z++) { + for (int y = (int)gh.getBoundingBox().getMaxY() - 2; y >= (int)gh.getBoundingBox().getMinY(); y--) { Block b = gh.getLocation().getWorld().getBlockAt(x, y, z); - if (b.getType().equals(Material.AIR) || b.getType().equals(Material.SNOW)) { + Material type = b.getType(); + if (type.equals(Material.AIR) || type.equals(Material.SNOW)) { b.getWorld().spawnParticle(Particle.SNOWBALL, b.getLocation(), 5); } else { // Add snow - if (b.getType().equals(Material.WATER)) { + if (type.equals(Material.WATER)) { waterBlocks.add(b); } else { // Not water if (Math.random() < addon.getSettings().getSnowDensity() && !b.isLiquid()) { b.getRelative(BlockFace.UP).setType(Material.SNOW); + createdSnow = true; } } @@ -68,6 +71,7 @@ public class SnowTracker implements Listener { if (maxSize > 0) { waterBlocks.stream().limit(maxSize).filter(b -> Math.random() < addon.getSettings().getSnowDensity()).forEach(b -> b.setType(Material.ICE)); } + return createdSnow; } @EventHandler @@ -85,11 +89,12 @@ public class SnowTracker implements Listener { } private void removeWaterBucketAndShake(Greenhouse g) { - Hopper h = ((Hopper)g.getRoofHopperLocation().getBlock().getState()); - h.getInventory().removeItem(new ItemStack(Material.WATER_BUCKET)); - h.getInventory().addItem(new ItemStack(Material.BUCKET)); // Scatter snow - getAirBlocks(g); + if (getAirBlocks(g)) { + Hopper h = ((Hopper)g.getRoofHopperLocation().getBlock().getState()); + h.getInventory().removeItem(new ItemStack(Material.WATER_BUCKET)); + h.getInventory().addItem(new ItemStack(Material.BUCKET)); + } } private void shakeGlobes(World world) {