Skip to content

Commit

Permalink
Added missing block check for greenhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Oct 12, 2019
1 parent 60f9b3e commit 6e13cbf
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 20 deletions.
9 changes: 5 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>


Expand Down
18 changes: 18 additions & 0 deletions src/main/java/world/bentobox/greenhouses/data/Greenhouse.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,6 +38,8 @@ public class Greenhouse implements DataObject {
private String biomeRecipeName;

private boolean broken;

private Map<Material, Integer> missingBlocks;

/**
* Constructor for database
Expand Down Expand Up @@ -204,4 +208,18 @@ public BiomeRecipe getBiomeRecipe() {
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down Expand Up @@ -146,13 +146,15 @@ public Set<GreenhouseResult> checkRecipe(Greenhouse gh) {
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;
Expand All @@ -179,12 +181,21 @@ public Set<GreenhouseResult> checkRecipe(Greenhouse gh) {
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;
}

Expand Down Expand Up @@ -434,13 +445,6 @@ public void setWatercoverage(int waterCoverage) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public enum GreenhouseResult {
FAIL_OVERLAPPING,
NULL,
SUCCESS,
FAIL_NO_RECIPE_FOUND
FAIL_NO_RECIPE_FOUND,
FAIL_INSUFFICIENT_BLOCKS
}

private final Greenhouses addon;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package world.bentobox.greenhouses.ui.panel;

import org.bukkit.Bukkit;
Expand All @@ -9,9 +6,11 @@
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;
Expand Down Expand Up @@ -69,6 +68,9 @@ private boolean makeGreenhouse(User user, BiomeRecipe br) {
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;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down

0 comments on commit 6e13cbf

Please sign in to comment.