Skip to content

Commit

Permalink
Adds recipe option to make command
Browse files Browse the repository at this point in the history
Completes missing block check.

#25
  • Loading branch information
tastybento committed Oct 12, 2019
1 parent 6e13cbf commit 31bc36c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
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();


public BiomeRecipe() {}

Expand Down Expand Up @@ -154,7 +154,6 @@ public Set<GreenhouseResult> checkRecipe(Greenhouse gh) {
}
}
}
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 @@ -181,17 +180,12 @@ 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
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);
Expand Down Expand Up @@ -352,7 +346,7 @@ public boolean growPlant(Block bl) {
bl.getRelative(BlockFace.UP).setBlockData(dataTop, false);
}
} else {
bl.setBlockData(dataBottom, false);
bl.setBlockData(dataBottom, false);
}
bl.getWorld().spawnParticle(Particle.SNOWBALL, bl.getLocation(), 10, 2, 2, 2);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package world.bentobox.greenhouses.ui.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.util.Vector;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
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 @@ -49,7 +55,35 @@ public boolean execute(User user, String label, List<String> args) {
new Panel((Greenhouses)this.getAddon()).ShowPanel(user);
return true;
}
return makeGreenhouse(user, null);
// Check recipe given matches
BiomeRecipe br = getRecipe(user, args.get(0));
if (br == null) {
user.sendMessage("greenhouses.commands.user.make.unknown-recipe");
user.sendMessage("greenhouses.commands.user.make.try-these");
getRecipes(user).forEach((k,v) -> user.sendMessage("greenhouses.commands.user.make.recipe-format", TextVariables.NAME, v.getName()));
return false;
}
return makeGreenhouse(user, br);
}

/**
* Get a recipe for user
* @param user - user
* @param arg - given string
* @return recipe or null if unknown
*/
private BiomeRecipe getRecipe(User user, String arg) {
return getRecipes(user).get(arg);
}
/**
* Get a string list of recipies the player has permission to use
* @param user - user
* @return list
*/
private Map<String, BiomeRecipe> getRecipes(User user) {
return ((Greenhouses)getAddon()).getRecipes().getBiomeRecipes().stream()
.filter(br -> user.hasPermission(br.getPermission()))
.collect(Collectors.toMap(br -> br.getName(), br -> br));
}

private boolean makeGreenhouse(User user, BiomeRecipe br) {
Expand Down Expand Up @@ -77,7 +111,15 @@ private boolean makeGreenhouse(User user, BiomeRecipe br) {
result.getFinder().getRedGlass().forEach(rg -> user.getPlayer().sendBlockChange(rg, Material.RED_STAINED_GLASS.createBlockData()));
Bukkit.getScheduler().runTaskLater(getPlugin(), () -> result.getFinder().getRedGlass().forEach(rg -> user.getPlayer().sendBlockChange(rg, rg.getBlock().getBlockData())), 120L);
}
if (br != null && 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;
}

@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
return Optional.of(new ArrayList<String>(this.getRecipes(user).keySet()));
}

}
3 changes: 3 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ greenhouses:
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]"
unknown-recipe: "&cUnknown recipe"
try-these: "&cTry one of these:"
recipe-format: "&3[name]"
info:
title: "&A[How To Build A Greenhouse]"
instructions: |
Expand Down

0 comments on commit 31bc36c

Please sign in to comment.