Java Code Examples for org.spongepowered.api.world.Location#getBlockRelative()

The following examples show how to use org.spongepowered.api.world.Location#getBlockRelative() . 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: BlockEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
private boolean checkSurroundings(org.spongepowered.api.event.Event event, Location<World> location, Player player, GDPlayerData playerData, GDClaim targetClaim) {
    if (playerData == null) {
        return true;
    }
    // Don't allow players to break blocks next to land they do not own
    if (!playerData.canIgnoreClaim(targetClaim)) {
        // check surrounding blocks for access
        for (Direction direction : BlockUtil.CARDINAL_SET) {
            Location<World> loc = location.getBlockRelative(direction);
            if (!(loc.getTileEntity().isPresent())) {
                continue;
            }
            final GDClaim claim = this.dataStore.getClaimAt(loc, targetClaim);
            if (!claim.isWilderness() && !targetClaim.equals(claim)) {
                Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, loc, claim, Flags.BLOCK_BREAK, player, loc.getBlock(), player, TrustTypes.BUILDER, true);
                if (result != Tristate.TRUE) {
                    final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.PERMISSION_BUILD_NEAR_CLAIM,
                            ImmutableMap.of(
                            "player", claim.getOwnerName()));
                    GriefDefenderPlugin.sendClaimDenyMessage(claim, player, message);
                    return false;
                }
            }
        }
    }
    return true;
}
 
Example 2
Source File: Visualization.java    From GriefPrevention with MIT License 5 votes vote down vote up
private static Location<World> getVisibleLocation(World world, int x, int y, int z, boolean waterIsTransparent) {
    Location<World> location = world.getLocation(x, y, z);
    Direction direction = (isTransparent(location.getBlock(), waterIsTransparent)) ? Direction.DOWN : Direction.UP;

    while (location.getPosition().getY() >= 1 &&
            location.getPosition().getY() < world.getDimension().getBuildHeight() - 1 &&
            (!isTransparent(location.getBlockRelative(Direction.UP).getBlock(), waterIsTransparent)
                    || isTransparent(location.getBlock(), waterIsTransparent))) {
        location = location.getBlockRelative(direction);
    }

    return location;
}
 
Example 3
Source File: ContainerManager.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
public boolean canOpen(BlockSnapshot b, Player p) {
    if (!RedProtect.get().config.configRoot().private_cat.use || p.hasPermission("redprotect.bypass")) {
        return true;
    }

    List<Direction> dirs = Arrays.asList(Direction.EAST, Direction.NORTH, Direction.SOUTH, Direction.WEST);
    String blocktype = b.getState().getType().getName();
    Location<World> loc = b.getLocation().get();
    List<String> blocks = RedProtect.get().config.configRoot().private_cat.allowed_blocks;
    boolean deny = true;
    if (blocks.stream().anyMatch(blocktype::matches)) {
        for (Direction dir : dirs) {
            Location<World> loc1 = loc.getBlockRelative(dir);
            if (isSign(loc1.createSnapshot())) {
                deny = false;
                if (validateOpenBlock(loc1.createSnapshot(), p)) {
                    return true;
                }
            }

            if (blocks.stream().anyMatch(loc1.getBlockType().getName()::matches) && loc1.getBlockType().equals(b.getState().getType())) {
                for (Direction dir2 : dirs) {
                    Location<World> loc3 = loc1.getBlockRelative(dir2);
                    if (isSign(loc3.createSnapshot())) {
                        deny = false;
                        if (validateOpenBlock(loc3.createSnapshot(), p)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return deny;
}
 
Example 4
Source File: BlockEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockNotify(NotifyNeighborBlockEvent event) {
    LocatableBlock locatableBlock = event.getCause().first(LocatableBlock.class).orElse(null);
    TileEntity tileEntity = event.getCause().first(TileEntity.class).orElse(null);
    Location<World> sourceLocation = locatableBlock != null ? locatableBlock.getLocation() : tileEntity != null ? tileEntity.getLocation() : null;
    GDClaim sourceClaim = null;
    GDPlayerData playerData = null;
    if (sourceLocation != null) {
        if (GriefDefenderPlugin.isSourceIdBlacklisted("block-notify", event.getSource(), sourceLocation.getExtent().getProperties())) {
            return;
        }
    }

    final User user = CauseContextHelper.getEventUser(event);
    if (user == null) {
        return;
    }
    if (sourceLocation == null) {
        Player player = event.getCause().first(Player.class).orElse(null);
        if (player == null) {
            return;
        }

        sourceLocation = player.getLocation();
        playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
        sourceClaim = this.dataStore.getClaimAtPlayer(playerData, player.getLocation());
    } else {
        playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(sourceLocation.getExtent(), user.getUniqueId());
        sourceClaim = this.dataStore.getClaimAt(sourceLocation);
    }

    if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(sourceLocation.getExtent().getUniqueId())) {
        return;
    }

    GDTimings.BLOCK_NOTIFY_EVENT.startTimingIfSync();
    Iterator<Direction> iterator = event.getNeighbors().keySet().iterator();
    GDClaim targetClaim = null;
    while (iterator.hasNext()) {
        Direction direction = iterator.next();
        Location<World> location = sourceLocation.getBlockRelative(direction);
        Vector3i pos = location.getBlockPosition();
        targetClaim = this.dataStore.getClaimAt(location, targetClaim);
        if (sourceClaim.isWilderness() && targetClaim.isWilderness()) {
            if (playerData != null) {
                playerData.eventResultCache = new EventResultCache(targetClaim, "block-notify", Tristate.TRUE);
            }
            continue;
        } else if (!sourceClaim.isWilderness() && targetClaim.isWilderness()) {
            if (playerData != null) {
                playerData.eventResultCache = new EventResultCache(targetClaim, "block-notify", Tristate.TRUE);
            }
            continue;
        } else if (sourceClaim.getUniqueId().equals(targetClaim.getUniqueId())) {
            if (playerData != null) {
                playerData.eventResultCache = new EventResultCache(targetClaim, "block-notify", Tristate.TRUE);
            }
            continue;
        } else {
            if (playerData.eventResultCache != null && playerData.eventResultCache.checkEventResultCache(targetClaim, "block-notify") == Tristate.TRUE) {
                continue;
            }
            // Needed to handle levers notifying doors to open etc.
            if (targetClaim.isUserTrusted(user, TrustTypes.ACCESSOR)) {
                if (playerData != null) {
                    playerData.eventResultCache = new EventResultCache(targetClaim, "block-notify", Tristate.TRUE, TrustTypes.ACCESSOR.getName().toLowerCase());
                }
                continue;
            }
        }

        // no claim crossing unless trusted
        iterator.remove();
    }
    GDTimings.BLOCK_NOTIFY_EVENT.stopTimingIfSync();
}
 
Example 5
Source File: ContainerManager.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
public boolean canBreak(Player p, BlockSnapshot b) {
    if (!RedProtect.get().config.configRoot().private_cat.use) {
        return true;
    }

    Region reg = RedProtect.get().rm.getTopRegion(b.getLocation().get(), this.getClass().getName());
    if (reg == null && !RedProtect.get().config.configRoot().private_cat.allow_outside) {
        return true;
    }

    List<Direction> dirs = Arrays.asList(Direction.EAST, Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.UP, Direction.DOWN);
    String blocktype = b.getState().getType().getName();
    Location<World> loc = b.getLocation().get();
    List<String> blocks = RedProtect.get().config.configRoot().private_cat.allowed_blocks;

    boolean deny = true;

    if (isSign(loc.createSnapshot()) && validatePrivateSign(b)) {
        deny = false;
        if (validateBreakSign(loc.createSnapshot(), p)) {
            return true;
        }
    }

    if (blocks.stream().anyMatch(blocktype::matches)) {
        for (Direction dir : dirs) {
            Location<World> loc1 = loc.getBlockRelative(dir);
            if (isSign(loc1.createSnapshot())) {
                deny = false;
                if (validateBreakSign(loc1.createSnapshot(), p)) {
                    return true;
                }
            }

            if (blocks.stream().anyMatch(loc1.getBlockType().getName()::matches) && loc1.getBlockType().equals(b.getState().getType())) {
                for (Direction dir2 : dirs) {
                    Location<World> loc3 = loc1.getBlockRelative(dir2);
                    if (isSign(loc3.createSnapshot())) {
                        deny = false;
                        if (validateBreakSign(loc3.createSnapshot(), p)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return deny;
}