Added missing block check for greenhouse

This commit is contained in:
tastybento 2019-10-11 18:52:32 -07:00
parent 60f9b3e1f1
commit 6e13cbf999
7 changed files with 411 additions and 20 deletions

View File

@ -44,7 +44,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<powermock.version>1.7.4</powermock.version>
<powermock.version>2.0.2</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.14.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.7.0</bentobox.version>
@ -146,8 +146,8 @@
<!-- Mockito (Unit testing) -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
@ -158,11 +158,12 @@
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -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<Material, Integer> 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<Material, Integer> missingBlocks) {
this.missingBlocks = missingBlocks;
}
/**
* @return the missingBlocks
*/
public Map<Material, Integer> getMissingBlocks() {
return missingBlocks;
}
}

View File

@ -57,7 +57,7 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
private String permission = "";
private final Random random = new Random();
private Map<Material, Integer> missingBlocks;
public BiomeRecipe() {}
@ -146,13 +146,15 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
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<BiomeRecipe> {
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<Material, Integer> 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<BiomeRecipe> {
this.waterCoverage = waterCoverage;
}
/**
* @return the missingBlocks
*/
public Map<Material, Integer> getMissingBlocks() {
return missingBlocks;
}
@Override
public int compareTo(BiomeRecipe o) {
return Integer.compare(o.getPriority(), this.getPriority());

View File

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

View File

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

View File

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

View File

@ -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<GreenhouseResult> 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<GreenhouseResult> 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");
}
}