Skip to content

Commit

Permalink
fix: actual fix SlimefunGuguProject#928 pick from Slimefun4#4062
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Aug 8, 2024
1 parent 38953fe commit 6e1a7cf
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ public void onBlockBreak(BlockBreakEvent e) {
var heldItem = e.getPlayer().getInventory().getItemInMainHand();
var block = e.getBlock();
var blockData = StorageCacheUtils.getBlock(block.getLocation());
var sfItem = SlimefunItem.getById(blockData.getSfId());

// If there is a Slimefun Block here, call our BreakEvent and, if cancelled, cancel this event
// and return
if (blockData != null) {
var sfItem = SlimefunItem.getById(blockData.getSfId());
SlimefunBlockBreakEvent breakEvent =
new SlimefunBlockBreakEvent(e.getPlayer(), heldItem, e.getBlock(), sfItem);
Bukkit.getPluginManager().callEvent(breakEvent);
Expand All @@ -204,7 +204,7 @@ public void onBlockBreak(BlockBreakEvent e) {
checkForSensitiveBlockAbove(e.getPlayer(), e.getBlock(), heldItem);

if (blockData == null || blockData.isPendingRemove()) {
dropItems(e, drops);
dropItems(e, heldItem, block, sfItem, drops);
return;
}

Expand All @@ -223,7 +223,7 @@ public void onBlockBreak(BlockBreakEvent e) {
return;
}
e.setDropItems(true);
dropItems(e, drops);
dropItems(e, heldItem, block, sfItem, drops);
},
true);
return;
Expand All @@ -233,7 +233,7 @@ public void onBlockBreak(BlockBreakEvent e) {
if (e.isCancelled()) {
blockData.setPendingRemove(false);
}
dropItems(e, drops);
dropItems(e, heldItem, block, sfItem, drops);

// Checks for vanilla sensitive blocks everywhere
// checkForSensitiveBlocks(e.getBlock(), 0, e.isDropItems());
Expand Down Expand Up @@ -271,27 +271,32 @@ private void callBlockHandler(BlockBreakEvent e, ItemStack item, List<ItemStack>
}

@ParametersAreNonnullByDefault
private void dropItems(BlockBreakEvent e, List<ItemStack> drops) {
private void dropItems(
BlockBreakEvent e, ItemStack item, Block block, @Nullable SlimefunItem sfBlock, List<ItemStack> drops) {
if (!drops.isEmpty()) {
// TODO: properly support loading inventories within unit tests
if (!Slimefun.instance().isUnitTest()) {
// Notify plugins like CoreProtect
Slimefun.getProtectionManager().logAction(e.getPlayer(), e.getBlock(), Interaction.BREAK_BLOCK);
Slimefun.getProtectionManager().logAction(e.getPlayer(), block, Interaction.BREAK_BLOCK);
}

// Fixes #2560
if (e.isDropItems()) {
// Disable normal block drops
e.setDropItems(false);

// Fixes #4051
if (sfBlock == null) {
block.breakNaturally(item);
}

// The list only contains other drops, not those from the block itself, so we still need to handle those
for (ItemStack drop : drops) {
// Prevent null or air from being dropped
if (drop != null && drop.getType() != Material.AIR) {
if (e.getPlayer().getGameMode() != GameMode.CREATIVE
|| Slimefun.getCfg().getBoolean("options.drop-block-creative")) {
e.getBlock()
.getWorld()
.dropItemNaturally(e.getBlock().getLocation(), drop);
block.getWorld().dropItemNaturally(block.getLocation(), drop);
}
}
}
Expand Down Expand Up @@ -330,7 +335,7 @@ private void checkForSensitiveBlockAbove(Player player, Block block, ItemStack i
sfItem.callItemHandler(
BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
controller.removeBlock(loc);
dropItems(dummyEvent, drops);
dropItems(dummyEvent, item, block, sfItem, drops);
} else {
blockData.setPendingRemove(true);
controller.loadBlockDataAsync(blockData, new IAsyncReadCallback<>() {
Expand All @@ -344,7 +349,7 @@ public void onResult(SlimefunBlockData result) {
sfItem.callItemHandler(
BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
controller.removeBlock(loc);
dropItems(dummyEvent, drops);
dropItems(dummyEvent, item, block, sfItem, drops);
}
});
}
Expand Down

0 comments on commit 6e1a7cf

Please sign in to comment.