Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 17, 2021
1 parent 148c92e commit b90cba0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public int getArea() {
/**
* @return the world
*/
@NonNull
@Nullable
public World getWorld() {
return this.getLocation().getWorld();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package world.bentobox.greenhouses.managers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import org.bukkit.Location;
import org.bukkit.block.Biome;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
Expand Down Expand Up @@ -167,12 +170,17 @@ public CompletableFuture<GhResult> tryToMakeGreenhouse(Location location, BiomeR
}
// Check if the greenhouse meets the requested recipe
if (greenhouseRecipe != null) {
checkRecipe(finder, greenhouseRecipe, resultSet).thenAccept(r::complete);
checkRecipe(finder, greenhouseRecipe, resultSet).thenAccept(rs -> {
r.complete(rs);
});
return;
}
// Try ordered recipes
findRecipe(finder, resultSet);
r.complete(new GhResult().setFinder(finder).setResults(resultSet));
findRecipe(finder).thenAccept(rs -> {
resultSet.addAll(rs);
r.complete(new GhResult().setFinder(finder).setResults(resultSet));
});

});
return r;
}
Expand All @@ -182,18 +190,29 @@ public CompletableFuture<GhResult> tryToMakeGreenhouse(Location location, BiomeR
* @param finder - finder object
* @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 -> {
// Success - set recipe and add to map
finder.getGh().setBiomeRecipe(r);
activateGreenhouse(finder.getGh());
handler.saveObjectAsync(finder.getGh());
return map.addGreenhouse(finder.getGh());
}).orElse(GreenhouseResult.FAIL_NO_RECIPE_FOUND));*/
private CompletableFuture<Set<GreenhouseResult>> findRecipe(GreenhouseFinder finder) {
CompletableFuture<Set<GreenhouseResult>> r = new CompletableFuture<>();
// Get sorted list of all recipes
List<BiomeRecipe> list = addon.getRecipes().getBiomeRecipes().stream().sorted().collect(Collectors.toList());
findRecipe(r, list, finder);
return r;
}

private void findRecipe(CompletableFuture<Set<GreenhouseResult>> r, List<BiomeRecipe> list,
GreenhouseFinder finder) {
if (list.isEmpty()) {
r.complete(Collections.singleton(GreenhouseResult.FAIL_NO_RECIPE_FOUND));
return;
}
BiomeRecipe br = list.get(0);
list.remove(0);
br.checkRecipe(finder.getGh()).thenAccept(results -> {
if (results.isEmpty()) {
findRecipe(r, list, finder);
} else {
r.complete(results);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ private Map<String, BiomeRecipe> getRecipes(User user) {
.collect(Collectors.toMap(br -> br.getName(), br -> br));
}

/**
* @param user - user
* @param br requested biome recipe, or null to try anything
* @return true if successful
*/
private boolean makeGreenhouse(User user, BiomeRecipe br) {
// Check flag
if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ private ChunkSnapshot getSnap(final int x, final int z) {
if (cache.containsKey(key)) {
return cache.get(key);
}
ChunkSnapshot cs;
ChunkSnapshot cs = null;
try {
// Block on getting the chunk because this is running async
cs = getAChunk(key.x, key.z).get();
} catch (InterruptedException | ExecutionException e) {
// Try again...
return getSnap(x,z);
Greenhouses.getInstance().logError("Could not get chunk! " + e);
Thread.currentThread().interrupt();
}
// Store in cache
cache.put(key, cs);
Expand Down

0 comments on commit b90cba0

Please sign in to comment.