33import java .util .*;
44
55import org .bukkit .Material ;
6- import org .bukkit .block .Block ;
7- import org .bukkit .entity .Player ;
86import org .bukkit .event .inventory .InventoryType ;
97import org .bukkit .inventory .Inventory ;
108import org .bukkit .inventory .ItemStack ;
119import org .bukkit .inventory .meta .ItemMeta ;
10+ import org .bukkit .block .Block ;
11+ import org .bukkit .entity .Player ;
1212
1313public class Utility {
1414
@@ -24,52 +24,45 @@ public static boolean isVeinBlock(Material m) {
2424 }
2525
2626
27- public static void destroyBranch (Player p , Block startBlock , boolean down ) {
28- Material blockType = startBlock .getType (); // Type of block to match
29- Set <Block > visited = new HashSet <>(); // Tracks blocks we've already processed
30- Deque <Block > toVisit = new ArrayDeque <>(); // BFS queue to process blocks layer by layer (ArrayDeque is used for efficiency)
31-
32- // Initialize with the starting block
33- toVisit .add (startBlock );
34- visited .add (startBlock );
27+ public static void destroyBranch (Player p , Block b , boolean down ) {
28+ Material branchType = b .getType ();
29+ final Queue <Block > blocksToDestroy = new LinkedList <>();
30+ final Set <Block > processedBlocks = new HashSet <>();
31+ blocksToDestroy .add (b );
3532
36- while (!toVisit .isEmpty ()) {
37- Block currentBlock = toVisit .poll (); // Remove and process the current block
33+ while (!blocksToDestroy .isEmpty ()) {
34+ Block currentBlock = blocksToDestroy .remove ();
35+ Material currentBlockType = currentBlock .getType ();
3836
39- // Ensure the block type still matches
40- if (!currentBlock .getType ().equals (blockType )) continue ;
37+ if (processedBlocks .contains (currentBlock ) || currentBlockType != branchType ) continue ;
4138
42- // Try to break the block
43- boolean broken = p .breakBlock (currentBlock );
44- if (!broken ) continue ; // If the block couldn't be broken, skip it
39+ processedBlocks .add (currentBlock );
4540
46- // Check all adjacent blocks within a 3x3x3 cube around the current block
4741 for (int x = -1 ; x <= 1 ; x ++) {
48- for (int y = (down ? -1 : 0 ); y <= 1 ; y ++) { // If 'down' is true, check below
49- for (int z = -1 ; z <= 1 ; z ++) {
50- // Skip the current block itself (x == 0, y == 0, z == 0)
51- if (x == 0 && y == 0 && z == 0 ) continue ;
52-
42+ for (int z = -1 ; z <= 1 ; z ++) {
43+ for (int y = (down ? -1 : 0 ); y <= 1 ; y ++) {
5344 Block neighbor = currentBlock .getRelative (x , y , z );
54-
55- // If the neighbor is the same block type and hasn't been visited, add it to the deque
56- if (!visited .contains (neighbor ) && neighbor .getType ().equals (blockType )) {
57- toVisit .add (neighbor ); // Add to the end of the deque
58- visited .add (neighbor ); // Mark it as visited immediately to avoid re-adding it
45+ if (neighbor .getType () == branchType ) {
46+ blocksToDestroy .add (neighbor );
5947 }
6048 }
6149 }
6250 }
51+
52+ currentBlock .breakNaturally (p .getInventory ().getItemInMainHand ());
53+
6354 }
6455 }
6556
57+
6658 /**
6759 * Sorts and combines an array of ItemStacks.
6860 *
6961 * @param items the ItemStack array to sort and combine
7062 * @return the sorted and combined ItemStack array
7163 */
72- @ SuppressWarnings ("all" )
64+
65+ @ SuppressWarnings ("all" )
7366 public static ItemStack [] sortAndCombine (ItemStack [] items ) {
7467 // Map to store item counts with metadata as the key
7568 Map <ItemStack , Integer > itemCount = new HashMap <>();
0 commit comments