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