Skip to content

Commit

Permalink
Use bonemeal for twerking.
Browse files Browse the repository at this point in the history
WIP - no sounds or effects when the trees grow.

#23 (comment)
  • Loading branch information
tastybento committed Jun 11, 2021
1 parent f66c275 commit d4e62b8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 71 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.1-R0.1-SNAPSHOT</spigot.version>
<spigot.version>1.16.5-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
74 changes: 4 additions & 70 deletions src/main/java/world/bentobox/twerk/listeners/TreeGrowListener.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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;
Expand All @@ -11,11 +9,9 @@
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Location;
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 @@ -34,38 +30,9 @@

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 @@ -103,52 +70,19 @@ protected void growTree(Block b) {
if (!Tag.SAPLINGS.isTagged(t)) {
return;
}
// 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()))) {
for (int i = 0; i < 100; i++) {
if (b.applyBoneMeal(BlockFace.UP)) {
// TODO : this never gets called.
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;
}
}
return false;
}

protected void showSparkles(Block b) {
Expand Down

0 comments on commit d4e62b8

Please sign in to comment.