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;
}
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;
}
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;
}
Aggregations