Skip to content

Commit

Permalink
Fixed cancelling the BlockUnstackEvent not actually cancelling the or…
Browse files Browse the repository at this point in the history
…iginal event (#1653)
  • Loading branch information
OmerBenGera committed Mar 31, 2023
1 parent aa3bb71 commit 836677d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Expand Up @@ -34,7 +34,10 @@ public static void register(SuperiorSkyblockPlugin plugin) {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMinionBreak(MinerBlockBreakEvent e) {
Log.debug(Debug.BLOCK_BREAK, e.getBlock().getType());
if (stackedBlocksListener.get().tryUnstack(null, e.getBlock())) {

StackedBlocksListener.UnstackResult unstackResult = stackedBlocksListener.get().tryUnstack(null, e.getBlock());

if (unstackResult.shouldCancelOriginalEvent()) {
e.setCancelled(true);
} else {
blockChangesListener.get().onBlockBreak(KeyImpl.of(e.getBlock()), e.getBlock().getLocation(),
Expand Down
Expand Up @@ -108,7 +108,10 @@ private static class AndroidMineListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onAndroidMiner(AndroidMineEvent e) {
Log.debug(Debug.BLOCK_BREAK, e.getBlock().getLocation(), e.getBlock().getType());
if (stackedBlocksListener.get().tryUnstack(null, e.getBlock())) {

StackedBlocksListener.UnstackResult unstackResult = stackedBlocksListener.get().tryUnstack(null, e.getBlock());

if (unstackResult.shouldCancelOriginalEvent()) {
e.setCancelled(true);
} else {
blockChangesListener.get().onBlockBreak(KeyImpl.of(e.getBlock()), e.getBlock().getLocation(),
Expand Down
Expand Up @@ -123,7 +123,8 @@ public void onBlockStack(BlockPlaceEvent e) {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockUnstack(BlockBreakEvent e) {
if (tryUnstack(e.getPlayer(), e.getBlock()))
UnstackResult unstackResult = tryUnstack(e.getPlayer(), e.getBlock());
if (unstackResult.shouldCancelOriginalEvent())
e.setCancelled(true);
}

Expand All @@ -142,14 +143,15 @@ public void onBlockUnstack(PlayerInteractEvent e) {
StackedBlocksDepositMenu depositMenu = new StackedBlocksDepositMenu(e.getClickedBlock().getLocation());
e.getPlayer().openInventory(depositMenu.getInventory());
} else if (protectionListener.get().preventBlockBreak(e.getClickedBlock(), e.getPlayer(), ProtectionListener.Flag.SEND_MESSAGES) ||
tryUnstack(e.getPlayer(), e.getClickedBlock())) {
tryUnstack(e.getPlayer(), e.getClickedBlock()).shouldCancelOriginalEvent()) {
e.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockUnstack(EntityChangeBlockEvent e) {
if (tryUnstack(null, e.getBlock()))
UnstackResult unstackResult = tryUnstack(null, e.getBlock());
if (unstackResult.shouldCancelOriginalEvent())
e.setCancelled(true);
}

Expand Down Expand Up @@ -323,11 +325,11 @@ public boolean tryStack(Player player, int amount, Location stackedBlock, Consum
return true;
}

public boolean tryUnstack(@Nullable Player player, Block block) {
public UnstackResult tryUnstack(@Nullable Player player, Block block) {
int blockAmount = plugin.getStackedBlocks().getStackedBlockAmount(block);

if (blockAmount <= 1)
return false;
return UnstackResult.NOT_STACKED;

// When sneaking, you'll break 64 from the stack. Otherwise, 1.
int amount = player == null || !player.isSneaking() ? 1 : 64;
Expand All @@ -336,7 +338,7 @@ public boolean tryUnstack(@Nullable Player player, Block block) {
amount = Math.min(amount, blockAmount);

if (!plugin.getEventsBus().callBlockUnstackEvent(block, player, blockAmount, blockAmount - amount))
return false;
return UnstackResult.CANCELLED;

Island island = plugin.getGrid().getIslandAt(block.getLocation());

Expand Down Expand Up @@ -381,7 +383,7 @@ public boolean tryUnstack(@Nullable Player player, Block block) {
block.getWorld().dropItemNaturally(block.getLocation(), blockItem);
}

return true;
return UnstackResult.SUCCESS;
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
Expand Down Expand Up @@ -481,4 +483,16 @@ public void onSpongeAbsorb(org.bukkit.event.block.SpongeAbsorbEvent e) {

}

public enum UnstackResult {

NOT_STACKED,
CANCELLED,
SUCCESS;

public boolean shouldCancelOriginalEvent() {
return this != NOT_STACKED;
}

}

}

0 comments on commit 836677d

Please sign in to comment.