Search in sources :

Example 11 with SpongeBlockSnapshot

use of org.spongepowered.common.block.SpongeBlockSnapshot in project SpongeCommon by SpongePowered.

the class AddTileEntity method captureState.

@Override
protected void captureState() {
    super.captureState();
    @Nullable final BlockEntity existingTile = this.chunkSupplier.get().getBlockEntity(this.affectedPosition, LevelChunk.EntityCreationType.CHECK);
    final SpongeBlockSnapshot added = TrackingUtil.createPooledSnapshot(this.originalState, this.affectedPosition, BlockChangeFlags.NONE, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT, this.added, this.worldSupplier, Optional::empty, Optional::empty);
    final SpongeBlockSnapshot existing = TrackingUtil.createPooledSnapshot(this.originalState, this.affectedPosition, BlockChangeFlags.NONE, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT, existingTile, this.worldSupplier, Optional::empty, Optional::empty);
    existing.blockChange = BlockChange.MODIFY;
    this.oldSnapshot = existing;
    this.addedSnapshot = added;
}
Also used : SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) Optional(java.util.Optional) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 12 with SpongeBlockSnapshot

use of org.spongepowered.common.block.SpongeBlockSnapshot in project SpongeCommon by SpongePowered.

the class ReplaceBlockEntity method captureState.

@Override
protected void captureState() {
    super.captureState();
    final BlockState currentState = this.worldSupplier.get().getBlockState(this.affectedPosition);
    final SpongeBlockSnapshot snapshot = TrackingUtil.createPooledSnapshot(currentState, this.affectedPosition, BlockChangeFlags.NONE, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT, this.removed, this.worldSupplier, Optional::empty, Optional::empty);
    snapshot.blockChange = BlockChange.MODIFY;
    this.removedSnapshot = snapshot;
}
Also used : SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockState(net.minecraft.world.level.block.state.BlockState) Optional(java.util.Optional)

Example 13 with SpongeBlockSnapshot

use of org.spongepowered.common.block.SpongeBlockSnapshot in project SpongeCommon by SpongePowered.

the class LocationBasedTickPhaseState method postBlockTransactionApplication.

@Override
public void postBlockTransactionApplication(final T context, final BlockChange blockChange, final BlockTransactionReceipt receipt) {
    // If we do not have a notifier at this point then there is no need to attempt to retrieve one from the chunk
    context.applyNotifierIfAvailable(user -> {
        final SpongeBlockSnapshot original = (SpongeBlockSnapshot) receipt.originalBlock();
        final Block block = (Block) original.state().type();
        final BlockPos changedBlockPos = original.getBlockPos();
        original.getServerWorld().ifPresent(worldServer -> {
            final LevelChunkBridge changedMixinChunk = (LevelChunkBridge) worldServer.getChunkAt(changedBlockPos);
            changedMixinChunk.bridge$addTrackedBlockPosition(block, changedBlockPos, user, PlayerTracker.Type.NOTIFIER);
            // to have new owners, which we can gather from the context.
            if (blockChange == BlockChange.PLACE) {
                context.applyOwnerIfAvailable(owner -> {
                    // We can do this when we check for notifiers because owners will always have a notifier set
                    // if not, well, file a bug report and find out the corner case that owners are set but not notifiers with block changes.
                    changedMixinChunk.bridge$addTrackedBlockPosition(block, changedBlockPos, owner, PlayerTracker.Type.CREATOR);
                });
            }
        });
    });
}
Also used : SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) LocatableBlock(org.spongepowered.api.world.LocatableBlock) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)

Example 14 with SpongeBlockSnapshot

use of org.spongepowered.common.block.SpongeBlockSnapshot in project SpongeCommon by SpongePowered.

the class BlockEventTickPhaseState method postBlockTransactionApplication.

@Override
public void postBlockTransactionApplication(final BlockEventTickContext context, final BlockChange blockChange, final BlockTransactionReceipt receipt) {
    final Block block = (Block) receipt.originalBlock().state().type();
    final SpongeBlockSnapshot original = (SpongeBlockSnapshot) receipt.originalBlock();
    final BlockPos changedBlockPos = original.getBlockPos();
    original.getServerWorld().ifPresent(worldServer -> {
        final LevelChunkBridge changedMixinChunk = (LevelChunkBridge) worldServer.getChunkAt(changedBlockPos);
        changedMixinChunk.bridge$getBlockCreatorUUID(changedBlockPos).ifPresent(owner -> changedMixinChunk.bridge$addTrackedBlockPosition(block, changedBlockPos, owner, PlayerTracker.Type.CREATOR));
        changedMixinChunk.bridge$getBlockNotifierUUID(changedBlockPos).ifPresent(user -> changedMixinChunk.bridge$addTrackedBlockPosition(block, changedBlockPos, user, PlayerTracker.Type.NOTIFIER));
    });
}
Also used : SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)

Example 15 with SpongeBlockSnapshot

use of org.spongepowered.common.block.SpongeBlockSnapshot in project SpongeCommon by SpongePowered.

the class TransactionSink method logBlockEvent.

@SuppressWarnings("ConstantConditions")
default void logBlockEvent(final BlockState state, final TrackedWorldBridge serverWorld, final BlockPos pos, final TrackableBlockEventDataBridge blockEvent) {
    final WeakReference<ServerLevel> worldRef = new WeakReference<>((ServerLevel) serverWorld);
    final Supplier<ServerLevel> worldSupplier = () -> Objects.requireNonNull(worldRef.get(), "ServerWorld dereferenced");
    @Nullable final BlockEntity tileEntity = ((ServerLevel) serverWorld).getBlockEntity(pos);
    final SpongeBlockSnapshot original = TrackingUtil.createPooledSnapshot(state, pos, BlockChangeFlags.NONE, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT, tileEntity, worldSupplier, Optional::empty, Optional::empty);
    original.blockChange = BlockChange.MODIFY;
    final AddBlockEventTransaction transaction = new AddBlockEventTransaction(original, blockEvent);
    this.logTransaction(transaction);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) Optional(java.util.Optional) WeakReference(java.lang.ref.WeakReference) AddBlockEventTransaction(org.spongepowered.common.event.tracking.context.transaction.block.AddBlockEventTransaction) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) RemoveBlockEntity(org.spongepowered.common.event.tracking.context.transaction.block.RemoveBlockEntity) ReplaceBlockEntity(org.spongepowered.common.event.tracking.context.transaction.block.ReplaceBlockEntity)

Aggregations

SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)20 Optional (java.util.Optional)10 IBlockState (net.minecraft.block.state.IBlockState)6 ServerLevel (net.minecraft.server.level.ServerLevel)6 Nullable (org.checkerframework.checker.nullness.qual.Nullable)6 BlockPos (net.minecraft.core.BlockPos)5 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)5 LevelChunkBridge (org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)5 WeakReference (java.lang.ref.WeakReference)4 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)4 NonNull (org.checkerframework.checker.nullness.qual.NonNull)4 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 UUID (java.util.UUID)3 WorldServer (net.minecraft.world.WorldServer)3 Block (net.minecraft.world.level.block.Block)3 BlockState (net.minecraft.world.level.block.state.BlockState)3 Entity (org.spongepowered.api.entity.Entity)3 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)3