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 <tastybento@wasteofplastic.com>
Co-authored-by: tastybento <tastybento@users.noreply.github.com>
This commit is contained in:
BONNe 2023-04-16 01:19:55 +03:00 committed by GitHub
parent de6a939bb9
commit 5fd9cbfd36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 181 additions and 55 deletions

View File

@ -1,8 +1,9 @@
# Greenhouses - an add-on for BentoBox # Greenhouses - an add-on for BentoBox
## Note for 1.15.x + servers [![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)
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! [![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 ## About

20
pom.xml
View File

@ -51,7 +51,7 @@
<!-- Revision variable removes warning about dynamic version --> <!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision> <revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. --> <!-- This allows to change between versions and snapshots. -->
<build.version>1.7.2</build.version> <build.version>1.7.3</build.version>
<build.number>-LOCAL</build.number> <build.number>-LOCAL</build.number>
<sonar.projectKey>BentoBoxWorld_Greenhouses</sonar.projectKey> <sonar.projectKey>BentoBoxWorld_Greenhouses</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization> <sonar.organization>bentobox-world</sonar.organization>
@ -116,12 +116,6 @@
<version>${spigot.version}</version> <version>${spigot.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>plugin-annotations</artifactId>
<version>1.2.3-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>world.bentobox</groupId> <groupId>world.bentobox</groupId>
<artifactId>bentobox</artifactId> <artifactId>bentobox</artifactId>
@ -200,6 +194,7 @@
<version>3.0.0-M5</version> <version>3.0.0-M5</version>
<configuration> <configuration>
<argLine> <argLine>
${argLine}
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.util.concurrent=ALL-UNNAMED --add-opens java.base/java.util.concurrent=ALL-UNNAMED
@ -257,7 +252,7 @@
<plugin> <plugin>
<groupId>org.jacoco</groupId> <groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId> <artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version> <version>0.8.7</version>
<configuration> <configuration>
<append>true</append> <append>true</append>
<excludes> <excludes>
@ -268,16 +263,21 @@
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
<id>pre-unit-test</id> <id>prepare-agent</id>
<goals> <goals>
<goal>prepare-agent</goal> <goal>prepare-agent</goal>
</goals> </goals>
</execution> </execution>
<execution> <execution>
<id>post-unit-test</id> <id>report</id>
<goals> <goals>
<goal>report</goal> <goal>report</goal>
</goals> </goals>
<configuration>
<formats>
<format>XML</format>
</formats>
</configuration>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>

View File

@ -1,10 +1,6 @@
package world.bentobox.greenhouses; 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.Addon;
import world.bentobox.bentobox.api.addons.Pladdon; import world.bentobox.bentobox.api.addons.Pladdon;
@ -12,9 +8,6 @@ import world.bentobox.bentobox.api.addons.Pladdon;
/** /**
* @author tastybento * @author tastybento
*/ */
@Plugin(name = "Pladdon", version = "1.0")
@ApiVersion(ApiVersion.Target.v1_18)
@Dependency(value = "BentoBox")
public class GreenhousesPladdon extends Pladdon public class GreenhousesPladdon extends Pladdon
{ {
@Override @Override

View File

@ -220,7 +220,7 @@ public class Greenhouse implements DataObject {
* @return true if inside the greenhouse * @return true if inside the greenhouse
*/ */
public boolean contains(Location location2) { public boolean contains(Location location2) {
return getLocation().getWorld() != null return getLocation() != null && getLocation().getWorld() != null
&& getLocation().getWorld().equals(location2.getWorld()) && getLocation().getWorld().equals(location2.getWorld())
&& getBoundingBox().contains(location2.toVector()); && getBoundingBox().contains(location2.toVector());
} }

View File

@ -32,7 +32,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Hoglin; import org.bukkit.entity.Hoglin;
import org.bukkit.entity.Piglin; import org.bukkit.entity.Piglin;
import org.bukkit.material.CocoaPlant;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import com.google.common.base.Enums; import com.google.common.base.Enums;
@ -102,6 +101,7 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
private String permission = ""; private String permission = "";
private final Random random = new Random(); private final Random random = new Random();
private int maxMob;
/** /**
@ -376,7 +376,7 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
} }
// Center spawned mob // Center spawned mob
Location spawnLoc = b.getLocation().clone().add(new Vector(0.5, 0, 0.5)); 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 // Check if the spawn on block matches, if it exists
.filter(m -> Optional.of(m.mobSpawnOn()) .filter(m -> Optional.of(m.mobSpawnOn())
.map(b.getRelative(BlockFace.DOWN).getType()::equals) .map(b.getRelative(BlockFace.DOWN).getType()::equals)
@ -396,7 +396,7 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
return true; return true;
}).orElse(false); }).orElse(false);
}).orElse(false); }).orElse(false);
return result;
} }
/** /**
@ -715,5 +715,20 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
return mobTree.values().stream().map(GreenhouseMob::mobType).collect(Collectors.toSet()); 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;
}
} }

View File

@ -132,15 +132,21 @@ public class EcoSystemManager {
} }
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(); final BoundingBox bb = gh.getBoundingBox();
if(gh.getLocation() == null || gh.getLocation().getWorld() == null || gh.getWorld() == null 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 // Skipping addmobs for unloaded greenhouse
return; return false;
} }
if (gh.getBiomeRecipe().noMobs()) { if (gh.getBiomeRecipe().noMobs()) {
return; return false;
} }
// Check greenhouse chunks are loaded // Check greenhouse chunks are loaded
for (double blockX = bb.getMinX(); blockX < bb.getMaxX(); blockX+=16) { for (double blockX = bb.getMinX(); blockX < bb.getMaxX(); blockX+=16) {
@ -148,7 +154,7 @@ public class EcoSystemManager {
int chunkX = (int)(blockX / 16); int chunkX = (int)(blockX / 16);
int chunkZ = (int)(blockZ / 16); int chunkZ = (int)(blockZ / 16);
if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) { if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) {
return; return false;
} }
} }
} }
@ -161,6 +167,9 @@ public class EcoSystemManager {
Collections.shuffle(list, new Random(System.currentTimeMillis())); Collections.shuffle(list, new Random(System.currentTimeMillis()));
Iterator<GrowthBlock> it = list.iterator(); Iterator<GrowthBlock> it = list.iterator();
// Check if the greenhouse is full // Check if the greenhouse is full
if (sum >= gh.getBiomeRecipe().getMaxMob()) {
return false;
}
while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) { while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) {
// Spawn something if chance says so // Spawn something if chance says so
if (gh.getBiomeRecipe().spawnMob(it.next().block())) { if (gh.getBiomeRecipe().spawnMob(it.next().block())) {
@ -168,6 +177,7 @@ public class EcoSystemManager {
sum++; sum++;
} }
} }
return sum > 0;
} }
/** /**

View File

@ -155,6 +155,7 @@ public class RecipeManager {
b.setLavacoverage(biomeRecipeConfig.getInt("lavacoverage",-1)); b.setLavacoverage(biomeRecipeConfig.getInt("lavacoverage",-1));
b.setIcecoverage(biomeRecipeConfig.getInt("icecoverage",-1)); b.setIcecoverage(biomeRecipeConfig.getInt("icecoverage",-1));
b.setMobLimit(biomeRecipeConfig.getInt("moblimit", 9)); b.setMobLimit(biomeRecipeConfig.getInt("moblimit", 9));
b.setMaxMob(biomeRecipeConfig.getInt("maxmobs", -1));
return b; return b;
} }

View File

@ -43,10 +43,6 @@ public class PanelClick implements ClickHandler {
} }
private boolean makeGreenhouse(User user, BiomeRecipe br) { private boolean makeGreenhouse(User user, BiomeRecipe br) {
if (user.getLocation() == null) {
addon.logError("User has no location : " + user.getName());
return false;
}
// Check flag // Check flag
if (!addon.getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) { if (!addon.getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
user.sendMessage("greenhouses.errors.no-rank"); user.sendMessage("greenhouses.errors.no-rank");

View File

@ -92,10 +92,6 @@ class MakeCommand extends CompositeCommand {
* @return true if successful * @return true if successful
*/ */
private boolean makeGreenhouse(User user, BiomeRecipe br) { private boolean makeGreenhouse(User user, BiomeRecipe br) {
if (user.getLocation() == null) {
getAddon().logError("User had no location");
return false;
}
// Check flag // Check flag
if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) { if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
user.sendMessage("greenhouses.errors.no-rank"); user.sendMessage("greenhouses.errors.no-rank");

View File

@ -35,10 +35,6 @@ class RemoveCommand extends CompositeCommand {
*/ */
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
if (user.getLocation() == null) {
getAddon().logError("User had no location");
return false;
}
// Check flag // Check flag
if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) { if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
user.sendMessage("greenhouses.errors.no-rank"); user.sendMessage("greenhouses.errors.no-rank");

View File

@ -30,8 +30,7 @@ public class UserCommand extends CompositeCommand {
public void setup() { public void setup() {
this.setPermission("greenhouses.player"); this.setPermission("greenhouses.player");
this.setOnlyPlayer(true); this.setOnlyPlayer(true);
this.setParametersHelp("greenhouses.command.parameters"); this.setDescription("greenhouses.commands.user.description");
this.setDescription("greenhouses.command.description");
//new InfoCommand(this); //new InfoCommand(this);
//new ListCommand(this); //new ListCommand(this);

View File

@ -38,8 +38,12 @@ biomes:
# The minimum number of blocks each mob requires. # The minimum number of blocks each mob requires.
# Mobs will not spawn if there is more than 1 per this number of # 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 # 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 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: Snowy_beach:
friendlyname: "Snowy beach" friendlyname: "Snowy beach"
biome: SNOWY_BEACH biome: SNOWY_BEACH
@ -65,6 +69,10 @@ biomes:
mobs: mobs:
WOLF: 10:SNOW WOLF: 10:SNOW
moblimit: 9 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: Cold_Rabbit:
friendlyname: "Cold Taiga Forest" friendlyname: "Cold Taiga Forest"
biome: OLD_GROWTH_SPRUCE_TAIGA biome: OLD_GROWTH_SPRUCE_TAIGA
@ -80,6 +88,10 @@ biomes:
mobs: mobs:
RABBIT: 10:SNOW RABBIT: 10:SNOW
moblimit: 9 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: DESERT:
friendlyname: "Desert" friendlyname: "Desert"
biome: DESERT biome: DESERT
@ -134,6 +146,10 @@ biomes:
STRIDER: 10:LAVA STRIDER: 10:LAVA
ENDERMAN: 5:NETHERRACK ENDERMAN: 5:NETHERRACK
moblimit: 9 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 permission: greenhouses.biome.nether
SOUL_SAND_VALLEY: SOUL_SAND_VALLEY:
friendlyname: "&cSoul Sand Valley" friendlyname: "&cSoul Sand Valley"
@ -149,6 +165,10 @@ biomes:
mobs: mobs:
SKELETON: 10:SOUL_SAND SKELETON: 10:SOUL_SAND
moblimit: 9 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 permission: greenhouses.biome.nether
# Conversion list - in this case, an adjacent block is required to convert # Conversion list - in this case, an adjacent block is required to convert
# Format is: # Format is:
@ -173,6 +193,10 @@ biomes:
PIGLIN: 10:CRIMSON_NYLIUM PIGLIN: 10:CRIMSON_NYLIUM
HOGLIN: 10:CRIMSON_NYLIUM HOGLIN: 10:CRIMSON_NYLIUM
moblimit: 9 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 permission: greenhouses.biome.nether
WARPED_FOREST: WARPED_FOREST:
friendlyname: "&cWarped Forest" friendlyname: "&cWarped Forest"
@ -190,6 +214,10 @@ biomes:
STRIDER: 10:LAVA STRIDER: 10:LAVA
ENDERMAN: 20:WARPED_NYLIUM ENDERMAN: 20:WARPED_NYLIUM
moblimit: 9 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 permission: greenhouses.biome.nether
JUNGLE: JUNGLE:
biome: JUNGLE biome: JUNGLE
@ -221,6 +249,10 @@ biomes:
mobs: mobs:
MUSHROOM_COW: 10:MYCELIUM MUSHROOM_COW: 10:MYCELIUM
moblimit: 9 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: OCEAN:
biome: OCEAN biome: OCEAN
icon: WATER_BUCKET icon: WATER_BUCKET
@ -242,6 +274,10 @@ biomes:
mobs: mobs:
HORSE: 10:GRASS_BLOCK HORSE: 10:GRASS_BLOCK
moblimit: 1 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: RIVER:
friendlyname: "Clay river" friendlyname: "Clay river"
biome: RIVER biome: RIVER
@ -286,6 +322,10 @@ biomes:
mobs: mobs:
SLIME: 5:WATER SLIME: 5:WATER
moblimit: 3 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: dripstone_caves:
friendlyname: "&6Drippy Drops" friendlyname: "&6Drippy Drops"
biome: dripstone_caves biome: dripstone_caves
@ -305,3 +345,7 @@ biomes:
skeleton: 5:STONE skeleton: 5:STONE
glow_squid: 5:WATER glow_squid: 5:WATER
moblimit: 5 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

View File

@ -41,6 +41,7 @@ greenhouses:
commands: commands:
user: user:
description: "Opens the Greenhouse selection GUI"
remove: remove:
description: "Removes a greenhouse that you are standing in if you are the owner" description: "Removes a greenhouse that you are standing in if you are the owner"
make: make:

View File

@ -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}

View File

@ -27,6 +27,7 @@ import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import org.junit.Before; import org.junit.Before;
@ -137,7 +138,7 @@ public class GreenhouseEventsTest {
when(nextBlock.getLocation()).thenReturn(location); when(nextBlock.getLocation()).thenReturn(location);
ItemStack item = mock(ItemStack.class); ItemStack item = mock(ItemStack.class);
when(item.getType()).thenReturn(Material.WATER_BUCKET); 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); ghe.onPlayerInteractInNether(e);
verify(nextBlock).setType(Material.WATER); verify(nextBlock).setType(Material.WATER);
} }
@ -154,7 +155,7 @@ public class GreenhouseEventsTest {
when(nextBlock.getLocation()).thenReturn(mock(Location.class)); when(nextBlock.getLocation()).thenReturn(mock(Location.class));
ItemStack item = mock(ItemStack.class); ItemStack item = mock(ItemStack.class);
when(item.getType()).thenReturn(Material.WATER_BUCKET); 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); ghe.onPlayerInteractInNether(e);
verify(nextBlock, never()).setType(Material.WATER); verify(nextBlock, never()).setType(Material.WATER);
} }
@ -170,7 +171,7 @@ public class GreenhouseEventsTest {
when(clickedBlock.getRelative(any())).thenReturn(nextBlock); when(clickedBlock.getRelative(any())).thenReturn(nextBlock);
ItemStack item = mock(ItemStack.class); ItemStack item = mock(ItemStack.class);
when(item.getType()).thenReturn(Material.WATER_BUCKET); 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); ghe.onPlayerInteractInNether(e);
verify(nextBlock, never()).setType(Material.WATER); verify(nextBlock, never()).setType(Material.WATER);
} }
@ -189,7 +190,7 @@ public class GreenhouseEventsTest {
when(nextBlock.getWorld()).thenReturn(world); when(nextBlock.getWorld()).thenReturn(world);
ItemStack item = mock(ItemStack.class); ItemStack item = mock(ItemStack.class);
when(item.getType()).thenReturn(Material.WATER_BUCKET); 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); ghe.onPlayerInteractInNether(e);
verify(nextBlock, never()).setType(Material.WATER); verify(nextBlock, never()).setType(Material.WATER);
} }
@ -205,7 +206,7 @@ public class GreenhouseEventsTest {
when(clickedBlock.getRelative(any())).thenReturn(nextBlock); when(clickedBlock.getRelative(any())).thenReturn(nextBlock);
ItemStack item = mock(ItemStack.class); ItemStack item = mock(ItemStack.class);
when(item.getType()).thenReturn(Material.ACACIA_BOAT); 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); ghe.onPlayerInteractInNether(e);
verify(nextBlock, never()).setType(Material.WATER); verify(nextBlock, never()).setType(Material.WATER);
} }
@ -222,7 +223,7 @@ public class GreenhouseEventsTest {
when(clickedBlock.getRelative(any())).thenReturn(nextBlock); when(clickedBlock.getRelative(any())).thenReturn(nextBlock);
ItemStack item = mock(ItemStack.class); ItemStack item = mock(ItemStack.class);
when(item.getType()).thenReturn(Material.WATER_BUCKET); 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); ghe.onPlayerInteractInNether(e);
verify(nextBlock, never()).setType(Material.WATER); verify(nextBlock, never()).setType(Material.WATER);
} }

View File

@ -1,6 +1,8 @@
package world.bentobox.greenhouses.managers; package world.bentobox.greenhouses.managers;
import static org.junit.Assert.assertEquals; 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.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
@ -9,11 +11,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Keyed; import org.bukkit.Keyed;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag; import org.bukkit.Tag;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -25,13 +27,13 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.stubbing.OngoingStubbing;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.greenhouses.data.Greenhouse; import world.bentobox.greenhouses.data.Greenhouse;
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock; import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock;
/** /**
@ -39,7 +41,7 @@ import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, Tag.class}) @PrepareForTest({Bukkit.class, BentoBox.class, Tag.class, RecipeManager.class})
public class EcoSystemManagerTest { public class EcoSystemManagerTest {
private Greenhouse gh; private Greenhouse gh;
@ -53,6 +55,8 @@ public class EcoSystemManagerTest {
private Block liquid; private Block liquid;
@Mock @Mock
private Block plant; private Block plant;
@Mock
private BiomeRecipe recipe;
// CUT // CUT
private EcoSystemManager eco; private EcoSystemManager eco;
@ -62,6 +66,7 @@ public class EcoSystemManagerTest {
public void setUp() { public void setUp() {
PowerMockito.mockStatic(Tag.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Tag.class, Mockito.RETURNS_MOCKS);
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
@SuppressWarnings("unchecked")
Tag<Keyed> tag = mock(Tag.class); Tag<Keyed> tag = mock(Tag.class);
when(Bukkit.getTag(anyString(), any(), any())).thenReturn(tag); when(Bukkit.getTag(anyString(), any(), any())).thenReturn(tag);
@ -95,6 +100,12 @@ public class EcoSystemManagerTest {
// Liquid false // Liquid false
when(block.getRelative(eq(BlockFace.UP))).thenReturn(air); 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); eco = new EcoSystemManager(null, null);
} }
@ -209,4 +220,57 @@ public class EcoSystemManagerTest {
assertEquals(liquid, value.block()); 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));
}
} }