diff --git a/build.gradle b/build.gradle index 7bc327d..70ad262 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,7 @@ repositories { maven { url "https://maven.enginehub.org/repo/" } maven { url = 'https://repo.extendedclip.com/releases/' } maven { url = "https://maven.zetaplugins.com/" } + maven { url "https://repo.nyon.dev/releases" } } def targetJavaVersion = 21 @@ -77,6 +78,7 @@ dependencies { compileOnly 'com.sk89q.worldguard:worldguard-bukkit:7.0.13' compileOnly 'me.clip:placeholderapi:2.11.6' compileOnly 'dev.aurelium:auraskills-api-bukkit:2.2.4' + compileOnly 'dev.nyon:magnetic:3.10.0-1.21.6+paper' implementation "com.fasterxml.jackson.core:jackson-databind:2.16.1" shadow "com.zetaplugins:zetacore:1.7.1" diff --git a/src/main/java/com/zetaplugins/timberz/handler/TreeAnimationHandler.java b/src/main/java/com/zetaplugins/timberz/handler/TreeAnimationHandler.java index d023d62..9b3c66c 100644 --- a/src/main/java/com/zetaplugins/timberz/handler/TreeAnimationHandler.java +++ b/src/main/java/com/zetaplugins/timberz/handler/TreeAnimationHandler.java @@ -1,6 +1,7 @@ package com.zetaplugins.timberz.handler; import com.zetaplugins.timberz.TimberZ; +import com.zetaplugins.timberz.service.magnetic.BreakingHelper; import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -51,8 +52,7 @@ public void run() { createBreakEffect(player, block, direction); // Break the block naturally to drop items - Material blockType = block.getType(); - block.breakNaturally(player.getInventory().getItemInMainHand()); + BreakingHelper.breakBlock(block, player, player.getInventory().getItemInMainHand(), true); // Random chance to spawn floating wood chips if (random.nextInt(5) == 0) { diff --git a/src/main/java/com/zetaplugins/timberz/listener/LeafBreakListener.java b/src/main/java/com/zetaplugins/timberz/listener/LeafBreakListener.java index 79006fc..988e578 100644 --- a/src/main/java/com/zetaplugins/timberz/listener/LeafBreakListener.java +++ b/src/main/java/com/zetaplugins/timberz/listener/LeafBreakListener.java @@ -3,6 +3,7 @@ import com.zetaplugins.timberz.TimberZ; import com.zetaplugins.timberz.service.MaterialTypeChecks; import com.zetaplugins.timberz.service.PlayerStateService; +import com.zetaplugins.timberz.service.magnetic.BreakingHelper; import com.zetaplugins.zetacore.annotations.AutoRegisterListener; import org.bukkit.Material; import org.bukkit.Sound; @@ -42,7 +43,7 @@ public void onBlockBreak(BlockDamageEvent event) { ItemStack tool = player.getInventory().getItemInMainHand(); if (tool.getType() == Material.AIR || !MaterialTypeChecks.isValidAxe(tool, plugin)) return; - brokenBlock.breakNaturally(tool); + BreakingHelper.breakBlock(brokenBlock, player, tool, true); player.playSound(player.getLocation(), Sound.BLOCK_GRASS_BREAK, 1.0f, 1.0f); } diff --git a/src/main/java/com/zetaplugins/timberz/service/TreeFellerService.java b/src/main/java/com/zetaplugins/timberz/service/TreeFellerService.java index 695b79c..89eeea4 100644 --- a/src/main/java/com/zetaplugins/timberz/service/TreeFellerService.java +++ b/src/main/java/com/zetaplugins/timberz/service/TreeFellerService.java @@ -3,6 +3,7 @@ import com.zetaplugins.timberz.TimberZ; import com.zetaplugins.timberz.handler.SaplingReplanter; import com.zetaplugins.timberz.handler.TreeAnimationHandler; +import com.zetaplugins.timberz.service.magnetic.BreakingHelper; import org.bukkit.Material; import org.bukkit.Particle; import org.bukkit.Sound; @@ -49,7 +50,7 @@ public void fellTree(Player player, Block sourceBlock, Set treeBlocks, It // Schedule leaf decay if (!leafBlocks.isEmpty() && plugin.getConfig().getBoolean("instantLeafDecay")) { - scheduleLeafDecay(leafBlocks); + scheduleLeafDecay(leafBlocks, player, tool); } boolean shouldReplant = plugin.getConfig().getBoolean("replant"); @@ -149,7 +150,7 @@ private Set collectLeaves(Set treeBlocks, List leafType) /** * Schedules leaf decay with a natural-looking pattern */ - private void scheduleLeafDecay(Set leafBlocks) { + private void scheduleLeafDecay(Set leafBlocks, Player player, ItemStack tool) { List sortedLeaves = new ArrayList<>(leafBlocks); // Sort leaves from bottom to top for more natural decay @@ -178,8 +179,8 @@ public void run() { Sound.BLOCK_GRASS_BREAK, 0.6f, pitch); - // Break the leaf naturally - leaf.breakNaturally(); + // Break the leaf + BreakingHelper.breakBlock(leaf, player, tool, false); } } }.runTaskLater(plugin, 5 + random.nextInt(40)); // Random delay between 5-45 ticks diff --git a/src/main/java/com/zetaplugins/timberz/service/magnetic/BreakingHelper.java b/src/main/java/com/zetaplugins/timberz/service/magnetic/BreakingHelper.java new file mode 100644 index 0000000..5babc1e --- /dev/null +++ b/src/main/java/com/zetaplugins/timberz/service/magnetic/BreakingHelper.java @@ -0,0 +1,41 @@ +package com.zetaplugins.timberz.service.magnetic; + +import org.apache.commons.lang3.mutable.MutableInt; +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; +import java.util.List; + +public class BreakingHelper { + + public static void breakBlock(Block block, Player player, ItemStack tool, boolean shouldDamage) { + // If magnetic is not present, simply break naturally + if (!Bukkit.getPluginManager().isPluginEnabled("magnetic")) { + block.breakNaturally(tool); + return; + } + + // Create magnetic drop event + ArrayList drops = new ArrayList<>(block.getDrops(tool, player)); + dev.nyon.magnetic.DropEvent dropEvent = new dev.nyon.magnetic.DropEvent(drops, new MutableInt(0), player, block.getLocation()); + dropEvent.callEvent(); + + // Drop the remaining items that were not handled by magnetic + List remainingItems = dropEvent.getItems(); + World world = block.getWorld(); + Location location = block.getLocation(); + remainingItems.forEach(stack -> world.dropItem(location, stack)); + + // Play effects on break + world.playSound(location.toCenterLocation(), block.getBlockSoundGroup().getBreakSound(), 1f, 1f); + + // Actually destroy block + block.setType(Material.AIR); + + // Damage tool if tool is damageable + if (!tool.isEmpty() && tool.getType().getMaxDurability() > 0 && shouldDamage) tool.damage(1, player); + } +} diff --git a/src/main/resources/paper-plugin.yml b/src/main/resources/paper-plugin.yml index e6b8bf2..d83c34f 100644 --- a/src/main/resources/paper-plugin.yml +++ b/src/main/resources/paper-plugin.yml @@ -22,6 +22,10 @@ dependencies: load: BEFORE required: false join-classpath: true + magnetic: + load: BEFORE + required: false + join-classpath: true permissions: timberz.admin: