use of com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent in project WorldGuard by EngineHub.
the class EventAbstractionListener method onPlayerInteract.
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
@Nullable ItemStack item = event.getItem();
Block clicked = event.getClickedBlock();
Block placed;
boolean modifiesWorld;
Cause cause = create(player);
switch(event.getAction()) {
case PHYSICAL:
if (event.useInteractedBlock() != Result.DENY) {
if (clicked.getType() == Material.FARMLAND || clicked.getType() == Material.TURTLE_EGG) {
BreakBlockEvent breakDelagate = new BreakBlockEvent(event, cause, clicked);
breakDelagate.setSilent(true);
breakDelagate.getRelevantFlags().add(Flags.TRAMPLE_BLOCKS);
boolean denied;
if (!(denied = Events.fireToCancel(event, breakDelagate))) {
PlaceBlockEvent placeDelegate = new PlaceBlockEvent(event, cause, clicked.getLocation(), clicked.getType() == Material.FARMLAND ? Material.DIRT : clicked.getType());
placeDelegate.setSilent(true);
placeDelegate.getRelevantFlags().add(Flags.TRAMPLE_BLOCKS);
denied = Events.fireToCancel(event, placeDelegate);
}
if (denied) {
playDenyEffect(player, clicked.getLocation());
}
return;
}
DelegateEvent firedEvent = new UseBlockEvent(event, cause, clicked).setAllowed(hasInteractBypass(clicked));
if (Tag.REDSTONE_ORES.isTagged(clicked.getType())) {
firedEvent.setSilent(true);
}
if (clicked.getType() == Material.BIG_DRIPLEAF) {
firedEvent.getRelevantFlags().add(Flags.USE_DRIPLEAF);
firedEvent.setSilent(true);
}
interactDebounce.debounce(clicked, event.getPlayer(), event, firedEvent);
if (event.useInteractedBlock() == Result.DENY) {
playDenyEffect(player, clicked.getLocation().add(0, 1, 0));
}
}
break;
case RIGHT_CLICK_BLOCK:
if (event.useInteractedBlock() != Result.DENY) {
placed = clicked.getRelative(event.getBlockFace());
// Re-used for dispensers
handleBlockRightClick(event, create(event.getPlayer()), item, clicked, placed);
}
case LEFT_CLICK_BLOCK:
if (event.useInteractedBlock() != Result.DENY) {
placed = clicked.getRelative(event.getBlockFace());
// Only fire events for blocks that are modified when right clicked
final boolean hasItemInteraction = item != null && isItemAppliedToBlock(item, clicked) && event.getAction() == Action.RIGHT_CLICK_BLOCK;
modifiesWorld = hasItemInteraction || isBlockModifiedOnClick(clicked, event.getAction() == Action.RIGHT_CLICK_BLOCK);
if (Events.fireAndTestCancel(new UseBlockEvent(event, cause, clicked).setAllowed(!modifiesWorld))) {
event.setUseInteractedBlock(Result.DENY);
}
// Handle connected blocks (i.e. beds, chests)
for (Block connected : Blocks.getConnected(clicked)) {
if (Events.fireAndTestCancel(new UseBlockEvent(event, create(event.getPlayer()), connected).setAllowed(!modifiesWorld))) {
event.setUseInteractedBlock(Result.DENY);
break;
}
}
if (hasItemInteraction) {
if (Events.fireAndTestCancel(new PlaceBlockEvent(event, cause, clicked.getLocation(), clicked.getType()))) {
event.setUseItemInHand(Result.DENY);
event.setUseInteractedBlock(Result.DENY);
}
}
// Special handling of putting out fires
if (event.getAction() == Action.LEFT_CLICK_BLOCK && Materials.isFire(placed.getType())) {
if (Events.fireAndTestCancel(new BreakBlockEvent(event, create(event.getPlayer()), placed))) {
event.setUseInteractedBlock(Result.DENY);
break;
}
}
if (event.isCancelled()) {
playDenyEffect(event.getPlayer(), clicked.getLocation().add(0.5, 1, 0.5));
}
}
case LEFT_CLICK_AIR:
case RIGHT_CLICK_AIR:
if (event.useItemInHand() != Result.DENY) {
if (item != null && !item.getType().isBlock() && Events.fireAndTestCancel(new UseItemEvent(event, cause, player.getWorld(), item))) {
event.setUseItemInHand(Result.DENY);
}
}
// throw events
if (item != null && getWorldConfig(player.getWorld()).blockUseAtFeet.test(item)) {
if (Events.fireAndTestCancel(new UseBlockEvent(event, cause, player.getLocation().getBlock()))) {
event.setUseInteractedBlock(Result.DENY);
}
}
break;
}
}
use of com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent in project WorldGuard by EngineHub.
the class EventAbstractionListener method onBlockBurn.
@EventHandler(ignoreCancelled = true)
public void onBlockBurn(BlockBurnEvent event) {
Block target = event.getBlock();
Block[] adjacent = { target.getRelative(BlockFace.NORTH), target.getRelative(BlockFace.SOUTH), target.getRelative(BlockFace.WEST), target.getRelative(BlockFace.EAST), target.getRelative(BlockFace.UP), target.getRelative(BlockFace.DOWN) };
int found = 0;
boolean allowed = false;
for (Block source : adjacent) {
if (Materials.isFire(source.getType())) {
found++;
if (Events.fireAndTestCancel(new BreakBlockEvent(event, create(source), target))) {
source.setType(Material.AIR);
} else {
allowed = true;
}
}
}
if (found > 0 && !allowed) {
event.setCancelled(true);
}
}
use of com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent in project WorldGuard by EngineHub.
the class EventAbstractionListener method onPlayerBucketFill.
@EventHandler(ignoreCancelled = true)
public void onPlayerBucketFill(PlayerBucketFillEvent event) {
Player player = event.getPlayer();
Block blockAffected = event.getBlockClicked().getRelative(event.getBlockFace());
boolean allowed = false;
// Milk buckets can't be emptied as of writing
if (event.getItemStack().getType() == Material.MILK_BUCKET) {
allowed = true;
}
ItemStack item = new ItemStack(event.getBucket(), 1);
Events.fireToCancel(event, new BreakBlockEvent(event, create(player), blockAffected).setAllowed(allowed));
Events.fireToCancel(event, new UseItemEvent(event, create(player), player.getWorld(), item).setAllowed(allowed));
if (event.isCancelled()) {
playDenyEffect(event.getPlayer(), blockAffected.getLocation().add(0.5, 0.5, 0.5));
}
}
use of com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent in project WorldGuard by EngineHub.
the class EventAbstractionListener method onBlockPistonRetract.
@EventHandler(ignoreCancelled = true)
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
if (event.isSticky()) {
EventDebounce.Entry entry = pistonRetractDebounce.getIfNotPresent(new BlockPistonRetractKey(event), event);
if (entry != null) {
Block piston = event.getBlock();
Cause cause = create(piston);
BlockFace direction = event.getDirection();
ArrayList<Block> blocks = new ArrayList<>(event.getBlocks());
int originalSize = blocks.size();
Events.fireBulkEventToCancel(event, new BreakBlockEvent(event, cause, event.getBlock().getWorld(), blocks, Material.AIR));
if (originalSize != blocks.size()) {
event.setCancelled(true);
return;
}
for (Block b : blocks) {
Location loc = b.getRelative(direction).getLocation();
Events.fireToCancel(event, new PlaceBlockEvent(event, cause, loc, b.getType()));
}
entry.setCancelled(event.isCancelled());
if (event.isCancelled()) {
playDenyEffect(piston.getLocation().add(0.5, 1, 0.5));
}
}
}
}
use of com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent in project WorldGuard by EngineHub.
the class EventAbstractionListener method onBlockPlace.
@EventHandler(ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
if (event instanceof BlockMultiPlaceEvent)
return;
BlockState previousState = event.getBlockReplacedState();
// Some blocks, like tall grass and fire, get replaced
if (previousState.getType() != Material.AIR && previousState.getType() != event.getBlockReplacedState().getType()) {
Events.fireToCancel(event, new BreakBlockEvent(event, create(event.getPlayer()), previousState.getLocation(), previousState.getType()));
}
if (!event.isCancelled()) {
ItemStack itemStack = new ItemStack(event.getBlockPlaced().getType(), 1);
Events.fireToCancel(event, new UseItemEvent(event, create(event.getPlayer()), event.getPlayer().getWorld(), itemStack));
}
if (!event.isCancelled()) {
Events.fireToCancel(event, new PlaceBlockEvent(event, create(event.getPlayer()), event.getBlock()));
}
if (event.isCancelled()) {
playDenyEffect(event.getPlayer(), event.getBlockPlaced().getLocation().add(0.5, 0.5, 0.5));
}
}
Aggregations