use of org.spongepowered.api.block.BlockType 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.spongepowered.api.block.BlockType in project LanternServer by LanternPowered.
the class PlayerInteractionHandler method handleBlockPlacing.
public void handleBlockPlacing(MessagePlayInPlayerBlockPlacement message) {
final HandType handType = message.getHandType();
// the main hand message.
if (handType == HandTypes.OFF_HAND) {
return;
}
// Try the action of the hotbar item first
final AbstractSlot hotbarSlot = this.player.getInventory().getHotbar().getSelectedSlot();
final AbstractSlot offHandSlot = this.player.getInventory().getOffhand();
// The offset can round up to 1, causing
// an incorrect clicked block location
final Vector3d pos2 = message.getClickOffset();
final double dx = Math.min(pos2.getX(), 0.999);
final double dy = Math.min(pos2.getY(), 0.999);
final double dz = Math.min(pos2.getZ(), 0.999);
final Location<World> clickedLocation = new Location<>(this.player.getWorld(), message.getPosition().toDouble().add(dx, dy, dz));
final Direction face = message.getFace();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.pushCause(this.player);
// Add context
frame.addContext(ContextKeys.INTERACTION_FACE, face);
frame.addContext(ContextKeys.INTERACTION_LOCATION, clickedLocation);
frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(clickedLocation.getExtent(), message.getPosition()));
frame.addContext(ContextKeys.PLAYER, this.player);
final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
final BehaviorContext.Snapshot snapshot = context.pushSnapshot();
if (!this.player.get(Keys.IS_SNEAKING).orElse(false)) {
final BlockState blockState = this.player.getWorld().getBlock(message.getPosition());
final LanternBlockType blockType = (LanternBlockType) blockState.getType();
frame.addContext(ContextKeys.BLOCK_TYPE, blockState.getType());
frame.addContext(ContextKeys.USED_BLOCK_STATE, blockState);
BehaviorContext.Snapshot snapshot1 = context.pushSnapshot();
// Try first with the main hand
hotbarSlot.peek().ifPresent(stack -> frame.addContext(ContextKeys.USED_ITEM_STACK, stack));
frame.addContext(ContextKeys.USED_SLOT, hotbarSlot);
frame.addContext(ContextKeys.INTERACTION_HAND, HandTypes.MAIN_HAND);
BehaviorResult result = context.process(blockType.getPipeline().pipeline(InteractWithBlockBehavior.class), (ctx, behavior) -> behavior.tryInteract(blockType.getPipeline(), ctx));
if (!result.isSuccess()) {
context.popSnapshot(snapshot1);
snapshot1 = context.pushSnapshot();
// Try again with the off hand
offHandSlot.peek().ifPresent(stack -> frame.addContext(ContextKeys.USED_ITEM_STACK, stack));
frame.addContext(ContextKeys.USED_SLOT, offHandSlot);
frame.addContext(ContextKeys.INTERACTION_HAND, HandTypes.OFF_HAND);
result = context.process(blockType.getPipeline().pipeline(InteractWithBlockBehavior.class), (ctx, behavior) -> behavior.tryInteract(blockType.getPipeline(), ctx));
}
if (result.isSuccess()) {
snapshot1 = context.pushSnapshot();
// We can still continue, doing other operations
if (result == BehaviorResult.CONTINUE) {
handleMainHandItemInteraction(context, snapshot1);
}
context.accept();
return;
}
context.popSnapshot(snapshot1);
snapshot1 = context.pushSnapshot();
if (result.isSuccess()) {
snapshot1 = context.pushSnapshot();
// We can still continue, doing other operations
if (result == BehaviorResult.CONTINUE) {
handleOffHandItemInteraction(context, snapshot1);
}
context.accept();
return;
}
context.popSnapshot(snapshot1);
}
handleItemInteraction(context, snapshot);
}
}
use of org.spongepowered.api.block.BlockType 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.spongepowered.api.block.BlockType 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;
}
}
use of org.spongepowered.api.block.BlockType in project LanternServer by LanternPowered.
the class ProcessorPlayOutParticleEffect method getBlockState.
private static int getBlockState(LanternParticleEffect effect, Optional<BlockState> defaultBlockState) {
final Optional<BlockState> blockState = effect.getOption(ParticleOptions.BLOCK_STATE);
if (blockState.isPresent()) {
return BlockRegistryModule.get().getStateInternalIdAndData(blockState.get());
} else {
final Optional<ItemStackSnapshot> optSnapshot = effect.getOption(ParticleOptions.ITEM_STACK_SNAPSHOT);
if (optSnapshot.isPresent()) {
final ItemStackSnapshot snapshot = optSnapshot.get();
final Optional<BlockType> blockType = snapshot.getType().getBlock();
if (blockType.isPresent()) {
final BlockState state;
if (blockType.get().getDefaultState().getTraits().isEmpty()) {
state = blockType.get().getDefaultState();
} else {
final BlockState.Builder builder = BlockState.builder().blockType(blockType.get());
// noinspection unchecked
snapshot.getValues().forEach(value -> builder.add((Key) value.getKey(), value.get()));
state = builder.build();
}
return BlockRegistryModule.get().getStateInternalIdAndData(state);
} else {
return 0;
}
} else {
return BlockRegistryModule.get().getStateInternalIdAndData(defaultBlockState.get());
}
}
}
Aggregations