From 051c51915e26de4f01961bfc180c5e37c40cda7d Mon Sep 17 00:00:00 2001 From: iTwins Date: Thu, 7 Sep 2023 05:33:42 +0200 Subject: [PATCH 1/3] fix sensitive blocks attached to sf blocks not dropping (1.19+) --- .../listeners/BlockListener.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java index 30593427e..75a9cdba8 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java @@ -14,6 +14,7 @@ import org.bukkit.Bukkit; 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; @@ -25,6 +26,7 @@ import org.bukkit.inventory.ItemStack; 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; @@ -53,6 +55,8 @@ import me.mrCookieSlime.Slimefun.api.BlockStorage; */ 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); } @@ -152,6 +156,9 @@ public class BlockListener implements Listener { } callBlockHandler(e, item, drops, sfItem); + + checkForSensitiveBlocks(e.getBlock(), 0); + dropItems(e, drops); } } @@ -222,6 +229,52 @@ public class BlockListener implements Listener { } } + /** + * 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 From 9c5114f9c274899923807b946a34f402895cfc45 Mon Sep 17 00:00:00 2001 From: iTwins Date: Thu, 7 Sep 2023 05:41:25 +0200 Subject: [PATCH 2/3] annotation --- .../slimefun4/implementation/listeners/BlockListener.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java index 75a9cdba8..b919ac098 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java @@ -266,6 +266,7 @@ public class BlockListener implements Listener { * @return * Whether the {@link BlockData} would be supported at the given {@link Block} */ + @ParametersAreNonnullByDefault private boolean isSupported(BlockData blockData, Block block) { if (Slimefun.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_19)) { return blockData.isSupported(block); From d4b6c0aac8d510fcc1bab2f19f877ce95c371822 Mon Sep 17 00:00:00 2001 From: iTwins Date: Sat, 9 Sep 2023 16:46:14 +0200 Subject: [PATCH 3/3] renamed c to count --- .../slimefun4/implementation/listeners/BlockListener.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java index b919ac098..46152d988 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java @@ -235,12 +235,12 @@ public class BlockListener implements Listener { * * @param block * The {@link Block} in question - * @param c + * @param count * The amount of times this has been recursively called */ @ParametersAreNonnullByDefault - private void checkForSensitiveBlocks(Block block, Integer c) { - if (c >= Bukkit.getServer().getMaxChainedNeighborUpdates()) { + private void checkForSensitiveBlocks(Block block, Integer count) { + if (count >= Bukkit.getServer().getMaxChainedNeighborUpdates()) { return; } for (BlockFace face : CARDINAL_BLOCKFACES) { @@ -250,7 +250,7 @@ public class BlockListener implements Listener { for (ItemStack drop : relative.getDrops()) { block.getWorld().dropItemNaturally(relative.getLocation(), drop); } - checkForSensitiveBlocks(relative, ++c); + checkForSensitiveBlocks(relative, ++count); } } }