use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callNotifyNeighborEvent.
@SuppressWarnings("rawtypes")
public static NotifyNeighborBlockEvent callNotifyNeighborEvent(World world, BlockPos sourcePos, EnumSet notifiedSides) {
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData peek = phaseTracker.getCurrentPhaseData();
final PhaseContext<?> context = peek.context;
// Don't fire notify events during world gen or while restoring
if (peek.state.isWorldGeneration() || peek.state == State.RESTORING_BLOCKS) {
return null;
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
final BlockState blockstate = (BlockState) ((net.minecraft.world.World) world).getBlockState(sourcePos);
final LocatableBlock locatable = LocatableBlock.builder().location(new Location<World>(world, sourcePos.getX(), sourcePos.getY(), sourcePos.getZ())).state(blockstate).build();
if (context.getNotifier().isPresent()) {
context.addNotifierAndOwnerToCauseStack();
} else {
final IMixinChunk mixinChunk = (IMixinChunk) ((WorldServer) world).getChunkFromBlockCoords(sourcePos);
mixinChunk.getBlockNotifier(sourcePos).ifPresent(user -> Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, user));
mixinChunk.getBlockOwner(sourcePos).ifPresent(owner -> Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner));
}
Sponge.getCauseStackManager().pushCause(locatable);
final Map<Direction, BlockState> neighbors = new HashMap<>();
for (Object obj : notifiedSides) {
EnumFacing notifiedSide = (EnumFacing) obj;
BlockPos offset = sourcePos.offset(notifiedSide);
Direction direction = DirectionFacingProvider.getInstance().getKey(notifiedSide).get();
Location<World> location = new Location<>(world, VecHelper.toVector3i(offset));
if (location.getBlockY() >= 0 && location.getBlockY() <= 255) {
neighbors.put(direction, location.getBlock());
}
}
// ImmutableMap<Direction, BlockState> originalNeighbors =
// ImmutableMap.copyOf(neighbors);
NotifyNeighborBlockEvent event = SpongeEventFactory.createNotifyNeighborBlockEvent(Sponge.getCauseStackManager().getCurrentCause(), neighbors, neighbors);
SpongeImpl.postEvent(event);
return event;
}
}
use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callChangeBlockEventModifyLiquidBreak.
public static ChangeBlockEvent.Break callChangeBlockEventModifyLiquidBreak(net.minecraft.world.World worldIn, BlockPos pos, IBlockState state, int flags) {
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData data = phaseTracker.getCurrentPhaseData();
BlockState fromState = BlockUtil.fromNative(worldIn.getBlockState(pos));
BlockState toState = BlockUtil.fromNative(state);
User owner = data.context.getOwner().orElse(null);
User notifier = data.context.getNotifier().orElse(null);
Object source = data.context.getSource(LocatableBlock.class).orElse(null);
if (source == null) {
// Fallback
source = worldIn;
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(source);
Sponge.getCauseStackManager().addContext(EventContextKeys.LIQUID_BREAK, (World) worldIn);
if (owner != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
}
if (notifier != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
}
WorldProperties world = ((World) worldIn).getProperties();
Vector3i position = new Vector3i(pos.getX(), pos.getY(), pos.getZ());
Transaction<BlockSnapshot> transaction = new Transaction<>(BlockSnapshot.builder().blockState(fromState).world(world).position(position).build(), BlockSnapshot.builder().blockState(toState).world(world).position(position).build());
ChangeBlockEvent.Break event = SpongeEventFactory.createChangeBlockEventBreak(Sponge.getCauseStackManager().getCurrentCause(), Collections.singletonList(transaction));
SpongeImpl.postEvent(event);
return event;
}
}
use of org.spongepowered.api.block.BlockState 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();
}
}
use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.
the class MixinBlockLeaves method getStateWithValue.
@Override
public <E> Optional<BlockState> getStateWithValue(IBlockState blockState, Key<? extends BaseValue<E>> key, E value) {
if (key.equals(Keys.TREE_TYPE)) {
final TreeType treeType = (TreeType) value;
final BlockPlanks.EnumType type = TreeTypeResolver.getFor(treeType);
if (blockState.getBlock() instanceof BlockOldLeaf) {
if (treeType.equals(TreeTypes.OAK) || treeType.equals(TreeTypes.BIRCH) || treeType.equals(TreeTypes.SPRUCE) || treeType.equals(TreeTypes.JUNGLE)) {
return Optional.of((BlockState) blockState.withProperty(BlockOldLeaf.VARIANT, type));
}
} else if (blockState.getBlock() instanceof BlockNewLeaf) {
if (treeType.equals(TreeTypes.ACACIA) || treeType.equals(TreeTypes.DARK_OAK)) {
return Optional.of((BlockState) blockState.withProperty(BlockNewLeaf.VARIANT, type));
}
}
return Optional.empty();
}
if (key.equals(Keys.DECAYABLE)) {
final boolean decayable = (Boolean) value;
return Optional.of((BlockState) blockState.withProperty(BlockLeaves.DECAYABLE, decayable));
}
return super.getStateWithValue(blockState, key, value);
}
use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.
the class RepresentedBlockValueProcessor method removeFrom.
@Override
public DataTransactionResult removeFrom(ValueContainer<?> container) {
if (container instanceof EntityMinecart) {
EntityMinecart cart = (EntityMinecart) container;
ImmutableValue<BlockState> block = new ImmutableSpongeValue<>(Keys.REPRESENTED_BLOCK, (BlockState) cart.getDisplayTile());
cart.setHasDisplayTile(false);
return DataTransactionResult.builder().replace(block).build();
}
return DataTransactionResult.failNoData();
}
Aggregations