Search in sources :

Example 1 with BlockRay

use of org.spongepowered.api.util.blockray.BlockRay 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)

Example 2 with BlockRay

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

the class EntityInfoCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
    // Get all the entities in the world.
    Vector3i playerPos = player.getLocation().getBlockPosition();
    Collection<Entity> entities = player.getWorld().getEntities().stream().filter(// 11 blocks.
    x -> x.getLocation().getBlockPosition().distanceSquared(playerPos) < 121).collect(Collectors.toList());
    BlockRay<World> bl = BlockRay.from(player).distanceLimit(10).stopFilter(BlockRay.continueAfterFilter(x -> {
        Vector3i pt1 = x.getLocation().getBlockPosition();
        Vector3i pt2 = pt1.add(0, 1, 0);
        return entities.stream().allMatch(e -> {
            Vector3i current = e.getLocation().getBlockPosition();
            // We don't want it to stop until one of these are hit.
            return !(current.equals(pt1) || current.equals(pt2));
        });
    }, 1)).build();
    Optional<BlockRayHit<World>> ob = bl.end();
    if (ob.isPresent()) {
        BlockRayHit<World> brh = ob.get();
        Vector3d location = brh.getLocation().getPosition();
        Vector3d locationOneUp = location.add(0, 1, 0);
        Optional<Entity> entityOptional = entities.stream().filter(e -> {
            Vector3i current = e.getLocation().getBlockPosition();
            return current.equals(location.toInt()) || current.equals(locationOneUp.toInt());
        }).sorted(Comparator.comparingDouble(x -> x.getLocation().getPosition().distanceSquared(location))).findFirst();
        if (entityOptional.isPresent()) {
            // Display info about the entity
            Entity entity = entityOptional.get();
            EntityType type = entity.getType();
            List<Text> lt = new ArrayList<>();
            lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.entityinfo.id", type.getId(), Util.getTranslatableIfPresent(type)));
            lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.entityinfo.uuid", entity.getUniqueId().toString()));
            if (args.hasAny("e") || args.hasAny("extended")) {
                // For each key, see if the entity supports it. If so, get and print the value.
                DataScanner.getInstance().getKeysForHolder(entity).entrySet().stream().filter(x -> x.getValue() != null).filter(x -> {
                    // Work around a Sponge bug.
                    try {
                        return entity.supports(x.getValue());
                    } catch (Exception e) {
                        return false;
                    }
                }).forEach(x -> {
                    Key<? extends BaseValue<Object>> k = (Key<? extends BaseValue<Object>>) x.getValue();
                    if (entity.get(k).isPresent()) {
                        DataScanner.getText(player, "command.entityinfo.key", x.getKey(), entity.get(k).get()).ifPresent(lt::add);
                    }
                });
            }
            Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.entityinfo.list.header", String.valueOf(brh.getBlockX()), String.valueOf(brh.getBlockY()), String.valueOf(brh.getBlockZ()))).sendTo(player);
            return CommandResult.success();
        }
    }
    player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.entityinfo.none"));
    return CommandResult.empty();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Vector3d(com.flowpowered.math.vector.Vector3d) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) BlockRay(org.spongepowered.api.util.blockray.BlockRay) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) CommandResult(org.spongepowered.api.command.CommandResult) Collection(java.util.Collection) BaseValue(org.spongepowered.api.data.value.BaseValue) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) Vector3i(com.flowpowered.math.vector.Vector3i) World(org.spongepowered.api.world.World) EntityType(org.spongepowered.api.entity.EntityType) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Comparator(java.util.Comparator) Entity(org.spongepowered.api.entity.Entity) BaseValue(org.spongepowered.api.data.value.BaseValue) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) EntityType(org.spongepowered.api.entity.EntityType) Vector3d(com.flowpowered.math.vector.Vector3d) Vector3i(com.flowpowered.math.vector.Vector3i) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Key(org.spongepowered.api.data.key.Key) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Example 3 with BlockRay

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

the class BlockInfoCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
    BlockRay<World> bl = BlockRay.from(player).distanceLimit(10).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).build();
    Optional<BlockRayHit<World>> ob = bl.end();
    // If the last block is not air...
    if (ob.isPresent() && ob.get().getLocation().getBlockType() != BlockTypes.AIR) {
        BlockRayHit<World> brh = ob.get();
        // get the information.
        BlockState b = brh.getLocation().getBlock();
        BlockType it = b.getType();
        List<Text> lt = new ArrayList<>();
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.id", it.getId(), it.getTranslation().get()));
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.extendedid", b.getId()));
        if (args.hasAny("e") || args.hasAny("extended")) {
            Collection<Property<?, ?>> cp = b.getApplicableProperties();
            if (!cp.isEmpty()) {
                cp.forEach(x -> {
                    if (x.getValue() != null) {
                        DataScanner.getText(player, "command.blockinfo.property.item", x.getKey().toString(), x.getValue()).ifPresent(lt::add);
                    }
                });
            }
            Collection<BlockTrait<?>> cb = b.getTraits();
            if (!cb.isEmpty()) {
                cb.forEach(x -> b.getTraitValue(x).ifPresent(v -> DataScanner.getText(player, "command.blockinfo.traits.item", x.getName(), v).ifPresent(lt::add)));
            }
        }
        Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.list.header", String.valueOf(brh.getBlockX()), String.valueOf(brh.getBlockY()), String.valueOf(brh.getBlockZ()))).sendTo(player);
        return CommandResult.success();
    }
    player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.none"));
    return CommandResult.empty();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) BlockRay(org.spongepowered.api.util.blockray.BlockRay) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ArrayList(java.util.ArrayList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) CommandResult(org.spongepowered.api.command.CommandResult) BlockTypes(org.spongepowered.api.block.BlockTypes) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) Property(org.spongepowered.api.data.Property) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Property(org.spongepowered.api.data.Property) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Aggregations

List (java.util.List)3 Optional (java.util.Optional)3 Player (org.spongepowered.api.entity.living.player.Player)3 Text (org.spongepowered.api.text.Text)3 BlockRay (org.spongepowered.api.util.blockray.BlockRay)3 BlockRayHit (org.spongepowered.api.util.blockray.BlockRayHit)3 World (org.spongepowered.api.world.World)3 Vector3d (com.flowpowered.math.vector.Vector3d)2 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 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Sponge (org.spongepowered.api.Sponge)2