Enables extended protection for explosion and piston events

This commit is contained in:
Kristian Knarvik 2022-01-22 16:35:30 +01:00
parent 69d182ec10
commit 0283c917e7

View File

@ -16,6 +16,8 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.ArrayList;
@ -36,14 +38,24 @@ public class BlockListener implements Listener {
}
}
@EventHandler
public void onPistonExtend(BlockPistonExtendEvent event) {
preventDestruction(event.getBlocks(), event);
}
@EventHandler
public void onPistonRetract(BlockPistonRetractEvent event) {
preventDestruction(event.getBlocks(), event);
}
@EventHandler
public void onBlockExplode(BlockExplodeEvent event) {
preventExplosion(event.blockList(), event);
preventDestruction(event.blockList(), event);
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
preventExplosion(event.blockList(), event);
preventDestruction(event.blockList(), event);
}
@EventHandler
@ -67,35 +79,15 @@ public class BlockListener implements Listener {
}
}
/**
* Prevents explosions from destroying permission signs
*
* @param blocks <p>The blocks affected by the explosion</p>
* @param event <p>The explosion event to cancel if a permission sign is found</p>
*/
private void preventExplosion(List<Block> blocks, Cancellable event) {
for (Block block : blocks) {
if (!Tag.SIGNS.isTagged(block.getBlockData().getMaterial())) {
continue;
}
Sign sign = (Sign) block.getState();
boolean registered = SignManager.getSign(sign.getLocation()) != null;
if (registered) {
event.setCancelled(true);
break;
}
}
}
/**
* Protects signs on falling blocks in the given direction
*
* @param event <p>The triggered block break event</p>
* @param event <p>The triggered event</p>
* @param block <p>The broken block</p>
* @param player <p>The player breaking the block</p>
* @param direction <p>The direction to check for affected blocks</p>
*/
private void protectSignsInDirection(BlockBreakEvent event, Block block, Player player, BlockFace direction) {
private void protectSignsInDirection(Cancellable event, Block block, Player player, BlockFace direction) {
Block directionBlock = block.getRelative(direction);
while ((direction == BlockFace.DOWN && directionBlock.getBlockData().getMaterial() ==
Material.POINTED_DRIPSTONE) || (direction == BlockFace.UP &&
@ -108,14 +100,37 @@ public class BlockListener implements Listener {
}
}
/**
* Prevents an event from destroying permission signs
*
* @param blocks <p>The blocks affected by the event</p>
* @param event <p>The event to cancel if a permission sign is found</p>
*/
private void preventDestruction(List<Block> blocks, Cancellable event) {
for (Block block : blocks) {
protectBlockIfPermissionSign(event, block, null);
if (event.isCancelled()) {
return;
}
if (extensiveSignProtectionEnabled()) {
protectSignsInDirection(event, block, null, BlockFace.UP);
if (event.isCancelled()) {
return;
}
protectSignsInDirection(event, block, null, BlockFace.DOWN);
}
}
}
/**
* Protects the given sign
*
* @param event <p>The triggered block break event</p>
* @param event <p>The triggered event</p>
* @param block <p>The block to check if it's a sign</p>
* @param player <p>The player breaking the block</p>
*/
private void protectBlockIfPermissionSign(BlockBreakEvent event, Block block, Player player) {
private void protectBlockIfPermissionSign(Cancellable event, Block block, Player player) {
//Protect the permission sign itself
Material material = block.getBlockData().getMaterial();
if (Tag.SIGNS.isTagged(material)) {
@ -131,11 +146,11 @@ public class BlockListener implements Listener {
/**
* Looks for permission signs adjacent to the current block
*
* @param event <p>The triggered block break event</p>
* @param event <p>The triggered event</p>
* @param block <p>The block to check if it's a sign</p>
* @param player <p>The player breaking the block</p>
*/
private void lookForAdjacentPermissionSigns(BlockBreakEvent event, Block block, Player player) {
private void lookForAdjacentPermissionSigns(Cancellable event, Block block, Player player) {
Block aboveBlock = block.getRelative(BlockFace.UP);
if (Tag.STANDING_SIGNS.isTagged(aboveBlock.getBlockData().getMaterial())) {
checkIfBlockIsPermissionSign(aboveBlock, player, event);
@ -162,14 +177,18 @@ public class BlockListener implements Listener {
*
* @param block <p>The block to check</p>
* @param player <p>The player that caused the event</p>
* @param event <p>The triggered block break event</p>
* @param event <p>The triggered event</p>
*/
private void checkIfBlockIsPermissionSign(Block block, Player player, BlockBreakEvent event) {
private void checkIfBlockIsPermissionSign(Block block, Player player, Cancellable event) {
Sign sign = (Sign) block.getState();
boolean registered = SignManager.getSign(sign.getLocation()) != null;
if (!registered) {
return;
}
if (player == null) {
event.setCancelled(true);
return;
}
if (!player.hasPermission("permissionsigns.admin.create")) {
event.setCancelled(true);
player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY));