Skip to content

Commit

Permalink
Release 1.5.0 (#32)
Browse files Browse the repository at this point in the history
* Version 1.4.3

* Remove sync chunk loading when twerking

#24

* WIP putting back tree growing code.

* Version 1.4.4

* Revert back to the original way to grow trees.

Apparently, the bee hive bug has been fixed. This will therefore prevent
trees growing outside of the island border again.

#23
#9

* Add 1.17 and 1.19 trees to the twerking.

* Added GitHubs action script and funding info

* Update pom.xml

* Fix javadoc plugin in POM

* BentoBox 2.0.0 API

* Update pom.xml version 1.5.0

* Added cherry and grow trees that may have beehives on them.

Update to latest APIs

* Fix predicate check

* Remove non-saplings

* Add sonar cube

* Refactor code to simplify

* Added option to press and hold crouch key to "twerk" #27

* Added sprinting as an option to grow trees. Fixes #14

---------

Co-authored-by: BONNe <[email protected]>
  • Loading branch information
tastybento and BONNe authored Jan 14, 2024
1 parent baca35a commit ccca7cc
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 29 deletions.
36 changes: 36 additions & 0 deletions src/main/java/world/bentobox/twerk/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ public class Settings implements ConfigObject {
@ConfigComment("If the player has not twerked enough, then the tree will not grow faster.")
@ConfigEntry(path = "minimum-twerks")
private int minimumTwerks = 4;

@ConfigComment("Hold to twerk. Accessibility feature. Instead of hitting the crouch button continuously, hold it down.")
@ConfigEntry(path = "hold-for-twerk")
private boolean holdForTwerk = false;

@ConfigComment("Use sprinting to grow trees instead of twerking.")
@ConfigEntry(path = "sprint-to-grow")
private boolean sprintToGrow = false;

@ConfigComment("Range to look for saplings when twerking. A range of 5 will look +/- 5 blocks in all directions around the player")
@ConfigComment("Making this too big will lag your server.")
@ConfigEntry(path = "range")
Expand Down Expand Up @@ -196,4 +204,32 @@ public int getRange() {
public void setRange(int range) {
this.range = range;
}

/**
* @return the holdForTwerk
*/
public boolean isHoldForTwerk() {
return holdForTwerk;
}

/**
* @param holdForTwerk the holdForTwerk to set
*/
public void setHoldForTwerk(boolean holdForTwerk) {
this.holdForTwerk = holdForTwerk;
}

/**
* @return the sprintToGrow
*/
public boolean isSprintToGrow() {
return sprintToGrow;
}

/**
* @param sprintToGrow the sprintToGrow to set
*/
public void setSprintToGrow(boolean sprintToGrow) {
this.sprintToGrow = sprintToGrow;
}
}
120 changes: 93 additions & 27 deletions src/main/java/world/bentobox/twerk/listeners/TreeGrowListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.event.player.PlayerToggleSprintEvent;
import org.bukkit.event.world.StructureGrowEvent;
import org.eclipse.jdt.annotation.NonNull;

Expand Down Expand Up @@ -80,6 +81,8 @@ public class TreeGrowListener implements Listener {
private Map<Island, Integer> twerkCount;
private Set<Island> isTwerking;
private Map<Location, Island> plantedTrees;
private Set<Player> twerkers = new HashSet<>();
private Set<Player> sprinters = new HashSet<>();

public TreeGrowListener(@NonNull TwerkingForTrees addon) {
this.addon = addon;
Expand All @@ -98,7 +101,7 @@ protected void runChecker() {
plantedTrees.values().removeIf(i -> !isTwerking.contains(i));
}
, 0L, 40L);
// Every 20 seconds
// Every 10 seconds
Bukkit.getScheduler().runTaskTimer(addon.getPlugin(), () ->
plantedTrees
.entrySet()
Expand All @@ -107,6 +110,14 @@ protected void runChecker() {
.map(Map.Entry::getKey)
.forEach(b -> Util.getChunkAtAsync(b).thenRun(() -> growTree(b.getBlock())))
, 10L, 400L);
// Simulate twerking
if (addon.getSettings().isHoldForTwerk()) {
Bukkit.getScheduler().runTaskTimer(addon.getPlugin(), () -> twerkers.forEach(this::twerk), 0L, 5L);
}
// Sprinting
if (addon.getSettings().isSprintToGrow()) {
Bukkit.getScheduler().runTaskTimer(addon.getPlugin(), () -> sprinters.forEach(this::twerk), 0L, 5L);
}
}

protected void growTree(Block b) {
Expand All @@ -117,7 +128,8 @@ protected void growTree(Block b) {
// 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)) {
}
if (SAPLING_TO_TREE_TYPE.containsKey(t)) {
TreeType type = SAPLING_TO_TREE_TYPE.getOrDefault(b.getType(), TreeType.TREE);
b.setType(Material.AIR);

Expand Down Expand Up @@ -174,30 +186,52 @@ protected void showSparkles(Block b) {
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());
}

for (List<BlockFace> quad : QUADS) {
if (isQuadOfSameType(b, quad, treeType)) {
clearSaplings(b, quad);
Location treeLocation = b.getRelative(quad.get(0)).getLocation();

if (generateBigTree(b, treeLocation, type)) {
playBigTreeEffectsAndSounds(b);
return true;
} else {
// Generation failed, reset saplings
q.stream().map(b::getRelative).forEach(c -> c.setType(treeType));
resetSaplings(b, quad, treeType);
}
}
}
return false;
}

private boolean isQuadOfSameType(Block b, List<BlockFace> quad, Material treeType) {
return quad.stream().map(b::getRelative).allMatch(c -> c.getType().equals(treeType));
}

private void clearSaplings(Block b, List<BlockFace> quad) {
quad.stream().map(b::getRelative).forEach(c -> c.setType(Material.AIR));
}

private boolean generateBigTree(Block b, Location location, TreeType type) {
return b.getWorld().generateTree(location, RAND, type,
bs -> Flags.TREES_GROWING_OUTSIDE_RANGE.isSetForWorld(bs.getWorld())
|| addon.getIslands().getProtectedIslandAt(bs.getLocation()).isPresent());
}

private void playBigTreeEffectsAndSounds(Block b) {
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());
}
}

private void resetSaplings(Block b, List<BlockFace> quad, Material treeType) {
quad.stream().map(b::getRelative).forEach(c -> c.setType(treeType));
}

protected void showSparkles(Block b) {
AROUND.stream().map(b::getRelative).map(Block::getLocation).forEach(x -> x.getWorld().playEffect(x, addon.getSettings().getEffectsTwerk(), 0));
}
Expand All @@ -214,18 +248,48 @@ public void onTreeGrow(StructureGrowEvent e) {

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onTwerk(PlayerToggleSneakEvent e) {
if (!e.getPlayer().getWorld().getEnvironment().equals(Environment.NORMAL)
|| !addon.getPlugin().getIWM().inWorld(Util.getWorld(e.getPlayer().getWorld()))
|| e.getPlayer().isFlying()
|| !e.getPlayer().hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getPlayer().getWorld()) + "twerkingfortrees")) {
if (check(e.getPlayer())) {
return;
}
if (addon.getSettings().isHoldForTwerk()) {
Player player = e.getPlayer();
if (!twerkers.add(player)) {
twerkers.remove(player);
}
return;
}
twerk(e.getPlayer());
}

private boolean check(Player player) {
return !player.getWorld().getEnvironment().equals(Environment.NORMAL)
|| !addon.getPlugin().getIWM().inWorld(Util.getWorld(player.getWorld())) || player.isFlying()
|| !player.hasPermission(
addon.getPlugin().getIWM().getPermissionPrefix(player.getWorld()) + "twerkingfortrees");
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSprint(PlayerToggleSprintEvent e) {
if (check(e.getPlayer())) {
return;
}
if (addon.getSettings().isSprintToGrow()) {
Player player = e.getPlayer();
if (!sprinters.add(player)) {
sprinters.remove(player);
}
return;
}
twerk(e.getPlayer());
}

private void twerk(Player player) {
// Get the island
addon.getIslands().getIslandAt(e.getPlayer().getLocation()).ifPresent(i -> {
addon.getIslands().getIslandAt(player.getLocation()).ifPresent(i -> {
// Check if there are any planted saplings around player
if (!twerkCount.containsKey(i) || twerkCount.get(i) == 0) {
// New twerking effort
getNearbySaplings(e.getPlayer(), i);
getNearbySaplings(player, i);
}
if (!plantedTrees.values().contains(i)) {
// None, so return
Expand All @@ -235,15 +299,17 @@ public void onTwerk(PlayerToggleSneakEvent e) {
twerkCount.putIfAbsent(i, 0);
int count = twerkCount.get(i) + 1;
twerkCount.put(i, count);
if (count == addon.getSettings().getMinimumTwerks()) {
e.getPlayer().playSound(e.getPlayer().getLocation(), addon.getSettings().getSoundsTwerkSound(),
if (count >= addon.getSettings().getMinimumTwerks()) {
player.playSound(player.getLocation(), addon.getSettings().getSoundsTwerkSound(),
(float)addon.getSettings().getSoundsTwerkVolume(), (float)addon.getSettings().getSoundsTwerkPitch());
e.getPlayer().spawnParticle(Particle.SPELL, e.getPlayer().getLocation(), 20, 3D, 0D, 3D);
player.spawnParticle(Particle.SPELL, player.getLocation(), 20, 3D, 0D, 3D);
}
});

}

private void getNearbySaplings(Player player, Island i) {
plantedTrees.values().removeIf(i::equals);
int range = addon.getSettings().getRange();
for (int x = player.getLocation().getBlockX() - range ; x <= player.getLocation().getBlockX() + range; x++) {
for (int y = player.getLocation().getBlockY() - range ; y <= player.getLocation().getBlockY() + range; y++) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# TwerkingForTrees configuration file.
#
# TwerkingForTrees configuration file. {$version}
#
# How many times the player must twerk before the tree start growing faster.
# If the player has not twerked enough, then the tree will not grow faster.
minimum-twerks: 4
# Hold to twerk. Accessibility feature. Instead of hitting the crouch button continuously, hold it down.
hold-for-twerk: false
# Use sprinting to grow trees instead of twerking.
sprint-to-grow: false
# Range to look for saplings when twerking. A range of 5 will look +/- 5 blocks in all directions around the player
# Making this too big will lag your server.
range: 5
Expand Down

0 comments on commit ccca7cc

Please sign in to comment.