Skip to content

Commit

Permalink
Added sprinting as an option to grow trees. Fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 14, 2024
1 parent 8ba50b0 commit e32afaa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/main/java/world/bentobox/twerk/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class Settings implements ConfigObject {
@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 @@ -214,4 +218,18 @@ public boolean isHoldForTwerk() {
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;
}
}
34 changes: 30 additions & 4 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 @@ -81,6 +82,7 @@ public class TreeGrowListener implements Listener {
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 Down Expand Up @@ -112,6 +114,10 @@ protected void runChecker() {
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 Down Expand Up @@ -209,10 +215,7 @@ 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()) {
Expand All @@ -225,6 +228,28 @@ public void onTwerk(PlayerToggleSneakEvent e) {
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(player.getLocation()).ifPresent(i -> {
Expand All @@ -251,6 +276,7 @@ private void twerk(Player player) {
}

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 e32afaa

Please sign in to comment.