Search in sources :

Example 6 with BlockRayHit

use of org.spongepowered.api.util.blockray.BlockRayHit in project Nucleus by NucleusPowered.

the class LightningCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
    Collection<Living> playerCollection = args.getAll(player);
    // No argument, let's not smite the subject.
    if (playerCollection.isEmpty()) {
        if (!(src instanceof Player)) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.playeronly"));
            return CommandResult.empty();
        }
        Player pl = (Player) src;
        // 100 is a good limit here.
        BlockRay<World> playerBlockRay = BlockRay.from(pl).distanceLimit(100).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).build();
        Optional<BlockRayHit<World>> obh = playerBlockRay.end();
        Location<World> lightningLocation;
        // Smite above, but not on.
        lightningLocation = obh.map(BlockRayHit::getLocation).orElseGet(() -> pl.getLocation().add(0, 3, 0));
        return this.spawnLightning(lightningLocation, src, null);
    }
    int successCount = 0;
    for (Living pl : playerCollection) {
        CommandResult cr = this.spawnLightning(pl.getLocation(), src, pl instanceof Player ? (Player) pl : null);
        successCount += cr.getSuccessCount().orElse(0);
    }
    return CommandResult.builder().successCount(successCount).build();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Living(org.spongepowered.api.entity.living.Living) World(org.spongepowered.api.world.World) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) CommandResult(org.spongepowered.api.command.CommandResult)

Example 7 with BlockRayHit

use of org.spongepowered.api.util.blockray.BlockRayHit in project guardian by ichorpowered.

the class RayUtil method getFirstBlock.

private static Optional<BlockRayHit> getFirstBlock(final Extent extent, final Vector3d from, final Vector3d direction, final Predicate<BlockType> skipFilter, final int distanceLimit) {
    final BlockRay<Extent> blockRay = BlockRay.from(extent, from).direction(direction).distanceLimit(distanceLimit).build();
    Optional<BlockRayHit> blockRayHit = Optional.empty();
    while (blockRay.hasNext()) {
        final BlockRayHit nextBlockRayHit = blockRay.next();
        final BlockType blockType = extent.getBlockType(nextBlockRayHit.getBlockPosition());
        if (!skipFilter.test(blockType)) {
            blockRayHit = Optional.of(nextBlockRayHit);
            break;
        }
    }
    return blockRayHit;
}
Also used : Extent(org.spongepowered.api.world.extent.Extent) BlockType(org.spongepowered.api.block.BlockType) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Example 8 with BlockRayHit

use of org.spongepowered.api.util.blockray.BlockRayHit in project modules-extra by CubeEngine.

the class Writer method editSignInSight.

/**
 * Edits the sign the user is looking at
 *
 * @param user  the user
 * @param line1 the 1st line
 * @param line2 the 2nd line
 * @param line3 the 3rd line
 * @param line4 the 4th line
 *
 * @return false if there is no sign to edit in sight
 */
public boolean editSignInSight(Player user, String line1, String line2, String line3, String line4) {
    Iterator<BlockRayHit<World>> it = BlockRay.from(user).distanceLimit(10).iterator();
    BlockRayHit<World> result = null;
    while (it.hasNext()) {
        BlockRayHit<World> next = it.next();
        BlockType type = next.getLocation().getBlockType();
        if (type == WALL_SIGN || type == STANDING_SIGN) {
            result = next;
            break;
        }
    }
    if (result == null) {
        return false;
    }
    Location<World> block = result.getLocation();
    BlockType type = block.getBlockType();
    if (type != WALL_SIGN && type != STANDING_SIGN) {
        return false;
    }
    SignData signData = block.get(SignData.class).get();
    ListValue<Text> lines = signData.lines();
    lines.set(0, line1 == null ? lines.get(0) : Text.of(line1));
    lines.set(1, line2 == null ? lines.get(1) : Text.of(line2));
    lines.set(2, line3 == null ? lines.get(2) : Text.of(line3));
    lines.set(3, line4 == null ? lines.get(3) : Text.of(line4));
    System.out.println(block.offer(signData).isSuccessful());
    i18n.send(user, POSITIVE, "The sign has been changed!");
    return true;
}
Also used : BlockType(org.spongepowered.api.block.BlockType) SignData(org.spongepowered.api.data.manipulator.mutable.tileentity.SignData) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Example 9 with BlockRayHit

use of org.spongepowered.api.util.blockray.BlockRayHit in project modules-extra by CubeEngine.

the class PlayerCommands method explosion.

@Command(desc = "Creates an explosion")
public void explosion(CommandSource context, @Optional Integer damage, @Named({ "player", "p" }) Player player, @Flag boolean unsafe, @Flag boolean fire, @Flag boolean blockDamage, @Flag boolean playerDamage) {
    damage = damage == null ? 1 : damage;
    if (damage > this.module.getConfig().command.explosion.power) {
        i18n.send(context, NEGATIVE, "The power of the explosion shouldn't be greater than {integer}", this.module.getConfig().command.explosion.power);
        return;
    }
    Location<World> loc;
    if (player != null) {
        if (!context.equals(player)) {
            if (!context.hasPermission(module.perms().COMMAND_EXPLOSION_OTHER.getId())) {
                i18n.send(context, NEGATIVE, "You are not allowed to specify a player.");
                return;
            }
        }
        loc = player.getLocation();
    } else {
        if (!(context instanceof Player)) {
            i18n.send(context, NEGATIVE, "This command can only be used by a player!");
            return;
        }
        java.util.Optional<BlockRayHit<World>> end = BlockRay.from(((Player) context)).distanceLimit(module.getConfig().command.explosion.distance).stopFilter(BlockRay.onlyAirFilter()).build().end();
        if (end.isPresent()) {
            loc = end.get().getLocation();
        } else {
            throw new IllegalStateException();
        }
    }
    if (!context.hasPermission(module.perms().COMMAND_EXPLOSION_BLOCK_DAMAGE.getId()) && (blockDamage || unsafe)) {
        i18n.send(context, NEGATIVE, "You are not allowed to break blocks");
        return;
    }
    if (!context.hasPermission(module.perms().COMMAND_EXPLOSION_FIRE.getId()) && (fire || unsafe)) {
        i18n.send(context, NEGATIVE, "You are not allowed to set fireticks");
        return;
    }
    if (!context.hasPermission(module.perms().COMMAND_EXPLOSION_PLAYER_DAMAGE.getId()) && (playerDamage || unsafe)) {
        i18n.send(context, NEGATIVE, "You are not allowed to damage another player");
        return;
    }
    Explosion explosion = Explosion.builder().location(loc).canCauseFire(fire || unsafe).shouldDamageEntities(playerDamage || unsafe).shouldBreakBlocks(blockDamage || unsafe).build();
    Sponge.getCauseStackManager().pushCause(context);
    loc.getExtent().triggerExplosion(explosion);
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Explosion(org.spongepowered.api.world.explosion.Explosion) World(org.spongepowered.api.world.World) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) Command(org.cubeengine.butler.parametric.Command)

Example 10 with BlockRayHit

use of org.spongepowered.api.util.blockray.BlockRayHit in project modules-extra by CubeEngine.

the class ElevatorListener method findNextSign.

private Vector3i findNextSign(Location<World> loc, Vector3i previous, Vector3i startPos, boolean up) {
    startPos = previous == null ? startPos : previous;
    // Search for next Elevator sign
    BlockRay<World> ray = BlockRay.from(loc.getExtent(), startPos.toDouble()).direction(new Vector3d(0, up ? 1 : -1, 0)).narrowPhase(false).stopFilter(b -> b.getBlockY() <= loc.getExtent().getBlockMax().getY()).stopFilter(b -> b.getBlockY() >= loc.getExtent().getBlockMin().getY()).build();
    while (ray.hasNext()) {
        BlockRayHit<World> next = ray.next();
        if (next.getBlockPosition().equals(startPos)) {
            continue;
        }
        Optional<ElevatorData> targetData = next.getLocation().get(ElevatorData.class);
        if (targetData.isPresent() && !next.getBlockPosition().equals(loc.getBlockPosition())) {
            return next.getBlockPosition();
        }
    }
    // nothing found? Return same location as before when it is valid
    Optional<ElevatorData> targetData = loc.getExtent().get(startPos, ElevatorData.class);
    return targetData.isPresent() ? previous : null;
}
Also used : Keys(org.spongepowered.api.data.key.Keys) ItemTypes(org.spongepowered.api.item.ItemTypes) Vector3d(com.flowpowered.math.vector.Vector3d) BlockRay(org.spongepowered.api.util.blockray.BlockRay) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Text(org.spongepowered.api.text.Text) ACTION_BAR(org.spongepowered.api.text.chat.ChatTypes.ACTION_BAR) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) Location(org.spongepowered.api.world.Location) I18n(org.cubeengine.libcube.service.i18n.I18n) BlockTypes(org.spongepowered.api.block.BlockTypes) IElevatorData(org.cubeengine.module.elevator.data.IElevatorData) ElevatorData(org.cubeengine.module.elevator.data.ElevatorData) POSITIVE(org.cubeengine.libcube.service.i18n.formatter.MessageType.POSITIVE) Root(org.spongepowered.api.event.filter.cause.Root) List(java.util.List) Vector3i(com.flowpowered.math.vector.Vector3i) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) NEGATIVE(org.cubeengine.libcube.service.i18n.formatter.MessageType.NEGATIVE) Listener(org.spongepowered.api.event.Listener) Vector3d(com.flowpowered.math.vector.Vector3d) World(org.spongepowered.api.world.World) IElevatorData(org.cubeengine.module.elevator.data.IElevatorData) ElevatorData(org.cubeengine.module.elevator.data.ElevatorData)

Aggregations

BlockRayHit (org.spongepowered.api.util.blockray.BlockRayHit)12 World (org.spongepowered.api.world.World)11 Player (org.spongepowered.api.entity.living.player.Player)8 BlockType (org.spongepowered.api.block.BlockType)6 Optional (java.util.Optional)4 Command (org.cubeengine.butler.parametric.Command)4 Text (org.spongepowered.api.text.Text)4 Vector3d (com.flowpowered.math.vector.Vector3d)3 List (java.util.List)3 CommandResult (org.spongepowered.api.command.CommandResult)3 BlockRay (org.spongepowered.api.util.blockray.BlockRay)3 Vector3i (com.flowpowered.math.vector.Vector3i)2 DataScanner (io.github.nucleuspowered.nucleus.internal.DataScanner)2 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)2 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)2 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)2 PermissionInformation (io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation)2 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2