use of org.spongepowered.api.block.BlockSnapshot in project Almura by AlmuraDev.
the class SignEditFeature method interact.
@Listener
public void interact(final InteractBlockEvent.Secondary.MainHand event, @Root final Player player) {
if (!player.require(Keys.IS_SNEAKING)) {
return;
}
final BlockSnapshot snapshot = event.getTargetBlock();
final BlockType type = snapshot.getState().getType();
if (type == BlockTypes.STANDING_SIGN || type == BlockTypes.WALL_SIGN) {
snapshot.getLocation().flatMap(Location::getTileEntity).ifPresent((be) -> {
if (be instanceof TileEntitySign) {
// Todo: this needs to be handled within NetHandlerPlayClient, specifically: handleSignEditorOpen
// ((TileEntitySign) be).setEditable(true);
((TileEntitySign) be).setPlayer((EntityPlayer) player);
((EntityPlayerMP) player).connection.sendPacket(new SPacketSignEditorOpen(VecHelper.toBlockPos(snapshot.getPosition())));
}
});
}
}
use of org.spongepowered.api.block.BlockSnapshot in project SpongeCommon by SpongePowered.
the class BlockEventBasedTransaction method markCancelledTransactions.
@Override
public final boolean markCancelledTransactions(final ChangeBlockEvent.All event, final ImmutableList<? extends GameTransaction<ChangeBlockEvent.All>> blockTransactions) {
boolean cancelledAny = false;
if (event.isCancelled()) {
event.transactions().forEach(BlockTransaction::invalidate);
}
for (final Transaction<BlockSnapshot> transaction : event.transactions()) {
if (!transaction.isValid()) {
cancelledAny = true;
for (final GameTransaction<ChangeBlockEvent.All> gameTransaction : blockTransactions) {
final BlockEventBasedTransaction blockTransaction = (BlockEventBasedTransaction) gameTransaction;
final Vector3i position = transaction.original().position();
final BlockPos affectedPosition = blockTransaction.affectedPosition;
if (position.x() == affectedPosition.getX() && position.y() == affectedPosition.getY() && position.z() == affectedPosition.getZ()) {
gameTransaction.markCancelled();
}
}
}
}
return cancelledAny;
}
use of org.spongepowered.api.block.BlockSnapshot in project SpongeCommon by SpongePowered.
the class TrackingUtil method randomTickBlock.
@SuppressWarnings("rawtypes")
public static void randomTickBlock(final TrackedWorldBridge mixinWorld, final net.minecraft.world.level.block.state.BlockState state, final BlockPos pos, final Random random) {
final ServerLevel world = (ServerLevel) mixinWorld;
final org.spongepowered.api.world.server.ServerWorld apiWorld = (org.spongepowered.api.world.server.ServerWorld) world;
if (ShouldFire.TICK_BLOCK_EVENT) {
final BlockSnapshot currentTickBlock = mixinWorld.bridge$createSnapshot(state, pos, BlockChangeFlags.NONE);
final TickBlockEvent event = SpongeEventFactory.createTickBlockEventRandom(PhaseTracker.getCauseStackManager().currentCause(), currentTickBlock);
SpongeCommon.post(event);
if (event.isCancelled()) {
return;
}
}
final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world(apiWorld).position(pos.getX(), pos.getY(), pos.getZ()).state((BlockState) state).build();
final BlockTickContext phaseContext = TickPhase.Tick.RANDOM_BLOCK.createPhaseContext(PhaseTracker.SERVER).source(locatable);
// We have to associate any notifiers in case of scheduled block updates from other sources
final PhaseContext<@NonNull ?> currentContext = PhaseTracker.getInstance().getPhaseContext();
currentContext.appendNotifierPreBlockTick(world, pos, phaseContext);
// Now actually switch to the new phase
try (final PhaseContext<@NonNull ?> context = phaseContext) {
context.buildAndSwitch();
PhaseTracker.LOGGER.trace(TrackingUtil.BLOCK_TICK, "Wrapping Random Block Tick: {}", state);
state.randomTick(world, pos, random);
} catch (final Exception | NoClassDefFoundError e) {
PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, phaseContext);
}
}
use of org.spongepowered.api.block.BlockSnapshot in project SpongeCommon by SpongePowered.
the class TrackingUtil method updateTickBlock.
@SuppressWarnings("rawtypes")
public static void updateTickBlock(final TrackedWorldBridge mixinWorld, final net.minecraft.world.level.block.state.BlockState block, final BlockPos pos, final Random random) {
final ServerLevel world = (ServerLevel) mixinWorld;
final org.spongepowered.api.world.server.ServerWorld apiWorld = (org.spongepowered.api.world.server.ServerWorld) world;
if (ShouldFire.TICK_BLOCK_EVENT) {
final BlockSnapshot snapshot = mixinWorld.bridge$createSnapshot(block, pos, BlockChangeFlags.NONE);
final TickBlockEvent event = SpongeEventFactory.createTickBlockEventScheduled(PhaseTracker.getCauseStackManager().currentCause(), snapshot);
SpongeCommon.post(event);
if (event.isCancelled()) {
return;
}
}
final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world(apiWorld).position(pos.getX(), pos.getY(), pos.getZ()).state((BlockState) block).build();
final BlockTickContext phaseContext = TickPhase.Tick.BLOCK.createPhaseContext(PhaseTracker.SERVER).source(locatable);
// We have to associate any notifiers in case of scheduled block updates from other sources
final PhaseContext<@NonNull ?> currentContext = PhaseTracker.getInstance().getPhaseContext();
currentContext.appendNotifierPreBlockTick(world, pos, phaseContext);
try (final PhaseContext<@NonNull ?> context = phaseContext;
final Timing timing = ((TimingBridge) block.getBlock()).bridge$timings()) {
timing.startTiming();
context.buildAndSwitch();
PhaseTracker.LOGGER.trace(TrackingUtil.BLOCK_TICK, () -> "Wrapping Block Tick: " + block.toString());
block.tick(world, pos, random);
} catch (final Exception | NoClassDefFoundError e) {
PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, phaseContext);
}
}
use of org.spongepowered.api.block.BlockSnapshot in project SpongeCommon by SpongePowered.
the class BedBlockMixin method impl$onUseBed.
@Inject(method = "use", at = @At(value = "HEAD"), cancellable = true)
private void impl$onUseBed(final BlockState param0, final Level param1, final BlockPos param2, final Player param3, final InteractionHand param4, final BlockHitResult param5, final CallbackInfoReturnable<InteractionResult> cir) {
if (!param1.isClientSide) {
final Cause currentCause = Sponge.server().causeStackManager().currentCause();
final BlockPos bedLocation = param5.getBlockPos();
final BlockSnapshot snapshot = ((ServerWorld) param1).createSnapshot(bedLocation.getX(), bedLocation.getY(), bedLocation.getZ());
if (Sponge.eventManager().post(SpongeEventFactory.createSleepingEventPre(currentCause, snapshot, (Living) param3))) {
cir.setReturnValue(InteractionResult.CONSUME);
}
}
}
Aggregations