Search in sources :

Example 1 with BlockSnapshotBuilder

use of org.lanternpowered.server.block.BlockSnapshotBuilder in project LanternServer by LanternPowered.

the class OpenableInteractionBehavior method tryInteract.

@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Location<World> location = context.requireContext(ContextKeys.BLOCK_LOCATION);
    final Optional<Boolean> optIsOpen = location.get(Keys.OPEN);
    if (!optIsOpen.isPresent()) {
        return BehaviorResult.FAIL;
    }
    final boolean isOpen = optIsOpen.get();
    final boolean success = this.access.get(context, isOpen ? OpenCloseAccess.Action.CLOSE : OpenCloseAccess.Action.OPEN, DEFAULT_ACCESS);
    if (!success) {
        return BehaviorResult.PASS;
    }
    final BlockSnapshotBuilder builder = BlockSnapshotBuilder.create();
    builder.from(location);
    context.populateBlockSnapshot(builder, BehaviorContext.PopulationFlags.NOTIFIER);
    builder.add(Keys.OPEN, !isOpen);
    context.addBlockChange(builder.build());
    return BehaviorResult.SUCCESS;
}
Also used : World(org.spongepowered.api.world.World) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder)

Example 2 with BlockSnapshotBuilder

use of org.lanternpowered.server.block.BlockSnapshotBuilder in project LanternServer by LanternPowered.

the class SlabItemInteractionBehavior method tryInteract.

@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Optional<Location<World>> optLocation = context.getContext(ContextKeys.INTERACTION_LOCATION);
    if (!optLocation.isPresent()) {
        return BehaviorResult.CONTINUE;
    }
    final BlockType halfSlabType = this.halfSlabType.get();
    final BlockType doubleSlabType = this.doubleSlabType.get();
    Location<World> location = optLocation.get();
    final Direction blockFace = context.getContext(ContextKeys.INTERACTION_FACE).get().getOpposite();
    final LanternBlockType blockType = (LanternBlockType) context.getContext(ContextKeys.ITEM_TYPE).get().getBlock().get();
    if (blockType != halfSlabType) {
        return BehaviorResult.PASS;
    }
    BlockState state = location.getBlock();
    final BlockState.Builder stateBuilder = BlockState.builder();
    stateBuilder.blockType(blockType);
    context.getContext(ContextKeys.USED_ITEM_STACK).ifPresent(itemStack -> itemStack.getValues().forEach(value -> stateBuilder.add((Key) value.getKey(), value.get())));
    BlockState blockState = stateBuilder.build();
    BlockSnapshotBuilder snapshotBuilder = null;
    boolean success = false;
    if (state.getType() == blockType) {
        if (state.getTraitValue(this.variantTrait).get().equals(blockState.getTraitValue(this.variantTrait).get())) {
            final PortionType portionType = state.getTraitValue(LanternEnumTraits.PORTION_TYPE).get();
            if ((blockFace == Direction.DOWN && portionType == PortionTypes.BOTTOM) || (blockFace == Direction.UP && portionType == PortionTypes.TOP)) {
                snapshotBuilder = BlockSnapshotBuilder.create().blockState(doubleSlabType.getDefaultState());
                success = true;
            }
        }
    } else if (location.getProperty(ReplaceableProperty.class).get().getValue()) {
        success = true;
    }
    if (!success) {
        location = location.add(blockFace.getOpposite().asBlockOffset());
        state = location.getBlock();
        if (state.getType() == blockType) {
            if (state.getTraitValue(this.variantTrait).get().equals(blockState.getTraitValue(this.variantTrait).get())) {
                final PortionType portionType = state.getTraitValue(LanternEnumTraits.PORTION_TYPE).get();
                if ((blockFace == Direction.DOWN && portionType == PortionTypes.TOP) || (blockFace == Direction.UP && portionType == PortionTypes.BOTTOM)) {
                    snapshotBuilder = BlockSnapshotBuilder.create().blockState(doubleSlabType.getDefaultState());
                    success = true;
                }
            }
        } else if (location.getProperty(ReplaceableProperty.class).get().getValue()) {
            success = true;
        }
    }
    if (success) {
        if (snapshotBuilder == null) {
            PortionType portionType;
            if (blockFace == Direction.UP) {
                portionType = PortionTypes.TOP;
            } else if (blockFace == Direction.DOWN) {
                portionType = PortionTypes.BOTTOM;
            } else {
                final double y = location.getY() - location.getBlockY();
                if (y >= 0.5) {
                    portionType = PortionTypes.TOP;
                } else {
                    portionType = PortionTypes.BOTTOM;
                }
            }
            snapshotBuilder = BlockSnapshotBuilder.create().blockState(halfSlabType.getDefaultState()).add(Keys.PORTION_TYPE, portionType);
        }
        final BlockSnapshotBuilder snapshotBuilder1 = snapshotBuilder;
        snapshotBuilder1.location(location);
        context.getContext(ContextKeys.USED_ITEM_STACK).ifPresent(itemStack -> itemStack.getValues().forEach(value -> snapshotBuilder1.add((Key) value.getKey(), value.get())));
        context.addBlockChange(snapshotBuilder1.build());
        context.getContext(ContextKeys.PLAYER).ifPresent(player -> {
            if (!player.get(Keys.GAME_MODE).orElse(GameModes.NOT_SET).equals(GameModes.CREATIVE)) {
                context.requireContext(ContextKeys.USED_SLOT).poll(1);
            }
        });
        return BehaviorResult.SUCCESS;
    }
    return BehaviorResult.FAIL;
}
Also used : ContextKeys(org.lanternpowered.server.behavior.ContextKeys) PortionTypes(org.spongepowered.api.data.type.PortionTypes) Location(org.spongepowered.api.world.Location) Keys(org.spongepowered.api.data.key.Keys) PortionType(org.spongepowered.api.data.type.PortionType) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) BehaviorPipeline(org.lanternpowered.server.behavior.pipeline.BehaviorPipeline) EnumTrait(org.spongepowered.api.block.trait.EnumTrait) Behavior(org.lanternpowered.server.behavior.Behavior) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) Supplier(java.util.function.Supplier) Key(org.spongepowered.api.data.key.Key) BlockState(org.spongepowered.api.block.BlockState) Direction(org.spongepowered.api.util.Direction) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) World(org.spongepowered.api.world.World) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) BlockType(org.spongepowered.api.block.BlockType) Optional(java.util.Optional) LanternEnumTraits(org.lanternpowered.server.block.trait.LanternEnumTraits) ReplaceableProperty(org.spongepowered.api.data.property.block.ReplaceableProperty) PortionType(org.spongepowered.api.data.type.PortionType) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) BlockState(org.spongepowered.api.block.BlockState) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) BlockType(org.spongepowered.api.block.BlockType) ReplaceableProperty(org.spongepowered.api.data.property.block.ReplaceableProperty) Location(org.spongepowered.api.world.Location) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder)

Example 3 with BlockSnapshotBuilder

use of org.lanternpowered.server.block.BlockSnapshotBuilder in project LanternServer by LanternPowered.

the class DoorBehavior method tryPlace.

@Override
public BehaviorResult tryPlace(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Location<World> location = context.requireContext(ContextKeys.BLOCK_LOCATION);
    final Direction face = context.requireContext(ContextKeys.INTERACTION_FACE);
    // Door can only be placed by clicking in the floor
    if (face != Direction.DOWN) {
        return BehaviorResult.PASS;
    }
    final Location<World> down = location.getBlockRelative(Direction.DOWN);
    final SolidCubeProperty solidProp = down.getProperty(SolidCubeProperty.class).get();
    // The door must be placed on a solid block
    if (!solidProp.getValue()) {
        return BehaviorResult.PASS;
    }
    final Location<World> up = location.getBlockRelative(Direction.UP);
    final ReplaceableProperty replaceableProp = up.getProperty(ReplaceableProperty.class).get();
    if (!replaceableProp.getValue()) {
        return BehaviorResult.PASS;
    }
    final BlockSnapshot snapshot = context.getContext(ContextKeys.BLOCK_SNAPSHOT).orElseThrow(() -> new IllegalStateException("The BlockSnapshotRetrieveBehavior BlockSnapshot isn't present."));
    final BlockSnapshotBuilder builder = BlockSnapshotBuilder.create().from(snapshot);
    context.populateBlockSnapshot(builder, BehaviorContext.PopulationFlags.CREATOR_AND_NOTIFIER);
    Direction facing = Direction.NORTH;
    Vector3i left = Vector3i.UNIT_X;
    final Optional<Entity> optSource = context.first(Entity.class);
    if (optSource.isPresent()) {
        final Entity source = optSource.get();
        final Vector3d rotVector;
        if (source instanceof Living) {
            rotVector = ((Living) source).getHeadRotation();
        } else {
            rotVector = optSource.get().getRotation();
        }
        // Calculate the direction the entity is looking
        final Vector3d dir = Quaternions.fromAxesAnglesDeg(rotVector.mul(-1)).rotate(Vector3d.FORWARD);
        facing = Direction.getClosestHorizontal(dir, Direction.Division.CARDINAL);
        left = LEFT_ANGLE.rotate(facing.asOffset()).toInt();
        facing = facing.getOpposite();
    }
    builder.add(Keys.DIRECTION, facing);
    // TODO: Hinges
    context.addBlockChange(builder.location(location).build());
    context.addBlockChange(builder.add(LanternKeys.DOOR_HALF, LanternDoorHalf.UPPER).location(up).build());
    return BehaviorResult.SUCCESS;
}
Also used : Entity(org.spongepowered.api.entity.Entity) Living(org.spongepowered.api.entity.living.Living) SolidCubeProperty(org.spongepowered.api.data.property.block.SolidCubeProperty) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) Vector3d(com.flowpowered.math.vector.Vector3d) ReplaceableProperty(org.spongepowered.api.data.property.block.ReplaceableProperty) Vector3i(com.flowpowered.math.vector.Vector3i) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder)

Example 4 with BlockSnapshotBuilder

use of org.lanternpowered.server.block.BlockSnapshotBuilder in project LanternServer by LanternPowered.

the class DoorBehavior method tryBreak.

@Override
public BehaviorResult tryBreak(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Location<World> location = context.getContext(ContextKeys.BLOCK_LOCATION).get();
    final BlockState baseState = location.getBlock();
    final LanternDoorHalf half = baseState.get(LanternKeys.DOOR_HALF).get();
    final BlockSnapshotBuilder builder = BlockSnapshotBuilder.create();
    builder.blockState(BlockTypes.AIR.getDefaultState());
    context.populateBlockSnapshot(builder, BehaviorContext.PopulationFlags.CREATOR_AND_NOTIFIER);
    builder.location(location);
    context.addBlockChange(builder.build());
    final Direction dir = half == LanternDoorHalf.LOWER ? Direction.UP : Direction.DOWN;
    final LanternDoorHalf other = half == LanternDoorHalf.LOWER ? LanternDoorHalf.UPPER : LanternDoorHalf.LOWER;
    final Location<World> loc = location.getBlockRelative(dir);
    BlockState otherState = loc.getBlock();
    if (otherState.get(LanternKeys.DOOR_HALF).orElse(null) == other && otherState.with(LanternKeys.DOOR_HALF, half).orElse(null) == baseState) {
        builder.location(loc);
        context.addBlockChange(builder.build());
        return BehaviorResult.CONTINUE;
    }
    return BehaviorResult.CONTINUE;
}
Also used : LanternDoorHalf(org.lanternpowered.server.data.type.LanternDoorHalf) BlockState(org.spongepowered.api.block.BlockState) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder)

Example 5 with BlockSnapshotBuilder

use of org.lanternpowered.server.block.BlockSnapshotBuilder in project LanternServer by LanternPowered.

the class TorchPlacementBehavior method tryPlace.

@Override
public BehaviorResult tryPlace(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    Direction face = context.requireContext(ContextKeys.INTERACTION_FACE).getOpposite();
    if (face == Direction.DOWN) {
        return BehaviorResult.PASS;
    }
    final BlockSnapshot snapshot = context.getContext(ContextKeys.BLOCK_SNAPSHOT).orElseThrow(() -> new IllegalStateException("The BlockSnapshotProviderPlaceBehavior's BlockSnapshot isn't present."));
    final Location<World> location = context.requireContext(ContextKeys.BLOCK_LOCATION);
    final Location<World> clickLocation = context.requireContext(ContextKeys.INTERACTION_LOCATION);
    boolean flag = clickLocation.getExtent().getProperty(clickLocation.getBlockPosition(), face, SolidSideProperty.class).get().getValue();
    BlockSnapshotBuilder builder = BlockSnapshotBuilder.create().from(snapshot);
    if (!flag) {
        for (Direction direction : HORIZONTAL_DIRECTIONS) {
            flag = location.getExtent().getProperty(location.getBlockRelative(direction).getBlockPosition(), direction.getOpposite(), SolidSideProperty.class).get().getValue();
            if (flag) {
                face = direction;
                break;
            }
        }
    }
    if (flag) {
        builder.add(Keys.DIRECTION, face.getOpposite());
    }
    context.addBlockChange(builder.location(location).build());
    return BehaviorResult.CONTINUE;
}
Also used : SolidSideProperty(org.lanternpowered.server.block.property.SolidSideProperty) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder)

Aggregations

BlockSnapshotBuilder (org.lanternpowered.server.block.BlockSnapshotBuilder)8 World (org.spongepowered.api.world.World)7 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)4 Direction (org.spongepowered.api.util.Direction)4 Location (org.spongepowered.api.world.Location)3 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Behavior (org.lanternpowered.server.behavior.Behavior)2 BehaviorContext (org.lanternpowered.server.behavior.BehaviorContext)2 BehaviorResult (org.lanternpowered.server.behavior.BehaviorResult)2 ContextKeys (org.lanternpowered.server.behavior.ContextKeys)2 BehaviorPipeline (org.lanternpowered.server.behavior.pipeline.BehaviorPipeline)2 LanternBlockSnapshot (org.lanternpowered.server.block.LanternBlockSnapshot)2 BlockState (org.spongepowered.api.block.BlockState)2 Keys (org.spongepowered.api.data.key.Keys)2 ReplaceableProperty (org.spongepowered.api.data.property.block.ReplaceableProperty)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Vector3i (com.flowpowered.math.vector.Vector3i)1 Optional (java.util.Optional)1