Skip to content

Commit

Permalink
fix sensitive blocks attached to sf blocks not dropping (1.19+)
Browse files Browse the repository at this point in the history
  • Loading branch information
iTwins committed Sep 7, 2023
1 parent d683a63 commit 051c519
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -25,6 +26,7 @@
import org.bukkit.inventory.meta.ItemMeta;

import io.github.bakedlibs.dough.protection.Interaction;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.api.events.ExplosiveToolBreakBlocksEvent;
import io.github.thebusybiscuit.slimefun4.api.events.SlimefunBlockBreakEvent;
import io.github.thebusybiscuit.slimefun4.api.events.SlimefunBlockPlaceEvent;
Expand Down Expand Up @@ -53,6 +55,8 @@
*/
public class BlockListener implements Listener {

private static final BlockFace[] CARDINAL_BLOCKFACES = new BlockFace[]{BlockFace.WEST, BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.DOWN, BlockFace.UP};

public BlockListener(@Nonnull Slimefun plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
Expand Down Expand Up @@ -152,6 +156,9 @@ public void onBlockBreak(BlockBreakEvent e) {
}

callBlockHandler(e, item, drops, sfItem);

checkForSensitiveBlocks(e.getBlock(), 0);

dropItems(e, drops);
}
}
Expand Down Expand Up @@ -222,6 +229,52 @@ private void dropItems(BlockBreakEvent e, List<ItemStack> drops) {
}
}

/**
* This method checks recursively for any sensitive blocks
* that are no longer supported due to this block breaking
*
* @param block
* The {@link Block} in question
* @param c
* The amount of times this has been recursively called
*/
@ParametersAreNonnullByDefault
private void checkForSensitiveBlocks(Block block, Integer c) {
if (c >= Bukkit.getServer().getMaxChainedNeighborUpdates()) {
return;
}
for (BlockFace face : CARDINAL_BLOCKFACES) {
block.setType(Material.AIR, false);
if (!isSupported(block.getRelative(face).getBlockData(), block.getRelative(face))) {
Block relative = block.getRelative(face);
for (ItemStack drop : relative.getDrops()) {
block.getWorld().dropItemNaturally(relative.getLocation(), drop);
}
checkForSensitiveBlocks(relative, ++c);
}
}
}

/**
* This method checks if the {@link BlockData} would be
* supported at the given {@link Block}.
*
* @param blockData
* The {@link BlockData} to check
* @param block
* The {@link Block} the {@link BlockData} would be at
* @return
* Whether the {@link BlockData} would be supported at the given {@link Block}
*/
private boolean isSupported(BlockData blockData, Block block) {
if (Slimefun.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_19)) {
return blockData.isSupported(block);
} else {
// TODO: Make 1.16-1.18 version. BlockData::isSupported is 1.19+.
return true;
}
}

/**
* This method checks for a sensitive {@link Block}.
* Sensitive {@link Block Blocks} are pressure plates or saplings, which should be broken
Expand Down

0 comments on commit 051c519

Please sign in to comment.