1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Changed it into a single event instead of calling an event per block

This commit is contained in:
GallowsDove 2021-03-03 16:45:11 +01:00
parent edf15fb40a
commit fe2492f006
No known key found for this signature in database
GPG Key ID: 36F4DFE2A475ADDF
2 changed files with 20 additions and 18 deletions

View File

@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
/** /**
* This {@link Event} is called when a {@link Block} is destroyed by an {@link ExplosiveTool}. * This {@link Event} is called when a {@link Block} is destroyed by an {@link ExplosiveTool}.
@ -23,13 +24,13 @@ public class ExplosiveToolBreakBlockEvent extends PlayerEvent implements Cancell
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private final ItemStack tool; private final ItemStack tool;
private final Block block; private final ArrayList<Block> blocks;
private boolean cancelled; private boolean cancelled;
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public ExplosiveToolBreakBlockEvent(Player player, Block block, ItemStack item) { public ExplosiveToolBreakBlockEvent(Player player, ArrayList<Block> blocks, ItemStack item) {
super(player); super(player);
this.block = block; this.blocks = blocks;
this.tool = item; this.tool = item;
} }
@ -43,12 +44,12 @@ public class ExplosiveToolBreakBlockEvent extends PlayerEvent implements Cancell
} }
/** /**
* Gets the {@link Block} destroyed in the event * Gets the {@link Block} ArrayList of blocks destroyed in the event
* *
*/ */
@Nonnull @Nonnull
public Block getBlock() { public ArrayList<Block> getBlocks() {
return this.block; return this.blocks;
} }
@Override @Override

View File

@ -70,6 +70,8 @@ public class ExplosiveTool extends SimpleSlimefunItem<ToolUseHandler> implements
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
private void breakBlocks(Player p, ItemStack item, Block b, List<Block> blocks, List<ItemStack> drops) { private void breakBlocks(Player p, ItemStack item, Block b, List<Block> blocks, List<ItemStack> drops) {
ArrayList<Block> blocksToDestroy = new ArrayList<>();
if (callExplosionEvent.getValue().booleanValue()) { if (callExplosionEvent.getValue().booleanValue()) {
BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(b, blocks, 0); BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(b, blocks, 0);
Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent); Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent);
@ -77,27 +79,26 @@ public class ExplosiveTool extends SimpleSlimefunItem<ToolUseHandler> implements
if (!blockExplodeEvent.isCancelled()) { if (!blockExplodeEvent.isCancelled()) {
for (Block block : blockExplodeEvent.blockList()) { for (Block block : blockExplodeEvent.blockList()) {
if (canBreak(p, block)) { if (canBreak(p, block)) {
ExplosiveToolBreakBlockEvent event = new ExplosiveToolBreakBlockEvent(p, block, item); blocksToDestroy.add(block);
Bukkit.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
breakBlock(p, item, block, drops);
}
} }
} }
} }
} else { } else {
for (Block block : blocks) { for (Block block : blocks) {
if (canBreak(p, block)) { if (canBreak(p, block)) {
ExplosiveToolBreakBlockEvent event = new ExplosiveToolBreakBlockEvent(p, block, item); blocksToDestroy.add(block);
Bukkit.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
breakBlock(p, item, block, drops);
}
} }
} }
} }
ExplosiveToolBreakBlockEvent event = new ExplosiveToolBreakBlockEvent(p, blocksToDestroy, item);
Bukkit.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()){
for (Block block : blocksToDestroy) {
breakBlock(p, item, block, drops);
}
}
} }
private List<Block> findBlocks(Block b) { private List<Block> findBlocks(Block b) {