use of org.lanternpowered.server.behavior.pipeline.BehaviorPipeline 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.pipeline.BehaviorPipeline 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.pipeline.BehaviorPipeline in project LanternServer by LanternPowered.
the class LanternWorld method placeBlock.
@Override
public boolean placeBlock(int x, int y, int z, BlockState block, Direction side, @Nullable GameProfile profile) {
final BehaviorPipeline<Behavior> pipeline = ((LanternBlockType) block.getType()).getPipeline();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(ContextKeys.USED_BLOCK_STATE, block);
frame.addContext(ContextKeys.INTERACTION_FACE, side);
frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(this, x, y, z));
frame.addContext(ContextKeys.BLOCK_TYPE, block.getType());
if (profile != null) {
frame.addContext(EventContextKeys.PLAYER_SIMULATED, profile);
}
final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
// Just pass an object trough to make sure that a value is present when successful
if (context.process(pipeline.pipeline(PlaceBlockBehavior.class), (ctx, behavior) -> behavior.tryPlace(pipeline, ctx)).isSuccess()) {
context.accept();
return true;
}
context.revert();
return false;
}
}
use of org.lanternpowered.server.behavior.pipeline.BehaviorPipeline in project LanternServer by LanternPowered.
the class LanternWorld method digBlock.
private boolean digBlock(int x, int y, int z, @Nullable GameProfile profile, @Nullable ItemStack itemStack) {
final LanternBlockType blockType = ((LanternBlockType) getBlockType(x, y, z));
final BehaviorPipeline<Behavior> pipeline = blockType.getPipeline();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(this, x, y, z));
frame.addContext(ContextKeys.BLOCK_TYPE, blockType);
if (profile != null) {
frame.addContext(EventContextKeys.PLAYER_SIMULATED, profile);
}
if (itemStack != null) {
frame.addContext(ContextKeys.USED_ITEM_STACK, itemStack);
}
final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
// Just pass an object trough to make sure that a value is present when successful
if (context.process(pipeline.pipeline(BreakBlockBehavior.class), (ctx, behavior) -> behavior.tryBreak(pipeline, ctx)).isSuccess()) {
context.accept();
return true;
}
context.revert();
return false;
}
}
use of org.lanternpowered.server.behavior.pipeline.BehaviorPipeline in project LanternServer by LanternPowered.
the class LanternWorld method interactBlock.
private boolean interactBlock(int x, int y, int z, Direction side, @Nullable GameProfile profile, @Nullable ItemStack itemStack) {
checkNotNull(side, "side");
final LanternBlockType blockType = ((LanternBlockType) getBlockType(x, y, z));
final BehaviorPipeline<Behavior> pipeline = blockType.getPipeline();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(ContextKeys.INTERACTION_FACE, side);
frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(this, x, y, z));
frame.addContext(ContextKeys.BLOCK_TYPE, blockType);
if (profile != null) {
frame.addContext(EventContextKeys.PLAYER_SIMULATED, profile);
}
if (itemStack != null) {
frame.addContext(ContextKeys.USED_ITEM_STACK, itemStack);
}
final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
// Just pass an object trough to make sure that a value is present when successful
if (context.process(pipeline.pipeline(InteractWithBlockBehavior.class), (ctx, behavior) -> behavior.tryInteract(pipeline, ctx)).isSuccess()) {
context.accept();
return true;
}
context.revert();
return false;
}
}
Aggregations