diff --git a/pom.xml b/pom.xml index 3fd244f..e8e0c23 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ UTF-8 UTF-8 1.8 - 1.7.4 + 2.0.2 1.14.4-R0.1-SNAPSHOT 1.7.0 @@ -146,8 +146,8 @@ org.mockito - mockito-all - 1.10.19 + mockito-core + 3.0.0 test @@ -158,11 +158,12 @@ org.powermock - powermock-api-mockito + powermock-api-mockito2 ${powermock.version} test + diff --git a/src/main/java/world/bentobox/greenhouses/data/Greenhouse.java b/src/main/java/world/bentobox/greenhouses/data/Greenhouse.java index 6278e9d..77fef37 100644 --- a/src/main/java/world/bentobox/greenhouses/data/Greenhouse.java +++ b/src/main/java/world/bentobox/greenhouses/data/Greenhouse.java @@ -1,8 +1,10 @@ package world.bentobox.greenhouses.data; +import java.util.Map; import java.util.UUID; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Biome; import org.bukkit.util.BoundingBox; @@ -36,6 +38,8 @@ public class Greenhouse implements DataObject { private String biomeRecipeName; private boolean broken; + + private Map missingBlocks; /** * Constructor for database @@ -204,4 +208,18 @@ public class Greenhouse implements DataObject { return RecipeManager.getBiomeRecipies(biomeRecipeName).orElse(null); } + /** + * @param missingBlocks the missingBlocks to set + */ + public void setMissingBlocks(Map missingBlocks) { + this.missingBlocks = missingBlocks; + } + + /** + * @return the missingBlocks + */ + public Map getMissingBlocks() { + return missingBlocks; + } + } diff --git a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java index af22341..32d56d7 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(); - private Map missingBlocks; + public BiomeRecipe() {} @@ -146,13 +146,15 @@ public class BiomeRecipe implements Comparable { 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++) { Block b = gh.getWorld().getBlockAt(x, y, z); - if (!b.getType().equals(Material.AIR)) { - blockCount.putIfAbsent(b.getType(), 0); - blockCount.merge(b.getType(), 1, Integer::sum); + Material type = b.getType(); + if (!type.equals(Material.AIR)) { + blockCount.putIfAbsent(type, 0); + blockCount.merge(type, 1, Integer::sum); } } } } + 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; @@ -179,12 +181,21 @@ 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 - missingBlocks = requiredBlocks.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() - blockCount.getOrDefault(e.getKey(), 0))); + 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); + } return result; } @@ -434,13 +445,6 @@ public class BiomeRecipe implements Comparable { this.waterCoverage = waterCoverage; } - /** - * @return the missingBlocks - */ - public Map getMissingBlocks() { - return missingBlocks; - } - @Override public int compareTo(BiomeRecipe o) { return Integer.compare(o.getPriority(), this.getPriority()); diff --git a/src/main/java/world/bentobox/greenhouses/managers/GreenhouseManager.java b/src/main/java/world/bentobox/greenhouses/managers/GreenhouseManager.java index 729903e..34f9e8f 100644 --- a/src/main/java/world/bentobox/greenhouses/managers/GreenhouseManager.java +++ b/src/main/java/world/bentobox/greenhouses/managers/GreenhouseManager.java @@ -50,7 +50,8 @@ public class GreenhouseManager implements Listener { FAIL_OVERLAPPING, NULL, SUCCESS, - FAIL_NO_RECIPE_FOUND + FAIL_NO_RECIPE_FOUND, + FAIL_INSUFFICIENT_BLOCKS } private final Greenhouses addon; diff --git a/src/main/java/world/bentobox/greenhouses/ui/panel/PanelClick.java b/src/main/java/world/bentobox/greenhouses/ui/panel/PanelClick.java index efd0a56..b057044 100644 --- a/src/main/java/world/bentobox/greenhouses/ui/panel/PanelClick.java +++ b/src/main/java/world/bentobox/greenhouses/ui/panel/PanelClick.java @@ -1,6 +1,3 @@ -/** - * - */ package world.bentobox.greenhouses.ui.panel; import org.bukkit.Bukkit; @@ -9,9 +6,11 @@ import org.bukkit.Material; import org.bukkit.event.inventory.ClickType; import org.bukkit.util.Vector; +import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.panels.Panel; import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler; 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; @@ -69,6 +68,9 @@ public class PanelClick implements ClickHandler { result.getFinder().getRedGlass().forEach(rg -> user.getPlayer().sendBlockChange(rg, Material.RED_STAINED_GLASS.createBlockData())); Bukkit.getScheduler().runTaskLater(addon.getPlugin(), () -> result.getFinder().getRedGlass().forEach(rg -> user.getPlayer().sendBlockChange(rg, rg.getBlock().getBlockData())), 120L); } + if (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; } } diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index a2cd5af..d662f7e 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -64,7 +64,9 @@ greenhouses: FAIL_NO_ICE: "&cIce is required to make this recipe" FAIL_NO_LAVA: "&cLava is required to make this recipe" FAIL_NO_WATER: "&cWater is required to make this recipe" + 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]" 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 new file mode 100644 index 0000000..4790e3a --- /dev/null +++ b/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java @@ -0,0 +1,363 @@ +package world.bentobox.greenhouses.greenhouse; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.when; + +import java.util.Set; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Biome; +import org.bukkit.block.Block; +import org.bukkit.util.BoundingBox; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.greenhouses.Greenhouses; +import world.bentobox.greenhouses.data.Greenhouse; +import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class}) +public class BiomeRecipeTest { + + private BiomeRecipe br; + @Mock + private Greenhouses addon; + + private Biome type; + @Mock + private Greenhouse gh; + + private BoundingBox bb; + @Mock + private World world; + @Mock + private Block block; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + type = Biome.BADLANDS; + // Greenhouse + when(gh.getArea()).thenReturn(100); + 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.getWorld()).thenReturn(world); + when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(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); + // 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); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addConvBlocks(org.bukkit.Material, org.bukkit.Material, double, org.bukkit.Material)}. + */ + @Test + public void testAddConvBlocks() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addMobs(org.bukkit.entity.EntityType, int, org.bukkit.Material)}. + */ + @Test + public void testAddMobs() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addPlants(org.bukkit.Material, int, org.bukkit.Material)}. + */ + @Test + public void testAddPlants() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addReqBlocks(org.bukkit.Material, int)}. + */ + @Test + public void testAddReqBlocks() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#checkRecipe(world.bentobox.greenhouses.data.Greenhouse)}. + */ + @Test + public void testCheckRecipe() { + Set result = br.checkRecipe(gh); + assertTrue(result.isEmpty()); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#checkRecipe(world.bentobox.greenhouses.data.Greenhouse)}. + */ + @Test + public void testCheckRecipeNotEnough() { + 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)}. + */ + @Test + public void testConvertBlock() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getBiome()}. + */ + @Test + public void testGetBiome() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getBlockConvert()}. + */ + @Test + public void testGetBlockConvert() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getFriendlyName()}. + */ + @Test + public void testGetFriendlyName() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getIceCoverage()}. + */ + @Test + public void testGetIceCoverage() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getIcon()}. + */ + @Test + public void testGetIcon() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getLavaCoverage()}. + */ + @Test + public void testGetLavaCoverage() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMobLimit()}. + */ + @Test + public void testGetMobLimit() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getName()}. + */ + @Test + public void testGetName() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getPermission()}. + */ + @Test + public void testGetPermission() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#spawnMob(org.bukkit.block.Block)}. + */ + @Test + public void testSpawnMob() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getRecipeBlocks()}. + */ + @Test + public void testGetRecipeBlocks() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getWaterCoverage()}. + */ + @Test + public void testGetWaterCoverage() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#growPlant(org.bukkit.block.Block)}. + */ + @Test + public void testGrowPlant() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setFriendlyName(java.lang.String)}. + */ + @Test + public void testSetFriendlyName() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setIcecoverage(int)}. + */ + @Test + public void testSetIcecoverage() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setIcon(org.bukkit.Material)}. + */ + @Test + public void testSetIcon() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setLavacoverage(int)}. + */ + @Test + public void testSetLavacoverage() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setMobLimit(int)}. + */ + @Test + public void testSetMobLimit() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setName(java.lang.String)}. + */ + @Test + public void testSetName() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setPermission(java.lang.String)}. + */ + @Test + public void testSetPermission() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setPriority(int)}. + */ + @Test + public void testSetPriority() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setType(org.bukkit.block.Biome)}. + */ + @Test + public void testSetType() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#setWatercoverage(int)}. + */ + @Test + public void testSetWatercoverage() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMissingBlocks()}. + */ + @Test + public void testGetMissingBlocks() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#compareTo(world.bentobox.greenhouses.greenhouse.BiomeRecipe)}. + */ + @Test + public void testCompareTo() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#noMobs()}. + */ + @Test + public void testNoMobs() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#getMobTypes()}. + */ + @Test + public void testGetMobTypes() { + fail("Not yet implemented"); + } + +}