From 5fd9cbfd36b4765e9e3ed1338ba581aec4293e2e Mon Sep 17 00:00:00 2001 From: BONNe Date: Sun, 16 Apr 2023 01:19:55 +0300 Subject: [PATCH] Release 1.7.3 (#107) * Version 1.7.3 * Add ${argLine} to get jacoco coverage * Updated Jacoco POM entry * Address bugs reported by SonarCloud * Updated ReadMe * Add max mobs option #99 * Use updated Bucket event * Added tests to cover #99 * Fixes help text for user command. Fixes #105 * Fixed maxmobs typo instead of maxmob * Remove unused imports * Update to new Bukkit Loader * Remove debug * Create plugin.yml (#106) * Create plugin.yml * Update pom.xml * Update GreenhousesPladdon.java --------- Co-authored-by: tastybento Co-authored-by: tastybento --- README.md | 7 +- pom.xml | 20 +++--- .../greenhouses/GreenhousesPladdon.java | 7 -- .../bentobox/greenhouses/data/Greenhouse.java | 2 +- .../greenhouses/greenhouse/BiomeRecipe.java | 21 +++++- .../managers/EcoSystemManager.java | 20 ++++-- .../greenhouses/managers/RecipeManager.java | 1 + .../greenhouses/ui/panel/PanelClick.java | 4 -- .../greenhouses/ui/user/MakeCommand.java | 4 -- .../greenhouses/ui/user/RemoveCommand.java | 4 -- .../greenhouses/ui/user/UserCommand.java | 3 +- src/main/resources/biomes.yml | 48 ++++++++++++- src/main/resources/locales/en-US.yml | 3 +- src/main/resources/plugin.yml | 9 +++ .../listeners/GreenhouseEventsTest.java | 13 ++-- .../managers/EcoSystemManagerTest.java | 70 ++++++++++++++++++- 16 files changed, 181 insertions(+), 55 deletions(-) create mode 100644 src/main/resources/plugin.yml diff --git a/README.md b/README.md index 36d0fca..3dd4bd7 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # Greenhouses - an add-on for BentoBox -## Note for 1.15.x + servers - -Biomes have changed so that they take up a 4x4 area and so greenhouse biomes now can bleed outside of the greenhouse. Unfortunately, this cannot be mitigated (as far as I know). If you have a good imagination, you can say that the biome of the greenhouse influences the surroundings a bit and it is natural! So it's a feature, not a bug! +[![Build Status](https://ci.codemc.org/buildStatus/icon?job=BentoBoxWorld/Greenhouses)](https://ci.codemc.org/job/BentoBoxWorld/job/Greenhouses/)[ +![Bugs](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Greenhouses&metric=bugs)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Greenhouses) +[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Greenhouses&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Greenhouses) +[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Greenhouses&metric=ncloc)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Greenhouses) ## About diff --git a/pom.xml b/pom.xml index c8542f6..c940abd 100644 --- a/pom.xml +++ b/pom.xml @@ -51,7 +51,7 @@ ${build.version}-SNAPSHOT - 1.7.2 + 1.7.3 -LOCAL BentoBoxWorld_Greenhouses bentobox-world @@ -116,12 +116,6 @@ ${spigot.version} provided - - org.spigotmc - plugin-annotations - 1.2.3-SNAPSHOT - provided - world.bentobox bentobox @@ -200,6 +194,7 @@ 3.0.0-M5 + ${argLine} --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util.concurrent=ALL-UNNAMED @@ -257,7 +252,7 @@ org.jacoco jacoco-maven-plugin - 0.8.3 + 0.8.7 true @@ -268,16 +263,21 @@ - pre-unit-test + prepare-agent prepare-agent - post-unit-test + report report + + + XML + + diff --git a/src/main/java/world/bentobox/greenhouses/GreenhousesPladdon.java b/src/main/java/world/bentobox/greenhouses/GreenhousesPladdon.java index f5531e0..336bdfb 100644 --- a/src/main/java/world/bentobox/greenhouses/GreenhousesPladdon.java +++ b/src/main/java/world/bentobox/greenhouses/GreenhousesPladdon.java @@ -1,10 +1,6 @@ package world.bentobox.greenhouses; -import org.bukkit.plugin.java.annotation.dependency.Dependency; -import org.bukkit.plugin.java.annotation.plugin.ApiVersion; -import org.bukkit.plugin.java.annotation.plugin.Plugin; - import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Pladdon; @@ -12,9 +8,6 @@ /** * @author tastybento */ -@Plugin(name = "Pladdon", version = "1.0") -@ApiVersion(ApiVersion.Target.v1_18) -@Dependency(value = "BentoBox") public class GreenhousesPladdon extends Pladdon { @Override diff --git a/src/main/java/world/bentobox/greenhouses/data/Greenhouse.java b/src/main/java/world/bentobox/greenhouses/data/Greenhouse.java index 3d052b3..0383c9b 100644 --- a/src/main/java/world/bentobox/greenhouses/data/Greenhouse.java +++ b/src/main/java/world/bentobox/greenhouses/data/Greenhouse.java @@ -220,7 +220,7 @@ public World getWorld() { * @return true if inside the greenhouse */ public boolean contains(Location location2) { - return getLocation().getWorld() != null + return getLocation() != null && getLocation().getWorld() != null && getLocation().getWorld().equals(location2.getWorld()) && getBoundingBox().contains(location2.toVector()); } diff --git a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java index 7cd7266..a589f77 100644 --- a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java +++ b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java @@ -32,7 +32,6 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Hoglin; import org.bukkit.entity.Piglin; -import org.bukkit.material.CocoaPlant; import org.bukkit.util.Vector; import com.google.common.base.Enums; @@ -102,6 +101,7 @@ public class BiomeRecipe implements Comparable { private String permission = ""; private final Random random = new Random(); + private int maxMob; /** @@ -376,7 +376,7 @@ public boolean spawnMob(Block b) { } // Center spawned mob Location spawnLoc = b.getLocation().clone().add(new Vector(0.5, 0, 0.5)); - return getRandomMob() + boolean result = getRandomMob() // Check if the spawn on block matches, if it exists .filter(m -> Optional.of(m.mobSpawnOn()) .map(b.getRelative(BlockFace.DOWN).getType()::equals) @@ -396,7 +396,7 @@ public boolean spawnMob(Block b) { return true; }).orElse(false); }).orElse(false); - + return result; } /** @@ -715,5 +715,20 @@ public Set getMobTypes() { return mobTree.values().stream().map(GreenhouseMob::mobType).collect(Collectors.toSet()); } + /** + * Set the maximum number of mobs in a greenhouse + * @param maxMob maximum + */ + public void setMaxMob(int maxMob) { + this.maxMob = maxMob; + } + + /** + * @return the maxMob + */ + public int getMaxMob() { + return maxMob; + } + } diff --git a/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java b/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java index b4032b8..0e17a42 100644 --- a/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java +++ b/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java @@ -132,15 +132,21 @@ private void verify(Greenhouse gh) { } - private void addMobs(Greenhouse gh) { + /** + * Try to spawn mobs in a greenhouse + * @param gh greenhouse + * @return true if mobs were spawned, false if not + */ + boolean addMobs(Greenhouse gh) { final BoundingBox bb = gh.getBoundingBox(); if(gh.getLocation() == null || gh.getLocation().getWorld() == null || gh.getWorld() == null - || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4) || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){ + || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4) + || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){ // Skipping addmobs for unloaded greenhouse - return; + return false; } if (gh.getBiomeRecipe().noMobs()) { - return; + return false; } // Check greenhouse chunks are loaded for (double blockX = bb.getMinX(); blockX < bb.getMaxX(); blockX+=16) { @@ -148,7 +154,7 @@ private void addMobs(Greenhouse gh) { int chunkX = (int)(blockX / 16); int chunkZ = (int)(blockZ / 16); if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) { - return; + return false; } } } @@ -161,6 +167,9 @@ private void addMobs(Greenhouse gh) { Collections.shuffle(list, new Random(System.currentTimeMillis())); Iterator it = list.iterator(); // Check if the greenhouse is full + if (sum >= gh.getBiomeRecipe().getMaxMob()) { + return false; + } while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) { // Spawn something if chance says so if (gh.getBiomeRecipe().spawnMob(it.next().block())) { @@ -168,6 +177,7 @@ private void addMobs(Greenhouse gh) { sum++; } } + return sum > 0; } /** diff --git a/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java b/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java index 248ba15..9b5750a 100644 --- a/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java +++ b/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java @@ -155,6 +155,7 @@ private BiomeRecipe getBiomeRecipe(ConfigurationSection biomeRecipeConfig, Strin b.setLavacoverage(biomeRecipeConfig.getInt("lavacoverage",-1)); b.setIcecoverage(biomeRecipeConfig.getInt("icecoverage",-1)); b.setMobLimit(biomeRecipeConfig.getInt("moblimit", 9)); + b.setMaxMob(biomeRecipeConfig.getInt("maxmobs", -1)); return b; } 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 31479d9..2935861 100644 --- a/src/main/java/world/bentobox/greenhouses/ui/panel/PanelClick.java +++ b/src/main/java/world/bentobox/greenhouses/ui/panel/PanelClick.java @@ -43,10 +43,6 @@ public boolean onClick(Panel panel, User user, ClickType clickType, int slot) { } private boolean makeGreenhouse(User user, BiomeRecipe br) { - if (user.getLocation() == null) { - addon.logError("User has no location : " + user.getName()); - return false; - } // Check flag if (!addon.getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) { user.sendMessage("greenhouses.errors.no-rank"); 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 0111e2e..6b7a893 100644 --- a/src/main/java/world/bentobox/greenhouses/ui/user/MakeCommand.java +++ b/src/main/java/world/bentobox/greenhouses/ui/user/MakeCommand.java @@ -92,10 +92,6 @@ private Map getRecipes(User user) { * @return true if successful */ private boolean makeGreenhouse(User user, BiomeRecipe br) { - if (user.getLocation() == null) { - getAddon().logError("User had no location"); - return false; - } // Check flag if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) { user.sendMessage("greenhouses.errors.no-rank"); diff --git a/src/main/java/world/bentobox/greenhouses/ui/user/RemoveCommand.java b/src/main/java/world/bentobox/greenhouses/ui/user/RemoveCommand.java index 8ae6a8f..49ca538 100644 --- a/src/main/java/world/bentobox/greenhouses/ui/user/RemoveCommand.java +++ b/src/main/java/world/bentobox/greenhouses/ui/user/RemoveCommand.java @@ -35,10 +35,6 @@ public void setup() { */ @Override public boolean execute(User user, String label, List args) { - if (user.getLocation() == null) { - getAddon().logError("User had no location"); - return false; - } // Check flag if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) { user.sendMessage("greenhouses.errors.no-rank"); diff --git a/src/main/java/world/bentobox/greenhouses/ui/user/UserCommand.java b/src/main/java/world/bentobox/greenhouses/ui/user/UserCommand.java index 2ce4a12..f03583a 100644 --- a/src/main/java/world/bentobox/greenhouses/ui/user/UserCommand.java +++ b/src/main/java/world/bentobox/greenhouses/ui/user/UserCommand.java @@ -30,8 +30,7 @@ public UserCommand(Greenhouses gh, CompositeCommand parent) { public void setup() { this.setPermission("greenhouses.player"); this.setOnlyPlayer(true); - this.setParametersHelp("greenhouses.command.parameters"); - this.setDescription("greenhouses.command.description"); + this.setDescription("greenhouses.commands.user.description"); //new InfoCommand(this); //new ListCommand(this); diff --git a/src/main/resources/biomes.yml b/src/main/resources/biomes.yml index 1a2807c..d196791 100644 --- a/src/main/resources/biomes.yml +++ b/src/main/resources/biomes.yml @@ -38,8 +38,12 @@ biomes: # The minimum number of blocks each mob requires. # Mobs will not spawn if there is more than 1 per this number of # blocks in the greenhouse. e.g., in this case only 2 mobs will spawn if the - # greenhouse area is 18 blocks + # greenhouse area is 18 blocks. This enables bigger greenhouses to spawn more. moblimit: 9 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 5 Snowy_beach: friendlyname: "Snowy beach" biome: SNOWY_BEACH @@ -65,6 +69,10 @@ biomes: mobs: WOLF: 10:SNOW moblimit: 9 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 5 Cold_Rabbit: friendlyname: "Cold Taiga Forest" biome: OLD_GROWTH_SPRUCE_TAIGA @@ -80,6 +88,10 @@ biomes: mobs: RABBIT: 10:SNOW moblimit: 9 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 20 DESERT: friendlyname: "Desert" biome: DESERT @@ -134,6 +146,10 @@ biomes: STRIDER: 10:LAVA ENDERMAN: 5:NETHERRACK moblimit: 9 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 50 permission: greenhouses.biome.nether SOUL_SAND_VALLEY: friendlyname: "&cSoul Sand Valley" @@ -149,6 +165,10 @@ biomes: mobs: SKELETON: 10:SOUL_SAND moblimit: 9 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 50 permission: greenhouses.biome.nether # Conversion list - in this case, an adjacent block is required to convert # Format is: @@ -173,6 +193,10 @@ biomes: PIGLIN: 10:CRIMSON_NYLIUM HOGLIN: 10:CRIMSON_NYLIUM moblimit: 9 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 50 permission: greenhouses.biome.nether WARPED_FOREST: friendlyname: "&cWarped Forest" @@ -190,6 +214,10 @@ biomes: STRIDER: 10:LAVA ENDERMAN: 20:WARPED_NYLIUM moblimit: 9 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 50 permission: greenhouses.biome.nether JUNGLE: biome: JUNGLE @@ -221,6 +249,10 @@ biomes: mobs: MUSHROOM_COW: 10:MYCELIUM moblimit: 9 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 20 OCEAN: biome: OCEAN icon: WATER_BUCKET @@ -242,6 +274,10 @@ biomes: mobs: HORSE: 10:GRASS_BLOCK moblimit: 1 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 10 RIVER: friendlyname: "Clay river" biome: RIVER @@ -286,6 +322,10 @@ biomes: mobs: SLIME: 5:WATER moblimit: 3 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 10 dripstone_caves: friendlyname: "&6Drippy Drops" biome: dripstone_caves @@ -304,4 +344,8 @@ biomes: mobs: skeleton: 5:STONE glow_squid: 5:WATER - moblimit: 5 \ No newline at end of file + moblimit: 5 + # Maxmobs - this is the maximum number of greenhouse-spawed mobs allowed in + # the greenhouse at once. Spawning will stop when this limit is reached. + # If this value is not given, there is no maximum. + maxmobs: 25 \ No newline at end of file diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 8d1ecc6..894ab48 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -38,9 +38,10 @@ greenhouses: minimumblockstitle: "[Minimum blocks required]" nootherblocks: "No other blocks required." missing: "Greenhouse is missing" - + commands: user: + description: "Opens the Greenhouse selection GUI" remove: description: "Removes a greenhouse that you are standing in if you are the owner" make: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..49ec66d --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,9 @@ +name: BentoBox-Greenhouses +main: world.bentobox.greenhouses.GreenhousesPladdon +version: ${project.version}${build.number} +api-version: "1.19" + +authors: [tastybento] +contributors: ["The BentoBoxWorld Community"] +website: https://bentobox.world +description: ${project.description} diff --git a/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java b/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java index 27093f7..b3c27d2 100644 --- a/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java +++ b/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java @@ -27,6 +27,7 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.junit.Before; @@ -137,7 +138,7 @@ public void testOnPlayerInteractInNetherInGreenhouse() { when(nextBlock.getLocation()).thenReturn(location); ItemStack item = mock(ItemStack.class); when(item.getType()).thenReturn(Material.WATER_BUCKET); - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item, EquipmentSlot.HAND); ghe.onPlayerInteractInNether(e); verify(nextBlock).setType(Material.WATER); } @@ -154,7 +155,7 @@ public void testOnPlayerInteractInNetherOutsideOfGreenhouse() { when(nextBlock.getLocation()).thenReturn(mock(Location.class)); ItemStack item = mock(ItemStack.class); when(item.getType()).thenReturn(Material.WATER_BUCKET); - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item, EquipmentSlot.HAND); ghe.onPlayerInteractInNether(e); verify(nextBlock, never()).setType(Material.WATER); } @@ -170,7 +171,7 @@ public void testOnPlayerInteractInNetherGreenhouse() { when(clickedBlock.getRelative(any())).thenReturn(nextBlock); ItemStack item = mock(ItemStack.class); when(item.getType()).thenReturn(Material.WATER_BUCKET); - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item, EquipmentSlot.HAND); ghe.onPlayerInteractInNether(e); verify(nextBlock, never()).setType(Material.WATER); } @@ -189,7 +190,7 @@ public void testOnPlayerInteractInNetherNotInNether() { when(nextBlock.getWorld()).thenReturn(world); ItemStack item = mock(ItemStack.class); when(item.getType()).thenReturn(Material.WATER_BUCKET); - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item, EquipmentSlot.HAND); ghe.onPlayerInteractInNether(e); verify(nextBlock, never()).setType(Material.WATER); } @@ -205,7 +206,7 @@ public void testOnPlayerInteractInNetherNotWaterBucket() { when(clickedBlock.getRelative(any())).thenReturn(nextBlock); ItemStack item = mock(ItemStack.class); when(item.getType()).thenReturn(Material.ACACIA_BOAT); - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER, item); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER, item, EquipmentSlot.HAND); ghe.onPlayerInteractInNether(e); verify(nextBlock, never()).setType(Material.WATER); } @@ -222,7 +223,7 @@ public void testOnPlayerInteractInNetherNotInGreenhouse() { when(clickedBlock.getRelative(any())).thenReturn(nextBlock); ItemStack item = mock(ItemStack.class); when(item.getType()).thenReturn(Material.WATER_BUCKET); - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item, EquipmentSlot.HAND); ghe.onPlayerInteractInNether(e); verify(nextBlock, never()).setType(Material.WATER); } diff --git a/src/test/java/world/bentobox/greenhouses/managers/EcoSystemManagerTest.java b/src/test/java/world/bentobox/greenhouses/managers/EcoSystemManagerTest.java index 0961b7b..a81c9f8 100644 --- a/src/test/java/world/bentobox/greenhouses/managers/EcoSystemManagerTest.java +++ b/src/test/java/world/bentobox/greenhouses/managers/EcoSystemManagerTest.java @@ -1,6 +1,8 @@ package world.bentobox.greenhouses.managers; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; @@ -9,11 +11,11 @@ import static org.mockito.Mockito.when; import java.util.List; +import java.util.Optional; import org.bukkit.Bukkit; import org.bukkit.Keyed; import org.bukkit.Location; -import org.bukkit.NamespacedKey; import org.bukkit.Tag; import org.bukkit.World; import org.bukkit.block.Block; @@ -25,13 +27,13 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.stubbing.OngoingStubbing; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; import world.bentobox.greenhouses.data.Greenhouse; +import world.bentobox.greenhouses.greenhouse.BiomeRecipe; import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock; /** @@ -39,7 +41,7 @@ * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, Tag.class}) +@PrepareForTest({Bukkit.class, BentoBox.class, Tag.class, RecipeManager.class}) public class EcoSystemManagerTest { private Greenhouse gh; @@ -53,6 +55,8 @@ public class EcoSystemManagerTest { private Block liquid; @Mock private Block plant; + @Mock + private BiomeRecipe recipe; // CUT private EcoSystemManager eco; @@ -62,6 +66,7 @@ public class EcoSystemManagerTest { public void setUp() { PowerMockito.mockStatic(Tag.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); + @SuppressWarnings("unchecked") Tag tag = mock(Tag.class); when(Bukkit.getTag(anyString(), any(), any())).thenReturn(tag); @@ -95,6 +100,12 @@ public void setUp() { // Liquid false when(block.getRelative(eq(BlockFace.UP))).thenReturn(air); + // Recipe + when(recipe.noMobs()).thenReturn(true); + PowerMockito.mockStatic(RecipeManager.class, Mockito.RETURNS_MOCKS); + when(RecipeManager.getBiomeRecipies(any())).thenReturn(Optional.of(recipe)); + + eco = new EcoSystemManager(null, null); } @@ -209,4 +220,57 @@ public void testGetAvailableBlocksAirAboveLiquidIgnoreLiquids() { assertEquals(liquid, value.block()); } } + + /** + * Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}. + */ + @Test + public void testAddMobsChunkNotLoaded() { + assertFalse(eco.addMobs(gh)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}. + */ + @Test + public void testAddMobsChunkLoadedNoMobs() { + when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true); + assertFalse(eco.addMobs(gh)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}. + */ + @Test + public void testAddMobsChunkLoadedWithMobsInRecipeMaxMobsZero() { + when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true); + when(recipe.noMobs()).thenReturn(false); + assertFalse(eco.addMobs(gh)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}. + */ + @Test + public void testAddMobsChunkLoadedWithMobsInRecipeMaxMobsNotZero() { + // Nothing spawned here + when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true); + when(recipe.noMobs()).thenReturn(false); + when(recipe.getMaxMob()).thenReturn(10); + assertFalse(eco.addMobs(gh)); + } + + /** + * Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}. + */ + @Test + public void testAddMobsSpawnMob() { + // Nothing spawned here + when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true); + when(recipe.noMobs()).thenReturn(false); + when(recipe.getMaxMob()).thenReturn(10); + when(recipe.spawnMob(any())).thenReturn(true); + assertTrue(eco.addMobs(gh)); + } + }