use of net.minecraft.block.state.BlockPistonStructureHelper in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method handlePistonEvent.
/**
* This simulates the blocks a piston moves and calls the event for saner
* debugging.
*
* @return if the event was cancelled
*/
public static boolean handlePistonEvent(IMixinWorldServer world, WorldServer.ServerBlockEventList list, Object obj, BlockPos pos, Block blockIn, int eventId, int eventParam) {
boolean extending = (eventId == 0);
final IBlockState blockstate = ((net.minecraft.world.World) world).getBlockState(pos);
EnumFacing direction = blockstate.getValue(BlockDirectional.FACING);
final LocatableBlock locatable = LocatableBlock.builder().location(new Location<>((World) world, pos.getX(), pos.getY(), pos.getZ())).state((BlockState) blockstate).build();
// Sets toss out duplicate values (even though there shouldn't be any)
HashSet<Location<org.spongepowered.api.world.World>> locations = new HashSet<>();
locations.add(new Location<>((World) world, pos.getX(), pos.getY(), pos.getZ()));
BlockPistonStructureHelper movedBlocks = new BlockPistonStructureHelper((WorldServer) world, pos, direction, extending);
// calculates blocks to be moved
movedBlocks.canMove();
Stream.concat(movedBlocks.getBlocksToMove().stream(), movedBlocks.getBlocksToDestroy().stream()).map(block -> new Location<>((World) world, block.getX(), block.getY(), block.getZ())).collect(// SUPER
Collectors.toCollection(() -> locations));
// If the piston is extending and there are no blocks to destroy, add the offset location for protection purposes
if (extending && movedBlocks.getBlocksToDestroy().isEmpty()) {
final List<BlockPos> movedPositions = movedBlocks.getBlocksToMove();
BlockPos offsetPos;
// If there are no blocks to move, add the offset of piston
if (movedPositions.isEmpty()) {
offsetPos = pos.offset(direction);
} else {
// Add the offset of last block set to move
offsetPos = movedPositions.get(movedPositions.size() - 1).offset(direction);
}
locations.add(new Location<>((World) world, offsetPos.getX(), offsetPos.getY(), offsetPos.getZ()));
}
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
if (extending) {
Sponge.getCauseStackManager().addContext(EventContextKeys.PISTON_EXTEND, world.asSpongeWorld());
} else {
Sponge.getCauseStackManager().addContext(EventContextKeys.PISTON_RETRACT, world.asSpongeWorld());
}
return SpongeCommonEventFactory.callChangeBlockEventPre(world, ImmutableList.copyOf(locations), locatable).isCancelled();
}
}
Aggregations