Search in sources :

Example 46 with Location

use of org.spongepowered.api.world.Location in project LanternServer by LanternPowered.

the class CommandTeleport method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(// TODO: Replace with entity selector
    GenericArguments.player(Text.of("target")), GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none()), GenericArguments.vector3d(Text.of("position")), GenericArguments.optional(GenericArguments.seq(GenericArguments2.relativeDoubleNum(Text.of("y-rot")), GenericArguments2.relativeDoubleNum(Text.of("x-rot"))))).executor((src, args) -> {
        // TODO: Replace with selected entities
        final Entity target = args.<Entity>getOne("target").get();
        final World world = CommandHelper.getWorld(src, args);
        final Location<World> location = new Location<>(world, args.<Vector3d>getOne("position").get());
        if (args.hasAny("y-rot")) {
            RelativeDouble yRot = args.<RelativeDouble>getOne("y-rot").get();
            RelativeDouble xRot = args.<RelativeDouble>getOne("x-rot").get();
            boolean rel = yRot.isRelative() || xRot.isRelative();
            if (rel && !(src instanceof Locatable)) {
                throw new CommandException(t("Relative rotation specified but source does not have a rotation."));
            }
            double xRot0 = xRot.getValue();
            double yRot0 = yRot.getValue();
            double zRot0 = 0;
            // is locatable, just handle it then as absolute
            if (src instanceof Entity) {
                final Vector3d rot = ((Entity) src).getRotation();
                xRot0 = xRot.applyToValue(rot.getX());
                yRot0 = yRot.applyToValue(rot.getY());
                zRot0 = rot.getZ();
            }
            target.setLocationAndRotation(location, new Vector3d(xRot0, yRot0, zRot0));
        } else {
            target.setLocation(location);
        }
        src.sendMessage(t("commands.teleport.success.coordinates", location.getX(), location.getY(), location.getZ()));
        return CommandResult.success();
    });
}
Also used : Entity(org.spongepowered.api.entity.Entity) Vector3d(com.flowpowered.math.vector.Vector3d) RelativeDouble(org.lanternpowered.server.command.element.RelativeDouble) CommandException(org.spongepowered.api.command.CommandException) World(org.spongepowered.api.world.World) Location(org.spongepowered.api.world.Location) Locatable(org.spongepowered.api.world.Locatable)

Example 47 with Location

use of org.spongepowered.api.world.Location in project LanternServer by LanternPowered.

the class PlayerInteractionHandler method handleBrokenBlock.

private void handleBrokenBlock() {
    final Location<World> location = new Location<>(this.player.getWorld(), this.diggingBlock);
    final CauseStack causeStack = CauseStack.current();
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        frame.pushCause(this.player);
        // Add context
        frame.addContext(EventContextKeys.PLAYER, this.player);
        frame.addContext(ContextKeys.INTERACTION_LOCATION, location);
        frame.addContext(ContextKeys.BLOCK_LOCATION, location);
        final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
        final BlockState blockState = location.getBlock();
        final LanternBlockType blockType = (LanternBlockType) blockState.getType();
        if (context.process(blockType.getPipeline().pipeline(BreakBlockBehavior.class), (ctx, behavior) -> behavior.tryBreak(blockType.getPipeline(), ctx)).isSuccess()) {
            context.accept();
            this.diggingBlock = null;
            this.diggingBlockType = null;
        } else {
            context.revert();
            // TODO: Resend tile entity data, action data, ... ???
            this.player.sendBlockChange(this.diggingBlock, blockState);
        }
        if (this.lastBreakState != -1) {
            sendBreakUpdate(-1);
        }
    }
}
Also used : MessagePlayOutEntityAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) MessagePlayInPlayerSwingArm(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerSwingArm) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayInOutFinishUsingItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutFinishUsingItem) MessagePlayInPlayerDigging(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging) HandType(org.spongepowered.api.data.type.HandType) MinimumUseDurationProperty(org.lanternpowered.server.item.property.MinimumUseDurationProperty) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) BreakBlockBehavior(org.lanternpowered.server.block.behavior.types.BreakBlockBehavior) ItemStack(org.spongepowered.api.item.inventory.ItemStack) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) CauseStack(org.lanternpowered.server.event.CauseStack) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) MessagePlayOutBlockBreakAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutBlockBreakAnimation) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) LanternWorld(org.lanternpowered.server.world.LanternWorld) InteractWithBlockBehavior(org.lanternpowered.server.block.behavior.types.InteractWithBlockBehavior) Nullable(javax.annotation.Nullable) ContextKeys(org.lanternpowered.server.behavior.ContextKeys) FinishUsingItemBehavior(org.lanternpowered.server.item.behavior.types.FinishUsingItemBehavior) Location(org.spongepowered.api.world.Location) MaximumUseDurationProperty(org.lanternpowered.server.item.property.MaximumUseDurationProperty) LanternItemType(org.lanternpowered.server.item.LanternItemType) MessagePlayInPlayerUseItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerUseItem) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) BlockTypes(org.spongepowered.api.block.BlockTypes) SwingHandEntityEvent(org.lanternpowered.server.entity.event.SwingHandEntityEvent) Set(java.util.Set) LanternGame(org.lanternpowered.server.game.LanternGame) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) Direction(org.spongepowered.api.util.Direction) DualWieldProperty(org.lanternpowered.server.item.property.DualWieldProperty) Lantern(org.lanternpowered.server.game.Lantern) 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) MessagePlayInPlayerBlockPlacement(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement) CauseStack(org.lanternpowered.server.event.CauseStack) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) BlockState(org.spongepowered.api.block.BlockState) LanternWorld(org.lanternpowered.server.world.LanternWorld) World(org.spongepowered.api.world.World) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) Location(org.spongepowered.api.world.Location)

Example 48 with Location

use of org.spongepowered.api.world.Location in project LanternServer by LanternPowered.

the class HandlerPlayInTabComplete method handle.

@Override
public void handle(NetworkContext context, MessagePlayInTabComplete message) {
    final String text = message.getText();
    // The content with normalized spaces, the spaces are trimmed
    // from the ends and there are never two spaces directly after eachother
    final String textNormalized = StringUtils.normalizeSpace(text);
    final Player player = context.getSession().getPlayer();
    final Location<World> targetBlock = message.getBlockPosition().map(pos -> new Location<>(player.getWorld(), pos)).orElse(null);
    final boolean hasPrefix = textNormalized.startsWith("/");
    if (hasPrefix || message.getAssumeCommand()) {
        String command = textNormalized;
        // Don't include the '/'
        if (hasPrefix) {
            command = command.substring(1);
        }
        // Keep the last space, it must be there!
        if (text.endsWith(" ")) {
            command = command + " ";
        }
        // Get the suggestions
        List<String> suggestions = ((LanternCommandManager) Sponge.getCommandManager()).getSuggestions(player, command, targetBlock, message.getAssumeCommand());
        // If the suggestions are for the command and there was a prefix, then append the prefix
        if (hasPrefix && command.split(" ").length == 1 && !command.endsWith(" ")) {
            suggestions = suggestions.stream().map(suggestion -> '/' + suggestion).collect(ImmutableList.toImmutableList());
        }
        context.getSession().send(new MessagePlayOutTabComplete(suggestions));
    } else {
        // Vanilla mc will complete user names if
        // no command is being completed
        final int index = text.lastIndexOf(' ');
        final String part;
        if (index == -1) {
            part = text;
        } else {
            part = text.substring(index + 1);
        }
        if (part.isEmpty()) {
            return;
        }
        final String part1 = part.toLowerCase();
        final List<String> suggestions = Sponge.getServer().getOnlinePlayers().stream().map(CommandSource::getName).filter(n -> n.toLowerCase().startsWith(part1)).collect(Collectors.toList());
        final Cause cause = Cause.of(EventContext.empty(), context.getSession().getPlayer());
        final TabCompleteEvent.Chat event = SpongeEventFactory.createTabCompleteEventChat(cause, ImmutableList.copyOf(suggestions), suggestions, text, Optional.ofNullable(targetBlock), false);
        if (!Sponge.getEventManager().post(event)) {
            context.getSession().send(new MessagePlayOutTabComplete(suggestions));
        }
    }
}
Also used : Location(org.spongepowered.api.world.Location) NetworkContext(org.lanternpowered.server.network.NetworkContext) CommandSource(org.spongepowered.api.command.CommandSource) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) MessagePlayInTabComplete(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInTabComplete) Handler(org.lanternpowered.server.network.message.handler.Handler) Sponge(org.spongepowered.api.Sponge) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) World(org.spongepowered.api.world.World) TabCompleteEvent(org.spongepowered.api.event.command.TabCompleteEvent) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) LanternCommandManager(org.lanternpowered.server.command.LanternCommandManager) MessagePlayOutTabComplete(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutTabComplete) EventContext(org.spongepowered.api.event.cause.EventContext) Player(org.spongepowered.api.entity.living.player.Player) World(org.spongepowered.api.world.World) CommandSource(org.spongepowered.api.command.CommandSource) TabCompleteEvent(org.spongepowered.api.event.command.TabCompleteEvent) LanternCommandManager(org.lanternpowered.server.command.LanternCommandManager) Cause(org.spongepowered.api.event.cause.Cause) MessagePlayOutTabComplete(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutTabComplete) Location(org.spongepowered.api.world.Location)

Example 49 with Location

use of org.spongepowered.api.world.Location in project LanternServer by LanternPowered.

the class LanternChunk method createSnapshot.

@Override
public BlockSnapshot createSnapshot(int x, int y, int z) {
    final BlockState state = getBlock(x, y, z);
    final Location<World> loc = new Location<>(this.world, x, y, z);
    // TODO: Tile entity data
    return new LanternBlockSnapshot(loc, state, ((LanternBlockType) state.getType()).getExtendedBlockStateProvider().get(state, loc, null), getCreator(x, y, z), getNotifier(x, y, z), ImmutableMap.of());
}
Also used : LanternBlockSnapshot(org.lanternpowered.server.block.LanternBlockSnapshot) BlockState(org.spongepowered.api.block.BlockState) World(org.spongepowered.api.world.World) LanternWorld(org.lanternpowered.server.world.LanternWorld) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) Location(org.spongepowered.api.world.Location)

Example 50 with Location

use of org.spongepowered.api.world.Location in project LanternServer by LanternPowered.

the class LanternChunk method getBlockSelectionBox.

@Override
public Optional<AABB> getBlockSelectionBox(int x, int y, int z) {
    final BlockState block = getBlock(x, y, z);
    if (block.getType() == BlockTypes.AIR) {
        return Optional.empty();
    }
    final ObjectProvider<AABB> aabbObjectProvider = ((LanternBlockType) block.getType()).getBoundingBoxProvider();
    if (aabbObjectProvider == null) {
        return Optional.empty();
    }
    final AABB aabb;
    if (aabbObjectProvider instanceof ConstantObjectProvider || aabbObjectProvider instanceof CachedSimpleObjectProvider || aabbObjectProvider instanceof SimpleObjectProvider) {
        aabb = aabbObjectProvider.get(block, null, null);
    } else {
        aabb = aabbObjectProvider.get(block, new Location<>(this.world, x, y, z), null);
    }
    return aabb == null ? Optional.empty() : Optional.of(aabb.offset(x, y, z));
}
Also used : BlockState(org.spongepowered.api.block.BlockState) ConstantObjectProvider(org.lanternpowered.server.block.provider.ConstantObjectProvider) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) AABB(org.spongepowered.api.util.AABB) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) SimpleObjectProvider(org.lanternpowered.server.block.provider.SimpleObjectProvider) Location(org.spongepowered.api.world.Location)

Aggregations

Location (org.spongepowered.api.world.Location)166 World (org.spongepowered.api.world.World)96 Listener (org.spongepowered.api.event.Listener)44 Player (org.spongepowered.api.entity.living.player.Player)40 Vector3d (com.flowpowered.math.vector.Vector3d)38 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)32 Vector3i (com.flowpowered.math.vector.Vector3i)31 Optional (java.util.Optional)28 Entity (org.spongepowered.api.entity.Entity)27 BlockType (org.spongepowered.api.block.BlockType)24 BlockState (org.spongepowered.api.block.BlockState)23 ItemStack (org.spongepowered.api.item.inventory.ItemStack)23 Direction (org.spongepowered.api.util.Direction)21 ArrayList (java.util.ArrayList)20 Keys (org.spongepowered.api.data.key.Keys)20 List (java.util.List)19 Text (org.spongepowered.api.text.Text)19 BlockPos (net.minecraft.util.math.BlockPos)18 Sponge (org.spongepowered.api.Sponge)15 Map (java.util.Map)14