diff --git a/UltimateTimber/Core/src/com/songoda/ultimatetimber/tree/TreeBlockSet.java b/UltimateTimber/Core/src/com/songoda/ultimatetimber/tree/TreeBlockSet.java index b935ffa..1255522 100644 --- a/UltimateTimber/Core/src/com/songoda/ultimatetimber/tree/TreeBlockSet.java +++ b/UltimateTimber/Core/src/com/songoda/ultimatetimber/tree/TreeBlockSet.java @@ -1,26 +1,33 @@ package com.songoda.ultimatetimber.tree; +import org.bukkit.block.Block; + import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; import java.util.Set; +import java.util.stream.Collectors; public class TreeBlockSet implements Collection { private final ITreeBlock initialLogBlock; - private final Set> logBlocks, leafBlocks; + private List> logBlocks; + private final List> leafBlocks; public TreeBlockSet() { this.initialLogBlock = null; - this.logBlocks = new HashSet<>(); - this.leafBlocks = new HashSet<>(); + this.logBlocks = new LinkedList<>(); + this.leafBlocks = new LinkedList<>(); } public TreeBlockSet(ITreeBlock initialLogBlock) { this.initialLogBlock = initialLogBlock; - this.logBlocks = new HashSet<>(); - this.leafBlocks = new HashSet<>(); + this.logBlocks = new LinkedList<>(); + this.leafBlocks = new LinkedList<>(); if (initialLogBlock != null) this.logBlocks.add(initialLogBlock); @@ -40,8 +47,8 @@ public class TreeBlockSet implements Collection { * * @return A Set of TreeBlocks */ - public Set> getLogBlocks() { - return Collections.unmodifiableSet(this.logBlocks); + public List> getLogBlocks() { + return Collections.unmodifiableList(this.logBlocks); } /** @@ -49,8 +56,8 @@ public class TreeBlockSet implements Collection { * * @return A Set of TreeBlocks */ - public Set> getLeafBlocks() { - return Collections.unmodifiableSet(this.leafBlocks); + public List> getLeafBlocks() { + return Collections.unmodifiableList(this.leafBlocks); } /** @@ -160,6 +167,21 @@ public class TreeBlockSet implements Collection { return removedAll; } + public void sortAndLimit(int max) { + if (logBlocks.size() < max) + return; + + logBlocks = logBlocks.stream().sorted(Comparator.comparingInt(b -> b.getLocation().getBlockY())) + .limit(max).collect(Collectors.toList()); + + int highest = logBlocks.get(logBlocks.size() - 1).getLocation().getBlockY(); + + if (logBlocks.size() >= max) + for (ITreeBlock leafBlock : new LinkedList<>(leafBlocks)) + if (leafBlock.getLocation().getY() > highest) + leafBlocks.remove(leafBlock); + } + /** * Removes all tree blocks of a given type * diff --git a/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeAnimationManager.java b/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeAnimationManager.java index a08ab3f..2a8b87e 100644 --- a/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeAnimationManager.java +++ b/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeAnimationManager.java @@ -30,7 +30,7 @@ import java.util.Set; public class TreeAnimationManager extends Manager implements Listener, Runnable { private final Set activeAnimations; - private int taskId; + private final int taskId; public TreeAnimationManager(UltimateTimber ultimateTimber) { super(ultimateTimber); diff --git a/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeDetectionManager.java b/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeDetectionManager.java index b1f5ce4..00bb59e 100644 --- a/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeDetectionManager.java +++ b/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeDetectionManager.java @@ -16,7 +16,7 @@ public class TreeDetectionManager extends Manager { private TreeDefinitionManager treeDefinitionManager; private PlacedBlockManager placedBlockManager; - private int maxLogBlocksAllowed, numLeavesRequiredForTree; + private int numLeavesRequiredForTree; private boolean onlyBreakLogsUpwards, entireTreeBase, destroyLeaves; public TreeDetectionManager(UltimateTimber ultimateTimber) { @@ -50,7 +50,6 @@ public class TreeDetectionManager extends Manager { public void reload() { this.treeDefinitionManager = this.plugin.getTreeDefinitionManager(); this.placedBlockManager = this.plugin.getPlacedBlockManager(); - this.maxLogBlocksAllowed = ConfigurationManager.Setting.MAX_LOGS_PER_CHOP.getInt(); this.numLeavesRequiredForTree = ConfigurationManager.Setting.LEAVES_REQUIRED_FOR_TREE.getInt(); this.onlyBreakLogsUpwards = ConfigurationManager.Setting.ONLY_DETECT_LOGS_UPWARDS.getBoolean(); this.entireTreeBase = ConfigurationManager.Setting.BREAK_ENTIRE_TREE_BASE.getBoolean(); @@ -154,9 +153,6 @@ public class TreeDetectionManager extends Manager { * @param startingBlockY The Y coordinate of the initial block */ private void recursiveBranchSearch(Set treeDefinitions, List trunkBlocks, TreeBlockSet treeBlocks, Block block, int startingBlockY) { - if (treeBlocks.size() > this.maxLogBlocksAllowed) - return; - for (Vector offset : this.onlyBreakLogsUpwards ? this.VALID_BRANCH_OFFSETS : this.VALID_TRUNK_OFFSETS) { Block targetBlock = block.getRelative(offset.getBlockX(), offset.getBlockY(), offset.getBlockZ()); TreeBlock treeBlock = new TreeBlock(targetBlock, TreeBlockType.LOG); diff --git a/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeFallManager.java b/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeFallManager.java index 65d21c5..804fbd9 100644 --- a/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeFallManager.java +++ b/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeFallManager.java @@ -28,6 +28,8 @@ import java.util.stream.Collectors; public class TreeFallManager extends Manager implements Listener { + private int maxLogBlocksAllowed; + public TreeFallManager(UltimateTimber ultimateTimber) { super(ultimateTimber); Bukkit.getPluginManager().registerEvents(this, ultimateTimber); @@ -35,7 +37,7 @@ public class TreeFallManager extends Manager implements Listener { @Override public void reload() { - + this.maxLogBlocksAllowed = ConfigurationManager.Setting.MAX_LOGS_PER_CHOP.getInt(); } @Override @@ -126,6 +128,8 @@ public class TreeFallManager extends Manager implements Listener { // Valid tree and meets all conditions past this point event.setCancelled(true); + detectedTree.getDetectedTreeBlocks().sortAndLimit(maxLogBlocksAllowed); + choppingManager.cooldownPlayer(player); // Destroy initiated block if enabled