Fixed and modified the way the max tree block setting worked.

This commit is contained in:
Brianna 2021-02-19 17:29:12 -06:00
parent 76e3b1fc3b
commit 4d78edcb45
4 changed files with 38 additions and 16 deletions

View File

@ -1,26 +1,33 @@
package com.songoda.ultimatetimber.tree; package com.songoda.ultimatetimber.tree;
import org.bukkit.block.Block;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
public class TreeBlockSet<BlockType> implements Collection { public class TreeBlockSet<BlockType> implements Collection {
private final ITreeBlock<BlockType> initialLogBlock; private final ITreeBlock<BlockType> initialLogBlock;
private final Set<ITreeBlock<BlockType>> logBlocks, leafBlocks; private List<ITreeBlock<BlockType>> logBlocks;
private final List<ITreeBlock<BlockType>> leafBlocks;
public TreeBlockSet() { public TreeBlockSet() {
this.initialLogBlock = null; this.initialLogBlock = null;
this.logBlocks = new HashSet<>(); this.logBlocks = new LinkedList<>();
this.leafBlocks = new HashSet<>(); this.leafBlocks = new LinkedList<>();
} }
public TreeBlockSet(ITreeBlock<BlockType> initialLogBlock) { public TreeBlockSet(ITreeBlock<BlockType> initialLogBlock) {
this.initialLogBlock = initialLogBlock; this.initialLogBlock = initialLogBlock;
this.logBlocks = new HashSet<>(); this.logBlocks = new LinkedList<>();
this.leafBlocks = new HashSet<>(); this.leafBlocks = new LinkedList<>();
if (initialLogBlock != null) if (initialLogBlock != null)
this.logBlocks.add(initialLogBlock); this.logBlocks.add(initialLogBlock);
@ -40,8 +47,8 @@ public class TreeBlockSet<BlockType> implements Collection {
* *
* @return A Set of TreeBlocks * @return A Set of TreeBlocks
*/ */
public Set<ITreeBlock<BlockType>> getLogBlocks() { public List<ITreeBlock<BlockType>> getLogBlocks() {
return Collections.unmodifiableSet(this.logBlocks); return Collections.unmodifiableList(this.logBlocks);
} }
/** /**
@ -49,8 +56,8 @@ public class TreeBlockSet<BlockType> implements Collection {
* *
* @return A Set of TreeBlocks * @return A Set of TreeBlocks
*/ */
public Set<ITreeBlock<BlockType>> getLeafBlocks() { public List<ITreeBlock<BlockType>> getLeafBlocks() {
return Collections.unmodifiableSet(this.leafBlocks); return Collections.unmodifiableList(this.leafBlocks);
} }
/** /**
@ -160,6 +167,21 @@ public class TreeBlockSet<BlockType> implements Collection {
return removedAll; 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<BlockType> leafBlock : new LinkedList<>(leafBlocks))
if (leafBlock.getLocation().getY() > highest)
leafBlocks.remove(leafBlock);
}
/** /**
* Removes all tree blocks of a given type * Removes all tree blocks of a given type
* *

View File

@ -30,7 +30,7 @@ import java.util.Set;
public class TreeAnimationManager extends Manager implements Listener, Runnable { public class TreeAnimationManager extends Manager implements Listener, Runnable {
private final Set<TreeAnimation> activeAnimations; private final Set<TreeAnimation> activeAnimations;
private int taskId; private final int taskId;
public TreeAnimationManager(UltimateTimber ultimateTimber) { public TreeAnimationManager(UltimateTimber ultimateTimber) {
super(ultimateTimber); super(ultimateTimber);

View File

@ -16,7 +16,7 @@ public class TreeDetectionManager extends Manager {
private TreeDefinitionManager treeDefinitionManager; private TreeDefinitionManager treeDefinitionManager;
private PlacedBlockManager placedBlockManager; private PlacedBlockManager placedBlockManager;
private int maxLogBlocksAllowed, numLeavesRequiredForTree; private int numLeavesRequiredForTree;
private boolean onlyBreakLogsUpwards, entireTreeBase, destroyLeaves; private boolean onlyBreakLogsUpwards, entireTreeBase, destroyLeaves;
public TreeDetectionManager(UltimateTimber ultimateTimber) { public TreeDetectionManager(UltimateTimber ultimateTimber) {
@ -50,7 +50,6 @@ public class TreeDetectionManager extends Manager {
public void reload() { public void reload() {
this.treeDefinitionManager = this.plugin.getTreeDefinitionManager(); this.treeDefinitionManager = this.plugin.getTreeDefinitionManager();
this.placedBlockManager = this.plugin.getPlacedBlockManager(); this.placedBlockManager = this.plugin.getPlacedBlockManager();
this.maxLogBlocksAllowed = ConfigurationManager.Setting.MAX_LOGS_PER_CHOP.getInt();
this.numLeavesRequiredForTree = ConfigurationManager.Setting.LEAVES_REQUIRED_FOR_TREE.getInt(); this.numLeavesRequiredForTree = ConfigurationManager.Setting.LEAVES_REQUIRED_FOR_TREE.getInt();
this.onlyBreakLogsUpwards = ConfigurationManager.Setting.ONLY_DETECT_LOGS_UPWARDS.getBoolean(); this.onlyBreakLogsUpwards = ConfigurationManager.Setting.ONLY_DETECT_LOGS_UPWARDS.getBoolean();
this.entireTreeBase = ConfigurationManager.Setting.BREAK_ENTIRE_TREE_BASE.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 * @param startingBlockY The Y coordinate of the initial block
*/ */
private void recursiveBranchSearch(Set<TreeDefinition> treeDefinitions, List<Block> trunkBlocks, TreeBlockSet<Block> treeBlocks, Block block, int startingBlockY) { private void recursiveBranchSearch(Set<TreeDefinition> treeDefinitions, List<Block> trunkBlocks, TreeBlockSet<Block> treeBlocks, Block block, int startingBlockY) {
if (treeBlocks.size() > this.maxLogBlocksAllowed)
return;
for (Vector offset : this.onlyBreakLogsUpwards ? this.VALID_BRANCH_OFFSETS : this.VALID_TRUNK_OFFSETS) { for (Vector offset : this.onlyBreakLogsUpwards ? this.VALID_BRANCH_OFFSETS : this.VALID_TRUNK_OFFSETS) {
Block targetBlock = block.getRelative(offset.getBlockX(), offset.getBlockY(), offset.getBlockZ()); Block targetBlock = block.getRelative(offset.getBlockX(), offset.getBlockY(), offset.getBlockZ());
TreeBlock treeBlock = new TreeBlock(targetBlock, TreeBlockType.LOG); TreeBlock treeBlock = new TreeBlock(targetBlock, TreeBlockType.LOG);

View File

@ -28,6 +28,8 @@ import java.util.stream.Collectors;
public class TreeFallManager extends Manager implements Listener { public class TreeFallManager extends Manager implements Listener {
private int maxLogBlocksAllowed;
public TreeFallManager(UltimateTimber ultimateTimber) { public TreeFallManager(UltimateTimber ultimateTimber) {
super(ultimateTimber); super(ultimateTimber);
Bukkit.getPluginManager().registerEvents(this, ultimateTimber); Bukkit.getPluginManager().registerEvents(this, ultimateTimber);
@ -35,7 +37,7 @@ public class TreeFallManager extends Manager implements Listener {
@Override @Override
public void reload() { public void reload() {
this.maxLogBlocksAllowed = ConfigurationManager.Setting.MAX_LOGS_PER_CHOP.getInt();
} }
@Override @Override
@ -126,6 +128,8 @@ public class TreeFallManager extends Manager implements Listener {
// Valid tree and meets all conditions past this point // Valid tree and meets all conditions past this point
event.setCancelled(true); event.setCancelled(true);
detectedTree.getDetectedTreeBlocks().sortAndLimit(maxLogBlocksAllowed);
choppingManager.cooldownPlayer(player); choppingManager.cooldownPlayer(player);
// Destroy initiated block if enabled // Destroy initiated block if enabled