Fixes several bugs in sign protection

This commit is contained in:
Kristian Knarvik 2022-01-22 14:37:52 +01:00
parent 6705d2e4b1
commit 9188fd1b96
2 changed files with 31 additions and 22 deletions

View File

@ -8,6 +8,7 @@ import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -15,9 +16,7 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static net.knarcraft.permissionsigns.PermissionSigns.extensiveSignProtectionEnabled;
@ -64,16 +63,16 @@ public class BlockListener implements Listener {
* @param direction <p>The direction to check for affected blocks</p>
*/
private void protectSignsInDirection(BlockBreakEvent event, Block block, Player player, BlockFace direction) {
Block directionBlock = block;
do {
directionBlock = directionBlock.getRelative(direction);
Block directionBlock = block.getRelative(direction);
while ((direction == BlockFace.DOWN && directionBlock.getBlockData().getMaterial() ==
Material.POINTED_DRIPSTONE) || (direction == BlockFace.UP &&
isAffectedByGravity(directionBlock.getBlockData().getMaterial()))) {
protectBlockIfPermissionSign(event, directionBlock, player);
if (event.isCancelled()) {
return;
}
} while ((direction == BlockFace.DOWN && directionBlock.getBlockData().getMaterial() ==
Material.POINTED_DRIPSTONE) || (direction == BlockFace.UP &&
isAffectedByGravity(directionBlock.getBlockData().getMaterial())));
directionBlock = directionBlock.getRelative(direction);
}
}
/**
@ -86,28 +85,38 @@ public class BlockListener implements Listener {
private void protectBlockIfPermissionSign(BlockBreakEvent event, Block block, Player player) {
//Protect the permission sign itself
Material material = block.getBlockData().getMaterial();
if (Tag.SIGNS.isTagged(material) || Tag.WALL_SIGNS.isTagged(material)) {
if (Tag.SIGNS.isTagged(material)) {
checkIfBlockIsPermissionSign(block, player, event);
if (event.isCancelled()) {
return;
}
}
Map<Block, Tag<Material>> blocksToCheck = new HashMap<>();
lookForAdjacentPermissionSigns(event, block, player);
}
//Protect any block with a permission sign on it
blocksToCheck.put(block.getRelative(BlockFace.UP), Tag.SIGNS);
//Protect any permission signs attached to the block
for (BlockFace blockFace : getRelevantBlockFaces()) {
if (!(Tag.WALL_SIGNS.isTagged(block.getBlockData().getMaterial()))) {
blocksToCheck.put(block.getRelative(blockFace), Tag.WALL_SIGNS);
/**
* Looks for permission signs adjacent to the current block
*
* @param event <p>The triggered block break 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) {
Block aboveBlock = block.getRelative(BlockFace.UP);
if (Tag.STANDING_SIGNS.isTagged(aboveBlock.getBlockData().getMaterial())) {
checkIfBlockIsPermissionSign(aboveBlock, player, event);
if (event.isCancelled()) {
return;
}
}
for (Block blockToCheck : blocksToCheck.keySet()) {
if (blocksToCheck.get(blockToCheck).isTagged(blockToCheck.getBlockData().getMaterial())) {
checkIfBlockIsPermissionSign(blockToCheck, player, event);
//Check for any wall permission signs that may be broken by breaking the block
for (BlockFace blockFace : getRelevantBlockFaces()) {
Block adjacentBlock = block.getRelative(blockFace);
if (Tag.WALL_SIGNS.isTagged(adjacentBlock.getBlockData().getMaterial()) &&
blockFace == ((WallSign) adjacentBlock.getBlockData()).getFacing()) {
checkIfBlockIsPermissionSign(adjacentBlock, player, event);
if (event.isCancelled()) {
return;
}
@ -165,7 +174,7 @@ public class BlockListener implements Listener {
private boolean isAffectedByGravity(Material material) {
//TODO: Find a better way of deciding which blocks are affected by gravity
return Tag.SAND.isTagged(material) || Tag.ANVIL.isTagged(material) || material == Material.POINTED_DRIPSTONE ||
Tag.SIGNS.isTagged(material) || material == Material.DRAGON_EGG || material == Material.GRAVEL ||
Tag.STANDING_SIGNS.isTagged(material) || material == Material.DRAGON_EGG || material == Material.GRAVEL ||
material == Material.BLACK_CONCRETE_POWDER || material == Material.BLUE_CONCRETE_POWDER ||
material == Material.BROWN_CONCRETE_POWDER || material == Material.CYAN_CONCRETE_POWDER ||
material == Material.LIME_CONCRETE_POWDER || material == Material.GRAY_CONCRETE_POWDER ||

View File

@ -45,7 +45,7 @@ public class SignListener implements Listener {
}
Material material = block.getBlockData().getMaterial();
if (!Tag.SIGNS.isTagged(material) && !Tag.WALL_SIGNS.isTagged(material)) {
if (!Tag.SIGNS.isTagged(material)) {
return;
}