Properly re-write BlockPlace listener

This commit is contained in:
Acrobot 2013-03-24 20:58:09 +01:00
parent 2448af7bcb
commit 30a514e783

View File

@ -8,8 +8,8 @@ import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -19,22 +19,16 @@ import org.bukkit.event.block.BlockPlaceEvent;
* @author Acrobot
*/
public class BlockPlace implements Listener {
private static BlockFace[] SEARCH_DIRECTIONS = {BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.UP, BlockFace.DOWN};
@EventHandler(ignoreCancelled = true)
public static void onBlockPlace(BlockPlaceEvent event) {
Block block = event.getBlockAgainst();
if (BlockUtil.isSign(block) && ChestShopSign.isValid((Sign) block.getState())) {
event.setCancelled(true);
return;
}
public static void onChestPlace(BlockPlaceEvent event) {
Block placed = event.getBlockPlaced();
if (!BlockUtil.isChest(placed) && placed.getType() != Material.DROPPER && placed.getType() != Material.HOPPER) {
if (!BlockUtil.isChest(placed)) {
return;
}
Player player = event.getPlayer();
if (Permission.has(player, Permission.ADMIN)) {
@ -46,20 +40,46 @@ public class BlockPlace implements Listener {
event.setCancelled(true);
}
if (!BlockUtil.isChest(placed)) {
return;
}
Chest neighbor = uBlock.findNeighbor(placed);
if (neighbor == null) {
return;
}
if (!Security.canAccess(event.getPlayer(), neighbor.getBlock())) {
if (neighbor != null && !Security.canAccess(event.getPlayer(), neighbor.getBlock())) {
event.getPlayer().sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
event.setCancelled(true);
}
}
@EventHandler(ignoreCancelled = true)
public static void onPlaceAgainstSign(BlockPlaceEvent event) {
Block against = event.getBlockAgainst();
if (!ChestShopSign.isValid(against)) {
return;
}
event.setCancelled(true);
}
@EventHandler(ignoreCancelled = true)
public static void onHopperDropperPlace(BlockPlaceEvent event) {
Block placed = event.getBlockPlaced();
if (placed.getType() != Material.HOPPER && placed.getType() != Material.DROPPER) {
return;
}
for (BlockFace face : SEARCH_DIRECTIONS) {
Block relative = placed.getRelative(face);
if (!BlockUtil.isChest(relative)) {
continue;
}
if (!Security.canAccess(event.getPlayer(), relative)) {
event.getPlayer().sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
event.setCancelled(true);
return;
}
}
}
}