Skip to content

Commit

Permalink
Added support for ceiling growing plants.
Browse files Browse the repository at this point in the history
VINES, WEEPING_VINES_PLANT, SPORE_BLOSSOM, CAVE_VINES_PLANT,
TWISTING_VINES_PLANT

#81
  • Loading branch information
tastybento committed Sep 18, 2021
1 parent 308cb7f commit 4708a70
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package world.bentobox.greenhouses.greenhouse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashSet;
Expand Down Expand Up @@ -37,6 +38,7 @@
import world.bentobox.bentobox.util.Util;
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.data.Greenhouse;
import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock;
import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult;
import world.bentobox.greenhouses.world.AsyncWorldCache;

Expand All @@ -49,6 +51,15 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
private String name;
private String friendlyName;

private static final List<Material> CEILING_PLANTS = new ArrayList<>();
static {
CEILING_PLANTS.add(Material.VINE);
Enums.getIfPresent(Material.class, "SPORE_BLOSSOM").toJavaUtil().ifPresent(CEILING_PLANTS::add);
Enums.getIfPresent(Material.class, "CAVE_VINES_PLANT").toJavaUtil().ifPresent(CEILING_PLANTS::add);
Enums.getIfPresent(Material.class, "TWISTING_VINES_PLANT").toJavaUtil().ifPresent(CEILING_PLANTS::add);
Enums.getIfPresent(Material.class, "WEEPING_VINES_PLANT").toJavaUtil().ifPresent(CEILING_PLANTS::add);
}

private static final List<BlockFace> ADJ_BLOCKS = Arrays.asList( BlockFace.DOWN, BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.UP, BlockFace.WEST);

// Content requirements
Expand All @@ -63,7 +74,6 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {

// Conversions
// Original Material, Original Type, New Material, New Type, Probability
//private final Map<Material, GreenhouseBlockConversions> conversionBlocks = new EnumMap<>(Material.class);
private final Multimap<Material, GreenhouseBlockConversions> conversionBlocks = ArrayListMultimap.create();

private int mobLimit;
Expand Down Expand Up @@ -427,28 +437,18 @@ public int getWaterCoverage() {

/**
* Plants a plant on block bl if it makes sense.
* @param bl - block
* @param bl - block that can have growth
* @return true if successful
*/
public boolean growPlant(Block bl) {
public boolean growPlant(GrowthBlock block) {
Block bl = block.block();
if (!bl.isEmpty()) {
return false;
}
return getRandomPlant().map(p -> {
if (bl.getY() != 0 && Optional.of(p.plantGrownOn()).map(m -> m.equals(bl.getRelative(BlockFace.DOWN).getType())).orElse(false)) {
BlockData dataBottom = p.plantMaterial().createBlockData();
if (dataBottom instanceof Bisected) {
((Bisected) dataBottom).setHalf(Bisected.Half.BOTTOM);
BlockData dataTop = p.plantMaterial().createBlockData();
((Bisected) dataTop).setHalf(Bisected.Half.TOP);
if (bl.getRelative(BlockFace.UP).getType().equals(Material.AIR)) {
bl.setBlockData(dataBottom, false);
bl.getRelative(BlockFace.UP).setBlockData(dataTop, false);
} else {
return false; // No room
}
} else {
bl.setBlockData(dataBottom, false);
if (bl.getY() != 0 && canGrowOn(block, p)) {
if (!isBisected(bl, p)) {
return false;
}
bl.getWorld().spawnParticle(Particle.SNOWBALL, bl.getLocation(), 10, 2, 2, 2);
return true;
Expand All @@ -457,6 +457,32 @@ public boolean growPlant(Block bl) {
}).orElse(false);
}

private boolean isBisected(Block bl, GreenhousePlant p) {
BlockData dataBottom = p.plantMaterial().createBlockData();
if (dataBottom instanceof Bisected bi) {
bi.setHalf(Bisected.Half.BOTTOM);
BlockData dataTop = p.plantMaterial().createBlockData();
((Bisected) dataTop).setHalf(Bisected.Half.TOP);
if (bl.getRelative(BlockFace.UP).getType().equals(Material.AIR)) {
bl.setBlockData(dataBottom, false);
bl.getRelative(BlockFace.UP).setBlockData(dataTop, false);
} else {
return false; // No room
}
} else {
bl.setBlockData(dataBottom, false);
}
return true;
}

private boolean canGrowOn(GrowthBlock block, GreenhousePlant p) {
// Ceiling plants can only grow on ceiling blocks
if (CEILING_PLANTS.contains(p.plantMaterial()) && block.floor()) {
return false;
}
return p.plantGrownOn().equals(block.block().getRelative(block.floor() ? BlockFace.DOWN : BlockFace.UP).getType());
}

/**
* @param friendlyName - set the friendly name
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ private void addMobs(Greenhouse gh) {
.filter(e -> gh.getBiomeRecipe().getMobTypes().contains(e.getType()))
.filter(e -> gh.contains(e.getLocation())).count();
// Get the blocks in the greenhouse where spawning could occur
List<Block> list = new ArrayList<>(getAvailableBlocks(gh, false));
List<GrowthBlock> list = new ArrayList<>(getAvailableBlocks(gh, false));
Collections.shuffle(list, new Random(System.currentTimeMillis()));
Iterator<Block> it = list.iterator();
Iterator<GrowthBlock> it = list.iterator();
// Check if the greenhouse is full
while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) {
// Spawn something if chance says so
if (gh.getBiomeRecipe().spawnMob(it.next())) {
if (gh.getBiomeRecipe().spawnMob(it.next().block())) {
// Add a mob to the sum in the greenhouse
sum++;
}
Expand All @@ -179,7 +179,7 @@ private void growPlants(Greenhouse gh) {
int bonemeal = getBoneMeal(gh);
if (bonemeal > 0) {
// Get a list of all available blocks
List<Block> list = getAvailableBlocks(gh, true);
List<GrowthBlock> list = getAvailableBlocks(gh, true);
Collections.shuffle(list);
int plantsGrown = list.stream().limit(bonemeal).mapToInt(bl -> gh.getBiomeRecipe().growPlant(bl) ? 1 : 0).sum();
if (plantsGrown > 0) {
Expand All @@ -204,25 +204,32 @@ private void setBoneMeal(Greenhouse gh, int value) {

}


/**
* Get a list of the lowest level blocks inside the greenhouse. May be air, liquid or plants.
* These blocks sit just above solid blocks
* @param gh - greenhouse
* @param ignoreLiquid - true if liquid blocks should be treated like air blocks
* @return List of blocks
*/
public List<Block> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
List<Block> result = new ArrayList<>();
protected List<GrowthBlock> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
List<GrowthBlock> result = new ArrayList<>();
if (gh.getWorld() == null) return result;
for (double x = gh.getInternalBoundingBox().getMinX(); x < gh.getInternalBoundingBox().getMaxX(); x++) {
for (double z = gh.getInternalBoundingBox().getMinZ(); z < gh.getInternalBoundingBox().getMaxZ(); z++) {
for (double y = gh.getInternalBoundingBox().getMaxY() - 1; y >= gh.getBoundingBox().getMinY(); y--) {
Block b = gh.getWorld().getBlockAt(NumberConversions.floor(x), NumberConversions.floor(y), NumberConversions.floor(z));
// Check ceiling blocks
if (b.isEmpty() && !b.getRelative(BlockFace.UP).isEmpty()) {
result.add(new GrowthBlock(b, false));
}

// Check floor blocks
if (!(b.isEmpty() || (ignoreLiquid && b.isLiquid()))
&& (b.getRelative(BlockFace.UP).isEmpty()
|| (b.getRelative(BlockFace.UP).isPassable() && !b.isLiquid())
|| (ignoreLiquid && b.isLiquid() && b.getRelative(BlockFace.UP).isPassable()))) {
result.add(b.getRelative(BlockFace.UP));
result.add(new GrowthBlock(b.getRelative(BlockFace.UP), true));
break;
}
}
Expand All @@ -231,6 +238,8 @@ public List<Block> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
return result;
}

public record GrowthBlock(Block block, Boolean floor) {}

private int getBoneMeal(Greenhouse gh) {
Hopper hopper = getHopper(gh);
if (hopper == null || !hopper.getInventory().contains(Material.BONE_MEAL)) {
Expand Down

0 comments on commit 4708a70

Please sign in to comment.