Skip to content

Commit

Permalink
Made greenhouse ecosystem checking async.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 17, 2021
1 parent cd5a0ce commit 27df88a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
Expand All @@ -36,6 +38,7 @@
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.data.Greenhouse;
import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult;
import world.bentobox.greenhouses.world.AsyncWorldCache;

public class BiomeRecipe implements Comparable<BiomeRecipe> {
private static final String CHANCE_FOR = "% chance for ";
Expand Down Expand Up @@ -155,21 +158,34 @@ public void addReqBlocks(Material blockMaterial, int blockQty) {
startupLog(" " + blockMaterial + " x " + blockQty);
}

// Check required blocks
/**
* Checks greenhouse meets recipe requirements.
* @return GreenhouseResult - result
*/
public Set<GreenhouseResult> checkRecipe(Greenhouse gh) {
public CompletableFuture<Set<GreenhouseResult>> checkRecipe(Greenhouse gh) {
CompletableFuture<Set<GreenhouseResult>> r = new CompletableFuture<>();
Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> checkRecipeAsync(r, gh));
return r;

}

/**
* Check greenhouse meets recipe requirements. Expected to be run async.
* @param r - future to complete when done
* @param gh - greenhouse
* @return set of results from the check
*/
private Set<GreenhouseResult> checkRecipeAsync(CompletableFuture<Set<GreenhouseResult>> r, Greenhouse gh) {
AsyncWorldCache cache = new AsyncWorldCache(gh.getWorld());
Set<GreenhouseResult> result = new HashSet<>();
long area = gh.getArea();
Map<Material, Integer> blockCount = new EnumMap<>(Material.class);

// Look through the greenhouse and count what is in there
for (int y = gh.getFloorHeight(); y< gh.getCeilingHeight();y++) {
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);
Material t = b.getType();
Material t = cache.getBlockType(x, y, z);
if (!t.equals(Material.AIR)) {
blockCount.putIfAbsent(t, 0);
blockCount.merge(t, 1, Integer::sum);
Expand Down Expand Up @@ -213,6 +229,8 @@ public Set<GreenhouseResult> checkRecipe(Greenhouse gh) {
result.add(GreenhouseResult.FAIL_INSUFFICIENT_BLOCKS);
gh.setMissingBlocks(missingBlocks);
}
// Return to main thread to complete
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> r.complete(result));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,13 @@ private void verify(Greenhouse gh) {
//addon.log("Skipping verify for unloaded greenhouse at " + gh.getLocation());
return;
}
if (!gh.getBiomeRecipe().checkRecipe(gh).isEmpty()) {
addon.log("Greenhouse failed verification at " + gh.getLocation());
g.removeGreenhouse(gh);
}
gh.getBiomeRecipe().checkRecipe(gh).thenAccept(rs -> {
if (!rs.isEmpty()) {
addon.log("Greenhouse failed verification at " + gh.getLocation());
g.removeGreenhouse(gh);
}
});

}

private void addMobs(Greenhouse gh) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ public CompletableFuture<GhResult> tryToMakeGreenhouse(Location location, BiomeR
}
// Check if the greenhouse meets the requested recipe
if (greenhouseRecipe != null) {
checkRecipe(r, finder, greenhouseRecipe, resultSet);
checkRecipe(finder, greenhouseRecipe, resultSet).thenAccept(r::complete);
return;
}

// Try ordered recipes
findRecipe(finder, resultSet);
r.complete(new GhResult().setFinder(finder).setResults(resultSet));
Expand All @@ -184,6 +183,8 @@ public CompletableFuture<GhResult> tryToMakeGreenhouse(Location location, BiomeR
* @param resultSet - result set from find
*/
private void findRecipe(GreenhouseFinder finder, Set<GreenhouseResult> resultSet) {
// TODO
/*
resultSet.add(addon.getRecipes().getBiomeRecipes().stream().sorted()
.filter(r -> r.checkRecipe(finder.getGh()).isEmpty()).findFirst()
.map(r -> {
Expand All @@ -192,7 +193,7 @@ private void findRecipe(GreenhouseFinder finder, Set<GreenhouseResult> resultSet
activateGreenhouse(finder.getGh());
handler.saveObjectAsync(finder.getGh());
return map.addGreenhouse(finder.getGh());
}).orElse(GreenhouseResult.FAIL_NO_RECIPE_FOUND));
}).orElse(GreenhouseResult.FAIL_NO_RECIPE_FOUND));*/
}

/**
Expand All @@ -203,19 +204,20 @@ private void findRecipe(GreenhouseFinder finder, Set<GreenhouseResult> resultSet
* @param resultSet - result set from finder
* @return Greenhouse result
*/
GhResult checkRecipe(CompletableFuture<GhResult> r, GreenhouseFinder finder, BiomeRecipe greenhouseRecipe, Set<GreenhouseResult> resultSet) {
resultSet = greenhouseRecipe.checkRecipe(finder.getGh());
if (resultSet.isEmpty()) {
// Success - set recipe and add to map
finder.getGh().setBiomeRecipe(greenhouseRecipe);
resultSet.add(map.addGreenhouse(finder.getGh()));
activateGreenhouse(finder.getGh());
handler.saveObjectAsync(finder.getGh());
}
GhResult recipe = new GhResult().setFinder(finder).setResults(resultSet);
r.complete(recipe);
return recipe;

CompletableFuture<GhResult> checkRecipe(GreenhouseFinder finder, BiomeRecipe greenhouseRecipe, Set<GreenhouseResult> resultSet) {
CompletableFuture<GhResult> r = new CompletableFuture<>();
greenhouseRecipe.checkRecipe(finder.getGh()).thenAccept(rs -> {
if (rs.isEmpty()) {
// Success - set recipe and add to map
finder.getGh().setBiomeRecipe(greenhouseRecipe);
resultSet.add(map.addGreenhouse(finder.getGh()));
activateGreenhouse(finder.getGh());
handler.saveObjectAsync(finder.getGh());
}
GhResult recipe = new GhResult().setFinder(finder).setResults(rs);
r.complete(recipe);
});
return r;
}

private void activateGreenhouse(Greenhouse gh) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import static org.mockito.Mockito.when;

import java.util.Optional;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -48,7 +47,6 @@
import world.bentobox.greenhouses.Settings;
import world.bentobox.greenhouses.data.Greenhouse;
import world.bentobox.greenhouses.managers.GreenhouseManager;
import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult;
import world.bentobox.greenhouses.managers.GreenhouseMap;

/**
Expand Down Expand Up @@ -237,8 +235,8 @@ public void testAddReqBlocks() {
*/
@Test
public void testCheckRecipe() {
Set<GreenhouseResult> result = br.checkRecipe(gh);
assertTrue(result.isEmpty());
br.checkRecipe(gh).thenAccept(result ->
assertTrue(result.isEmpty()));
}

/**
Expand All @@ -247,8 +245,8 @@ public void testCheckRecipe() {
@Test
public void testCheckRecipeNotEnough() {
br.addReqBlocks(Material.ACACIA_LEAVES, 3);
Set<GreenhouseResult> result = br.checkRecipe(gh);
assertFalse(result.isEmpty());
br.checkRecipe(gh).thenAccept(result ->
assertFalse(result.isEmpty()));

}

Expand Down

0 comments on commit 27df88a

Please sign in to comment.