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.ParametersAreNonnullByDefault;
import java.util.ArrayList;
/**
* 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 final ItemStack tool;
private final Block block;
private final ArrayList<Block> blocks;
private boolean cancelled;
@ParametersAreNonnullByDefault
public ExplosiveToolBreakBlockEvent(Player player, Block block, ItemStack item) {
public ExplosiveToolBreakBlockEvent(Player player, ArrayList<Block> blocks, ItemStack item) {
super(player);
this.block = block;
this.blocks = blocks;
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
public Block getBlock() {
return this.block;
public ArrayList<Block> getBlocks() {
return this.blocks;
}
@Override

View File

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