Skip to content

Commit

Permalink
Merge pull request Slimefun#3924 from iTwins/fix/sensitive_blocks_exp…
Browse files Browse the repository at this point in the history
…losive_tools
  • Loading branch information
Sfiguz7 authored Aug 26, 2023
2 parents 27ed35e + 4398098 commit 68edac5
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.inventory.meta.ItemMeta;

import io.github.bakedlibs.dough.protection.Interaction;
import io.github.thebusybiscuit.slimefun4.api.events.ExplosiveToolBreakBlocksEvent;
import io.github.thebusybiscuit.slimefun4.api.events.SlimefunBlockBreakEvent;
import io.github.thebusybiscuit.slimefun4.api.events.SlimefunBlockPlaceEvent;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void onBlockPlaceExisting(BlockPlaceEvent e) {
public void onBlockPlace(BlockPlaceEvent e) {
ItemStack item = e.getItemInHand();
SlimefunItem sfItem = SlimefunItem.getByItem(item);

// TODO: Protection manager is null in testing environment.
if (!Slimefun.instance().isUnitTest()) {
Slimefun.getProtectionManager().logAction(e.getPlayer(), e.getBlock(), Interaction.PLACE_BLOCK);
Expand Down Expand Up @@ -141,7 +142,7 @@ public void onBlockBreak(BlockBreakEvent e) {
}

if (!e.isCancelled()) {
checkForSensitiveBlockAbove(e, item);
checkForSensitiveBlockAbove(e.getPlayer(), e.getBlock(), item);

int fortune = getBonusDropsWithFortune(item, e.getBlock());
List<ItemStack> drops = new ArrayList<>();
Expand All @@ -155,6 +156,13 @@ public void onBlockBreak(BlockBreakEvent e) {
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onExplosiveToolBlockBreak(ExplosiveToolBreakBlocksEvent e) {
for (Block block : e.getAdditionalBlocks()) {
checkForSensitiveBlockAbove(e.getPlayer(), block, e.getItemInHand());
}
}

@ParametersAreNonnullByDefault
private void callToolHandler(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops) {
SlimefunItem tool = SlimefunItem.getByItem(item);
Expand Down Expand Up @@ -219,14 +227,16 @@ private void dropItems(BlockBreakEvent e, List<ItemStack> drops) {
* Sensitive {@link Block Blocks} are pressure plates or saplings, which should be broken
* when the block beneath is broken as well.
*
* @param p
* @param player
* The {@link Player} who broke this {@link Block}
* @param b
* @param block
* The {@link Block} that was broken
* @param item
* The {@link ItemStack} that was used to break the {@link Block}
*/
@ParametersAreNonnullByDefault
private void checkForSensitiveBlockAbove(BlockBreakEvent e, ItemStack item) {
Block blockAbove = e.getBlock().getRelative(BlockFace.UP);
private void checkForSensitiveBlockAbove(Player player, Block block, ItemStack item) {
Block blockAbove = block.getRelative(BlockFace.UP);

if (SlimefunTag.SENSITIVE_MATERIALS.isTagged(blockAbove.getType())) {
SlimefunItem sfItem = BlockStorage.check(blockAbove);
Expand All @@ -236,9 +246,8 @@ private void checkForSensitiveBlockAbove(BlockBreakEvent e, ItemStack item) {
* We create a dummy here to pass onto the BlockBreakHandler.
* This will set the correct block context.
*/
BlockBreakEvent dummyEvent = new BlockBreakEvent(blockAbove, e.getPlayer());
List<ItemStack> drops = new ArrayList<>();
drops.addAll(sfItem.getDrops(e.getPlayer()));
BlockBreakEvent dummyEvent = new BlockBreakEvent(blockAbove, player);
List<ItemStack> drops = new ArrayList<>(sfItem.getDrops(player));

sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
blockAbove.setType(Material.AIR);
Expand Down

0 comments on commit 68edac5

Please sign in to comment.