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