Add old piston checks back in for backwards compatibility. (Fixes #91)

These checks are completely unsupported though!
This commit is contained in:
Phoenix616 2017-12-31 01:13:13 +01:00
parent 7c9dd5c3f3
commit d84313716f

View File

@ -77,7 +77,7 @@ public class SignBreak implements Listener {
@EventHandler(ignoreCancelled = true)
public static void onBlockPistonExtend(BlockPistonExtendEvent event) {
for (Block block : event.getBlocks()) {
for (Block block : getExtendBlocks(event)) {
if (!canBlockBeBroken(block, null)) {
event.setCancelled(true);
return;
@ -87,7 +87,7 @@ public class SignBreak implements Listener {
@EventHandler(ignoreCancelled = true)
public static void onBlockPistonRetract(BlockPistonRetractEvent event) {
for (Block block : event.getBlocks()) {
for (Block block : getRetractBlocks(event)) {
if (!canBlockBeBroken(block, null)) {
event.setCancelled(true);
return;
@ -192,4 +192,55 @@ public class SignBreak implements Listener {
return attachedSigns;
}
}
private static List<Block> getRetractBlocks(BlockPistonRetractEvent event) {
try {
return event.getBlocks();
} catch (NoSuchMethodError outdated) { // backwards compatiblity
List<Block> blocks = new ArrayList<>();
Block block = getRetractLocationBlock(event);
if (block != null && !BlockUtil.isSign(block)) {
blocks.add(block);
}
return blocks;
}
}
//Those are fixes for a very old CraftBukkit's piston bug, where piston appears not to be a piston.
private static BlockFace getPistonDirection(Block block) {
return block.getState().getData() instanceof PistonBaseMaterial ? ((Directional) block.getState().getData()).getFacing() : null;
}
private static Block getRetractLocationBlock(BlockPistonRetractEvent event) {
BlockFace pistonDirection = getPistonDirection(event.getBlock());
return pistonDirection != null ? event.getBlock().getRelative((pistonDirection), 2).getLocation().getBlock() : null;
}
private static List<Block> getExtendBlocks(BlockPistonExtendEvent event) {
try {
return event.getBlocks();
} catch (NoSuchMethodError outdated) { // backwards compatiblity
BlockFace pistonDirection = getPistonDirection(event.getBlock());
if (pistonDirection == null) {
return new ArrayList<>();
}
Block piston = event.getBlock();
List<Block> pushedBlocks = new ArrayList<>();
for (int currentBlock = 1; currentBlock < event.getLength() + 1; currentBlock++) {
Block block = piston.getRelative(pistonDirection, currentBlock);
Material blockType = block.getType();
if (blockType == Material.AIR) {
break;
}
pushedBlocks.add(block);
}
return pushedBlocks;
}
}
}