Java Code Examples for org.spongepowered.api.event.block.InteractBlockEvent#getTargetBlock()

The following examples show how to use org.spongepowered.api.event.block.InteractBlockEvent#getTargetBlock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayerInteractListener.java    From EagleFactions with MIT License 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockInteract(final InteractBlockEvent event, @Root final Player player)
{
    //If AIR or NONE then return
    if (event.getTargetBlock() == BlockSnapshot.NONE || event.getTargetBlock().getState().getType() == BlockTypes.AIR)
        return;

    final Optional<Location<World>> optionalLocation = event.getTargetBlock().getLocation();
    if (!optionalLocation.isPresent())
        return;

    final Location<World> blockLocation = optionalLocation.get();

    final ProtectionResult protectionResult = super.getPlugin().getProtectionManager().canInteractWithBlock(blockLocation, player, true);
    if (!protectionResult.hasAccess())
    {
        event.setCancelled(true);
        return;
    }
    else
    {
        if(event instanceof InteractBlockEvent.Secondary && protectionResult.isEagleFeather())
            removeEagleFeather(player);
    }
}
 
Example 2
Source File: BlockListener.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onInteractBlock(InteractBlockEvent event, @First Player p) {
    BlockSnapshot b = event.getTargetBlock();
    Location<World> l;

    RedProtect.get().logger.debug(LogLevel.PLAYER, "BlockListener - Is InteractBlockEvent event");

    if (!b.getState().getType().equals(BlockTypes.AIR)) {
        l = b.getLocation().get();
        RedProtect.get().logger.debug(LogLevel.PLAYER, "BlockListener - Is InteractBlockEvent event. The block is " + b.getState().getType().getName());
    } else {
        l = p.getLocation();
    }

    Region r = RedProtect.get().rm.getTopRegion(l, this.getClass().getName());
    if (r != null) {
        ItemType itemInHand = RedProtect.get().getVersionHelper().getItemInHand(p);
        if (itemInHand.equals(ItemTypes.ARMOR_STAND) && !r.canBuild(p)) {
            RedProtect.get().lang.sendMessage(p, "blocklistener.region.cantbuild");
            event.setCancelled(true);
        }
    }
}
 
Example 3
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
private void handleFakePlayerInteractBlockPrimary(InteractBlockEvent event, User user, Object source) {
    final BlockSnapshot clickedBlock = event.getTargetBlock();
    final Location<World> location = clickedBlock.getLocation().orElse(null);
    final GDClaim claim = this.dataStore.getClaimAt(location);
    final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.INTERACT_BLOCK_PRIMARY, source, event.getTargetBlock(), user, TrustTypes.BUILDER, true);
    if (result == Tristate.FALSE) {
        event.setCancelled(true);
    }
}
 
Example 4
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
private void handleFakePlayerInteractBlockSecondary(InteractBlockEvent event, User user, Object source) {
    final BlockSnapshot clickedBlock = event.getTargetBlock();
    final Location<World> location = clickedBlock.getLocation().orElse(null);
    final GDClaim claim = this.dataStore.getClaimAt(location);
    final TileEntity tileEntity = clickedBlock.getLocation().get().getTileEntity().orElse(null);
    final TrustType trustType = (tileEntity != null && NMSUtil.getInstance().containsInventory(tileEntity)) ? TrustTypes.CONTAINER : TrustTypes.ACCESSOR;
    final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.INTERACT_BLOCK_SECONDARY, source, event.getTargetBlock(), user, trustType, true);
    if (result == Tristate.FALSE) {
        event.setCancelled(true);
    }
}
 
Example 5
Source File: RequiredInteractListener.java    From Prism with MIT License 4 votes vote down vote up
/**
 * Listens for interactions by Players with active inspection wands.
 * <br>
 * This listener is required and does not track any events.
 *
 * @param event  InteractBlockEvent
 * @param player Player
 */
@Listener(order = Order.EARLY)
public void onInteractBlock(InteractBlockEvent event, @First Player player) {
    // Wand support
    if (!Prism.getInstance().getActiveWands().contains(player.getUniqueId())) {
        return;
    }

    event.setCancelled(true);

    // Ignore OffHand events
    if (event instanceof InteractBlockEvent.Primary.OffHand || event instanceof InteractBlockEvent.Secondary.OffHand) {
        return;
    }

    // Verify target block is valid
    if (event.getTargetBlock() == BlockSnapshot.NONE || !event.getTargetBlock().getLocation().isPresent()) {
        return;
    }

    // Location of block
    Location<World> location = event.getTargetBlock().getLocation().get();

    // Secondary click gets location relative to side clicked
    if (event instanceof InteractBlockEvent.Secondary) {
        location = location.getRelative(event.getTargetSide());
    }

    QuerySession session = new QuerySession(player);
    // session.addFlag(Flag.EXTENDED);
    session.addFlag(Flag.NO_GROUP);
    session.newQuery().addCondition(ConditionGroup.from(location));

    player.sendMessage(Text.of(
            Format.prefix(), TextColors.GOLD,
            "--- Inspecting ", Format.item(location.getBlockType().getId(), true),
            " at ", location.getBlockX(), " ", location.getBlockY(), " ", location.getBlockZ(), " ---"));

    // Pass off to an async lookup helper
    AsyncUtil.lookup(session);
}