Search in sources :

Example 1 with BehaviorContext

use of org.lanternpowered.server.behavior.BehaviorContext in project LanternServer by LanternPowered.

the class InteractWithBlockItemBehavior 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 Direction blockFace = context.getContext(ContextKeys.INTERACTION_FACE).get();
    Location<World> location = optLocation.get();
    if (!location.getProperty(ReplaceableProperty.class).get().getValue()) {
        location = location.add(blockFace.asBlockOffset());
    }
    context.addContext(ContextKeys.BLOCK_LOCATION, location);
    final LanternBlockType blockType = (LanternBlockType) context.getContext(ContextKeys.ITEM_TYPE).get().getBlock().get();
    context.addContext(ContextKeys.BLOCK_TYPE, blockType);
    final BehaviorContext.Snapshot snapshot = context.pushSnapshot();
    // Continue processing through the placement pipeline
    final boolean success = context.process(blockType.getPipeline().pipeline(PlaceBlockBehavior.class), (context1, behavior1) -> behavior1.tryPlace(pipeline, context1)).isSuccess();
    if (success) {
        for (BlockSnapshot blockSnapshot : context.getBlockSnapshots()) {
            final Location<World> location1 = blockSnapshot.getLocation().get();
            final int buildHeight = location1.getExtent().getDimension().getBuildHeight();
            // Check if the block is placed within the building limits
            if (location1.getBlockY() >= buildHeight) {
                context.popSnapshot(snapshot);
                context.getContext(ContextKeys.PLAYER).ifPresent(player -> player.sendMessage(ChatTypes.ACTION_BAR, t("build.tooHigh", buildHeight)));
                return BehaviorResult.FAIL;
            }
        }
        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.PASS;
}
Also used : ContextKeys(org.lanternpowered.server.behavior.ContextKeys) ChatTypes(org.spongepowered.api.text.chat.ChatTypes) Location(org.spongepowered.api.world.Location) Keys(org.spongepowered.api.data.key.Keys) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) BehaviorPipeline(org.lanternpowered.server.behavior.pipeline.BehaviorPipeline) Behavior(org.lanternpowered.server.behavior.Behavior) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) Direction(org.spongepowered.api.util.Direction) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) PlaceBlockBehavior(org.lanternpowered.server.block.behavior.types.PlaceBlockBehavior) World(org.spongepowered.api.world.World) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) Optional(java.util.Optional) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) ReplaceableProperty(org.spongepowered.api.data.property.block.ReplaceableProperty) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) ReplaceableProperty(org.spongepowered.api.data.property.block.ReplaceableProperty) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) Location(org.spongepowered.api.world.Location)

Example 2 with BehaviorContext

use of org.lanternpowered.server.behavior.BehaviorContext 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 BehaviorContext

use of org.lanternpowered.server.behavior.BehaviorContext in project LanternServer by LanternPowered.

the class SimpleBreakBehavior method tryBreak.

@Override
public BehaviorResult tryBreak(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final BlockSnapshotBuilder builder = BlockSnapshotBuilder.create();
    // Apply the creator and notifier to the block
    context.populateBlockSnapshot(builder, BehaviorContext.PopulationFlags.CREATOR_AND_NOTIFIER);
    builder.location(context.requireContext(ContextKeys.BLOCK_LOCATION));
    // The block should be replaced with air
    builder.blockState(BlockTypes.AIR.getDefaultState());
    // Add the block change
    context.addBlockChange(builder.build());
    context.getContext(EventContextKeys.PLAYER).ifPresent(p -> p.get(Keys.EXHAUSTION).ifPresent(value -> p.offer(Keys.EXHAUSTION, value + 0.005)));
    context.process(pipeline.pipeline(BlockDropsProviderBehavior.class), (ctx, behavior) -> {
        behavior.tryAddDrops(pipeline, context);
        return BehaviorResult.CONTINUE;
    });
    return BehaviorResult.CONTINUE;
}
Also used : ContextKeys(org.lanternpowered.server.behavior.ContextKeys) BreakBlockBehavior(org.lanternpowered.server.block.behavior.types.BreakBlockBehavior) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder) Keys(org.spongepowered.api.data.key.Keys) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) BlockTypes(org.spongepowered.api.block.BlockTypes) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) BehaviorPipeline(org.lanternpowered.server.behavior.pipeline.BehaviorPipeline) Behavior(org.lanternpowered.server.behavior.Behavior) BlockDropsProviderBehavior(org.lanternpowered.server.block.behavior.types.BlockDropsProviderBehavior) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder) BlockDropsProviderBehavior(org.lanternpowered.server.block.behavior.types.BlockDropsProviderBehavior)

Aggregations

Behavior (org.lanternpowered.server.behavior.Behavior)3 BehaviorContext (org.lanternpowered.server.behavior.BehaviorContext)3 BehaviorResult (org.lanternpowered.server.behavior.BehaviorResult)3 ContextKeys (org.lanternpowered.server.behavior.ContextKeys)3 BehaviorPipeline (org.lanternpowered.server.behavior.pipeline.BehaviorPipeline)3 Keys (org.spongepowered.api.data.key.Keys)3 Optional (java.util.Optional)2 BlockSnapshotBuilder (org.lanternpowered.server.block.BlockSnapshotBuilder)2 LanternBlockType (org.lanternpowered.server.block.LanternBlockType)2 InteractWithItemBehavior (org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior)2 ReplaceableProperty (org.spongepowered.api.data.property.block.ReplaceableProperty)2 GameModes (org.spongepowered.api.entity.living.player.gamemode.GameModes)2 Direction (org.spongepowered.api.util.Direction)2 Location (org.spongepowered.api.world.Location)2 World (org.spongepowered.api.world.World)2 Supplier (java.util.function.Supplier)1 BlockDropsProviderBehavior (org.lanternpowered.server.block.behavior.types.BlockDropsProviderBehavior)1 BreakBlockBehavior (org.lanternpowered.server.block.behavior.types.BreakBlockBehavior)1 PlaceBlockBehavior (org.lanternpowered.server.block.behavior.types.PlaceBlockBehavior)1 LanternEnumTraits (org.lanternpowered.server.block.trait.LanternEnumTraits)1