Skip to content

Commit dee8255

Browse files
Multiple leaves per log & removed leaves list config
1 parent 55ae913 commit dee8255

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

src/main/java/com/zetaplugins/timberz/service/MaterialTypeChecks.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@
1414
public final class MaterialTypeChecks {
1515
private MaterialTypeChecks() {}
1616

17+
/**
18+
* Checks if a material is a leaf block based on the configuration
19+
* @param material The material to check
20+
* @param blocksConfig The blocks configuration
21+
* @return True if the material is a leaf block, false otherwise
22+
*/
1723
public static boolean isLeafBlock(Material material, FileConfiguration blocksConfig) {
18-
List<String> leafBlocks = blocksConfig.getStringList("leafBlocks");
24+
List<String> leafBlocks = blocksConfig.getStringList("logToLeafMap").stream()
25+
.filter(entry ->entry.contains(":"))
26+
.map(entry -> entry.split(":")[1])
27+
.filter(leaf -> !leaf.isEmpty())
28+
.toList();
1929

2030
for (String leafBlock : leafBlocks) {
2131
if (material.toString().equalsIgnoreCase(leafBlock)) {

src/main/java/com/zetaplugins/timberz/service/TreeDetectionService.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public final class TreeDetectionService {
1717
private final int MIN_LOGS_REQUIRED;
1818

1919
// Maps log types to their corresponding leaf types
20-
private final Map<Material, Material> LOG_TO_LEAF_MAP = new HashMap<>();
20+
private final Map<Material, List<Material>> LOG_TO_LEAF_MAP = new HashMap<>();
2121

2222
public TreeDetectionService(TimberZ plugin) {
2323
this.plugin = plugin;
@@ -45,7 +45,9 @@ public void fetchLogToLeaveMap() {
4545

4646
Material logType = Material.getMaterial(parts[0].toUpperCase());
4747
Material leafType = Material.getMaterial(parts[1].toUpperCase());
48-
if (logType != null && leafType != null) LOG_TO_LEAF_MAP.put(logType, leafType);
48+
if (logType != null && leafType != null) {
49+
LOG_TO_LEAF_MAP.computeIfAbsent(logType, k -> new ArrayList<>()).add(leafType);
50+
}
4951
}
5052
}
5153

@@ -57,9 +59,9 @@ public void fetchLogToLeaveMap() {
5759
*/
5860
public Set<Block> identifyTreeStructure(Block sourceBlock) {
5961
Material sourceType = sourceBlock.getType();
60-
Material matchingLeafType = LOG_TO_LEAF_MAP.get(sourceType);
62+
List<Material> matchingLeafTypes = LOG_TO_LEAF_MAP.get(sourceType);
6163

62-
if (matchingLeafType == null) {
64+
if (matchingLeafTypes == null) {
6365
return Collections.emptySet(); // Not a valid log type
6466
}
6567

@@ -95,7 +97,7 @@ public Set<Block> identifyTreeStructure(Block sourceBlock) {
9597
}
9698

9799
// Verify the existence of matching leaf blocks
98-
boolean hasLeaves = validateLeaves(connectedLogs, matchingLeafType);
100+
boolean hasLeaves = validateLeaves(connectedLogs, matchingLeafTypes);
99101
if (!hasLeaves) {
100102
return Collections.emptySet(); // No leaves found, not a tree
101103
}
@@ -207,7 +209,7 @@ private boolean isDiagonalConnectionValid(Block block1, Block block2, Material l
207209
/**
208210
* Validate that the connected logs have appropriate leaf blocks nearby.
209211
*/
210-
private boolean validateLeaves(Set<Block> connectedLogs, Material leafType) {
212+
private boolean validateLeaves(Set<Block> connectedLogs, List<Material> leafTypes) {
211213
int leafCount = 0;
212214
Set<Block> checkedBlocks = new HashSet<>();
213215

@@ -217,18 +219,14 @@ private boolean validateLeaves(Set<Block> connectedLogs, Material leafType) {
217219
for (int z = -MAX_SEARCH_RADIUS; z <= MAX_SEARCH_RADIUS; z++) {
218220
// Skip blocks we already checked
219221
Block checkBlock = log.getRelative(x, y, z);
220-
if (checkedBlocks.contains(checkBlock)) {
221-
continue;
222-
}
222+
if (checkedBlocks.contains(checkBlock)) continue;
223223

224224
checkedBlocks.add(checkBlock);
225225

226-
if (checkBlock.getType() == leafType) {
226+
if (leafTypes.contains(checkBlock.getType())) {
227227
leafCount++;
228228

229-
if (leafCount >= MIN_LEAVES_REQUIRED) {
230-
return true;
231-
}
229+
if (leafCount >= MIN_LEAVES_REQUIRED) return true;
232230
}
233231
}
234232
}
@@ -252,7 +250,7 @@ public boolean containsLog(Material blockType){
252250
/**
253251
* Get the corresponding leaf type for a log type
254252
*/
255-
public Material getLeafType(Material logType) {
253+
public List<Material> getLeafTypes(Material logType) {
256254
return LOG_TO_LEAF_MAP.get(logType);
257255
}
258256
}

src/main/java/com/zetaplugins/timberz/service/TreeFellerService.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import org.bukkit.Particle;
88
import org.bukkit.Sound;
99
import org.bukkit.block.Block;
10-
import org.bukkit.block.data.BlockData;
11-
import org.bukkit.block.data.type.Leaves;
1210
import org.bukkit.entity.Player;
1311
import org.bukkit.inventory.ItemStack;
1412
import org.bukkit.inventory.meta.Damageable;
@@ -38,13 +36,13 @@ public void fellTree(Player player, Block sourceBlock, Set<Block> treeBlocks, It
3836
Material logType = sourceBlock.getType();
3937

4038
// Get corresponding leaf type
41-
Material leafType = plugin.getTreeDetectionService().getLeafType(logType);
39+
List<Material> leafTypes = plugin.getTreeDetectionService().getLeafTypes(logType);
4240

4341
// Store information about tree type for replanting
4442
SaplingReplanter.TreeInfo treeInfo = saplingReplanter.analyzeTreeType(sourceBlock, treeBlocks);
4543

4644
// Collect leaf blocks associated with the tree
47-
Set<Block> leafBlocks = collectLeaves(treeBlocks, leafType);
45+
Set<Block> leafBlocks = collectLeaves(treeBlocks, leafTypes);
4846

4947
// Break logs with animation (one by one with delay)
5048
breakTreeWithAnimation(player, treeBlocks, tool, durabilityCost);
@@ -76,7 +74,7 @@ private double distanceBetween(Block block1, Block block2) {
7674
/**
7775
* Collects leaf blocks that most likely belong to the tree being cut down
7876
*/
79-
private Set<Block> collectLeaves(Set<Block> treeBlocks, Material leafType) {
77+
private Set<Block> collectLeaves(Set<Block> treeBlocks, List<Material> leafType) {
8078
Set<Block> leafBlocks = new HashSet<>();
8179
Set<Block> checkedBlocks = new HashSet<>();
8280
Set<Block> otherTreeLogs = new HashSet<>();
@@ -99,7 +97,7 @@ private Set<Block> collectLeaves(Set<Block> treeBlocks, Material leafType) {
9997

10098
checkedBlocks.add(checkBlock);
10199

102-
if (checkBlock.getType() == leafType) {
100+
if (leafType.contains(checkBlock.getType())) {
103101
leafBlocks.add(checkBlock);
104102
} else if (plugin.getTreeDetectionService().containsLog(checkBlock.getType()) && !treeBlocks.contains(checkBlock)) {
105103
// This is a log block that's not part of our tree - potential other tree

src/main/resources/blocks.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,6 @@ logToSaplingMap:
3636
- "STRIPPED_CHERRY_LOG:CHERRY_SAPLING"
3737
- "PALE_OAK_LOG:PALE_OAK_SAPLING"
3838

39-
leafBlocks:
40-
- "OAK_LEAVES"
41-
- "BIRCH_LEAVES"
42-
- "SPRUCE_LEAVES"
43-
- "JUNGLE_LEAVES"
44-
- "ACACIA_LEAVES"
45-
- "DARK_OAK_LEAVES"
46-
- "MANGROVE_LEAVES"
47-
- "CHERRY_LEAVES"
48-
4939
axes:
5040
- "WOODEN_AXE"
5141
- "STONE_AXE"

0 commit comments

Comments
 (0)