Skip to content

Commit

Permalink
WIP putting back tree growing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 9, 2022
1 parent 1db10a5 commit 0ee4d33
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<java.version>1.8</java.version>
<powermock.version>2.0.4</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.16.5-R0.1-SNAPSHOT</spigot.version>
<spigot.version>1.16.1-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.14.0-SNAPSHOT</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
Expand Down
89 changes: 86 additions & 3 deletions src/main/java/world/bentobox/twerk/listeners/TreeGrowListener.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package world.bentobox.twerk.listeners;

import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -11,6 +15,7 @@
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.Tag;
import org.bukkit.TreeType;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -29,6 +34,38 @@

public class TreeGrowListener implements Listener {

// The first entry in the list of the quads is where the big tree should be planted - always most positive x and z.
private static final List<BlockFace> QUAD1 = Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.NORTH_EAST, BlockFace.SELF);
private static final List<BlockFace> QUAD2 = Arrays.asList(BlockFace.NORTH_WEST, BlockFace.SELF, BlockFace.WEST, BlockFace.NORTH);
private static final List<BlockFace> QUAD3 = Arrays.asList(BlockFace.WEST, BlockFace.SOUTH, BlockFace.SOUTH_WEST, BlockFace.SELF);
private static final List<BlockFace> QUAD4 = Arrays.asList(BlockFace.SELF, BlockFace.SOUTH_EAST, BlockFace.EAST, BlockFace.SOUTH);
private static final List<List<BlockFace>> QUADS = Arrays.asList(QUAD1, QUAD2, QUAD3, QUAD4);

private static final List<BlockFace> AROUND = Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.NORTH_EAST,
BlockFace.SOUTH, BlockFace.WEST, BlockFace.NORTH_WEST, BlockFace.SOUTH_EAST, BlockFace.SOUTH_WEST);

/**
* Converts between sapling and tree type. Why doesn't this API exist already I wonder?
*/
private static final Map<Material, TreeType> SAPLING_TO_TREE_TYPE;
static {
Map<Material, TreeType> conv = new EnumMap<>(Material.class);
conv.put(Material.ACACIA_SAPLING, TreeType.ACACIA);
conv.put(Material.BIRCH_SAPLING, TreeType.BIRCH);
conv.put(Material.JUNGLE_SAPLING, TreeType.SMALL_JUNGLE);
conv.put(Material.OAK_SAPLING, TreeType.TREE);
conv.put(Material.SPRUCE_SAPLING, TreeType.REDWOOD);
SAPLING_TO_TREE_TYPE = Collections.unmodifiableMap(conv);
}
private static final Map<Material, TreeType> SAPLING_TO_BIG_TREE_TYPE;
static {
Map<Material, TreeType> conv = new EnumMap<>(Material.class);
conv.put(Material.DARK_OAK_SAPLING, TreeType.DARK_OAK);
conv.put(Material.SPRUCE_SAPLING, TreeType.MEGA_REDWOOD);
conv.put(Material.JUNGLE_SAPLING, TreeType.JUNGLE);
SAPLING_TO_BIG_TREE_TYPE = Collections.unmodifiableMap(conv);
}

private TwerkingForTrees addon;
private Map<Island, Integer> twerkCount;
private Set<Island> isTwerking;
Expand Down Expand Up @@ -67,10 +104,56 @@ protected void growTree(Block b) {
if (!Tag.SAPLINGS.isTagged(t)) {
return;
}
// Apply 100 bonemeals - this seems to be enough
for (int i = 0; i < 100; i++) {
b.applyBoneMeal(BlockFace.UP);
// Try to grow big tree if possible
if (SAPLING_TO_BIG_TREE_TYPE.containsKey(t) && bigTreeSaplings(b)) {
return;
} else if (SAPLING_TO_TREE_TYPE.containsKey(t)) {
TreeType type = SAPLING_TO_TREE_TYPE.getOrDefault(b.getType(), TreeType.TREE);
b.setType(Material.AIR);
if (b.getWorld().generateTree(b.getLocation(), type, new BlockChangeHandler(addon, b.getWorld()))) {
if (addon.getSettings().isEffectsEnabled()) {
showSparkles(b);
}
if (addon.getSettings().isSoundsEnabled()) {
b.getWorld().playSound(b.getLocation(), addon.getSettings().getSoundsGrowingSmallTreeSound(),
(float)addon.getSettings().getSoundsGrowingSmallTreeVolume(), (float)addon.getSettings().getSoundsGrowingSmallTreePitch());
}
} else {
// Tree generation failed, so reset block
b.setType(t);
}
}
}

protected boolean bigTreeSaplings(Block b) {
Material treeType = b.getType();
TreeType type = SAPLING_TO_BIG_TREE_TYPE.get(treeType);
for (List<BlockFace> q : QUADS) {
if (q.stream().map(b::getRelative).allMatch(c -> c.getType().equals(treeType))) {
// All the same sapling type found in this quad
q.stream().map(b::getRelative).forEach(c -> c.setType(Material.AIR));
// Get the tree planting location
Location l = b.getRelative(q.get(0)).getLocation();
if (b.getWorld().generateTree(l, type, new BlockChangeHandler(addon, b.getWorld()))) {
if (addon.getSettings().isEffectsEnabled()) {
showSparkles(b);
}
if (addon.getSettings().isSoundsEnabled()) {
b.getWorld().playSound(b.getLocation(), addon.getSettings().getSoundsGrowingBigTreeSound(),
(float)addon.getSettings().getSoundsGrowingBigTreeVolume(), (float)addon.getSettings().getSoundsGrowingBigTreePitch());
}
return true;
} else {
// Generation failed, reset saplings
q.stream().map(b::getRelative).forEach(c -> c.setType(treeType));
}
}
}
return false;
}

protected void showSparkles(Block b) {
AROUND.stream().map(b::getRelative).map(Block::getLocation).forEach(x -> x.getWorld().playEffect(x, addon.getSettings().getEffectsTwerk(), 0));
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
Expand Down

0 comments on commit 0ee4d33

Please sign in to comment.