Search in sources :

Example 6 with BlockSnapshotBuilder

use of org.lanternpowered.server.block.BlockSnapshotBuilder in project LanternServer by LanternPowered.

the class BehaviorContextImpl method transformBlockChanges.

@Override
public void transformBlockChanges(BiConsumer<BlockSnapshot, BlockSnapshotBuilder> snapshotTransformer) {
    checkNotNull(snapshotTransformer, "snapshotTransformer");
    final Set<BlockSnapshot> newPositionlessBlockSnapshots = new HashSet<>();
    final Map<Location<World>, BlockSnapshot> newBlockSnapshots = new HashMap<>();
    final BlockSnapshotBuilder builder = BlockSnapshotBuilder.createPositionless();
    for (BlockSnapshot blockSnapshot : Iterables.concat(this.positionlessBlockSnapshots, this.blockSnapshots.values())) {
        builder.from(blockSnapshot);
        snapshotTransformer.accept(blockSnapshot, builder);
        final BlockSnapshot result = builder.build();
        if (((LanternBlockSnapshot) result).isPositionless()) {
            newPositionlessBlockSnapshots.add(result);
        } else {
            final Location<World> loc = blockSnapshot.getLocation().orElseThrow(() -> new IllegalArgumentException("Unable to retrieve the location of the block snapshot, is the world loaded?"));
            checkArgument(newBlockSnapshots.putIfAbsent(loc, result) == null, "There is already a block snapshot present for the location: %s", loc);
        }
    }
    this.positionlessBlockSnapshots = newPositionlessBlockSnapshots;
    this.blockSnapshots = newBlockSnapshots;
}
Also used : LanternBlockSnapshot(org.lanternpowered.server.block.LanternBlockSnapshot) HashMap(java.util.HashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) LanternBlockSnapshot(org.lanternpowered.server.block.LanternBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) HashSet(java.util.HashSet) Location(org.spongepowered.api.world.Location) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder)

Example 7 with BlockSnapshotBuilder

use of org.lanternpowered.server.block.BlockSnapshotBuilder in project LanternServer by LanternPowered.

the class BehaviorContextImpl method transformBlockChangesWithFunction.

@Override
public void transformBlockChangesWithFunction(Function<BlockSnapshot, BlockSnapshot> snapshotTransformer) {
    checkNotNull(snapshotTransformer, "snapshotTransformer");
    final Set<BlockSnapshot> newPositionlessBlockSnapshots = new HashSet<>();
    final Map<Location<World>, BlockSnapshot> newBlockSnapshots = new HashMap<>();
    final BlockSnapshotBuilder builder = BlockSnapshotBuilder.createPositionless();
    for (BlockSnapshot blockSnapshot : Iterables.concat(this.positionlessBlockSnapshots, this.blockSnapshots.values())) {
        final BlockSnapshot newSnapshot = snapshotTransformer.apply(blockSnapshot);
        if (newSnapshot == null) {
            continue;
        }
        final BlockSnapshot result = builder.build();
        if (((LanternBlockSnapshot) result).isPositionless()) {
            newPositionlessBlockSnapshots.add(result);
        } else {
            final Location<World> loc = blockSnapshot.getLocation().orElseThrow(() -> new IllegalArgumentException("Unable to retrieve the location of the block snapshot, is the world loaded?"));
            checkArgument(newBlockSnapshots.putIfAbsent(loc, blockSnapshot) == null, "There is already a block snapshot present for the location: %s", loc);
        }
    }
    this.positionlessBlockSnapshots = newPositionlessBlockSnapshots;
    this.blockSnapshots = newBlockSnapshots;
}
Also used : LanternBlockSnapshot(org.lanternpowered.server.block.LanternBlockSnapshot) HashMap(java.util.HashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) LanternBlockSnapshot(org.lanternpowered.server.block.LanternBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) HashSet(java.util.HashSet) Location(org.spongepowered.api.world.Location) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder)

Example 8 with BlockSnapshotBuilder

use of org.lanternpowered.server.block.BlockSnapshotBuilder 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

BlockSnapshotBuilder (org.lanternpowered.server.block.BlockSnapshotBuilder)8 World (org.spongepowered.api.world.World)7 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)4 Direction (org.spongepowered.api.util.Direction)4 Location (org.spongepowered.api.world.Location)3 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Behavior (org.lanternpowered.server.behavior.Behavior)2 BehaviorContext (org.lanternpowered.server.behavior.BehaviorContext)2 BehaviorResult (org.lanternpowered.server.behavior.BehaviorResult)2 ContextKeys (org.lanternpowered.server.behavior.ContextKeys)2 BehaviorPipeline (org.lanternpowered.server.behavior.pipeline.BehaviorPipeline)2 LanternBlockSnapshot (org.lanternpowered.server.block.LanternBlockSnapshot)2 BlockState (org.spongepowered.api.block.BlockState)2 Keys (org.spongepowered.api.data.key.Keys)2 ReplaceableProperty (org.spongepowered.api.data.property.block.ReplaceableProperty)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Vector3i (com.flowpowered.math.vector.Vector3i)1 Optional (java.util.Optional)1