Search in sources :

Example 1 with BlockTransactionReceipt

use of org.spongepowered.api.block.transaction.BlockTransactionReceipt in project SpongeCommon by SpongePowered.

the class BlockTransactionType method consumeEventsAndMarker.

@Override
protected void consumeEventsAndMarker(PhaseContext<@NonNull ?> context, final Collection<? extends ChangeBlockEvent.All> changeBlockEvents) {
    final Multimap<ResourceKey, ChangeBlockEvent.All> eventsByWorld = LinkedListMultimap.create();
    changeBlockEvents.forEach(event -> eventsByWorld.put(event.world().key(), event));
    eventsByWorld.asMap().forEach((key, events) -> {
        final Optional<ServerWorld> serverWorld = ((SpongeServer) SpongeCommon.server()).worldManager().world(key);
        if (!serverWorld.isPresent()) {
            return;
        }
        final ListMultimap<BlockPos, SpongeBlockSnapshot> positions = LinkedListMultimap.create();
        // Gather transactions that were valid
        events.stream().filter(event -> !event.isCancelled()).flatMap(event -> event.transactions().stream()).filter(BlockTransaction::isValid).forEach(transactions -> {
            // Then "put" the most recent transactions such that we have a complete rebuild of
            // each position according to what originally existed and then
            // the ultimate final block on that position
            final SpongeBlockSnapshot original = (SpongeBlockSnapshot) transactions.original();
            positions.put(original.getBlockPos(), original);
            positions.put(original.getBlockPos(), (SpongeBlockSnapshot) transactions.finalReplacement());
        });
        // just return.
        if (positions.isEmpty()) {
            return;
        }
        final ImmutableList<BlockTransactionReceipt> transactions = positions.asMap().values().stream().map(spongeBlockSnapshots -> {
            final List<SpongeBlockSnapshot> snapshots = new ArrayList<>(spongeBlockSnapshots);
            if (snapshots.isEmpty() || snapshots.size() < 2) {
                // Error case
                return Optional.<BlockTransactionReceipt>empty();
            }
            final SpongeBlockSnapshot original = snapshots.get(0);
            final SpongeBlockSnapshot result = snapshots.get(snapshots.size() - 1);
            final Operation operation = context.getBlockOperation(original, result);
            final BlockTransactionReceipt eventTransaction = new BlockTransactionReceipt(original, result, operation);
            context.postBlockTransactionApplication(original.blockChange, eventTransaction);
            return Optional.of(eventTransaction);
        }).filter(Optional::isPresent).map(Optional::get).collect(ImmutableList.toImmutableList());
        final Cause cause = PhaseTracker.getInstance().currentCause();
        SpongeCommon.post(SpongeEventFactory.createChangeBlockEventPost(cause, transactions, serverWorld.get()));
    });
}
Also used : LinkedListMultimap(com.google.common.collect.LinkedListMultimap) NonNull(org.checkerframework.checker.nullness.qual.NonNull) ServerWorld(org.spongepowered.api.world.server.ServerWorld) ListMultimap(com.google.common.collect.ListMultimap) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) SpongeServer(org.spongepowered.common.SpongeServer) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Collection(java.util.Collection) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Multimap(com.google.common.collect.Multimap) Cause(org.spongepowered.api.event.Cause) ArrayList(java.util.ArrayList) BlockTransactionReceipt(org.spongepowered.api.block.transaction.BlockTransactionReceipt) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BlockPos(net.minecraft.core.BlockPos) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) BlockTransaction(org.spongepowered.api.block.transaction.BlockTransaction) ResourceKey(org.spongepowered.api.ResourceKey) Optional(java.util.Optional) Operation(org.spongepowered.api.block.transaction.Operation) BlockTransactionReceipt(org.spongepowered.api.block.transaction.BlockTransactionReceipt) Optional(java.util.Optional) Operation(org.spongepowered.api.block.transaction.Operation) ResourceKey(org.spongepowered.api.ResourceKey) ServerWorld(org.spongepowered.api.world.server.ServerWorld) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) Cause(org.spongepowered.api.event.Cause) BlockPos(net.minecraft.core.BlockPos) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with BlockTransactionReceipt

use of org.spongepowered.api.block.transaction.BlockTransactionReceipt in project SpongeCommon by SpongePowered.

the class TrackingUtil method associateTrackerToTarget.

public static void associateTrackerToTarget(final BlockChange blockChange, final BlockTransactionReceipt receipt, final UUID uuid) {
    final BlockSnapshot finalSnapshot = receipt.finalBlock();
    final SpongeBlockSnapshot spongeSnapshot = (SpongeBlockSnapshot) finalSnapshot;
    final BlockPos pos = spongeSnapshot.getBlockPos();
    final Block block = ((net.minecraft.world.level.block.state.BlockState) spongeSnapshot.state()).getBlock();
    spongeSnapshot.getServerWorld().map(world -> world.getChunkAt(pos)).map(chunk -> (LevelChunkBridge) chunk).ifPresent(spongeChunk -> {
        final PlayerTracker.Type trackerType = blockChange == BlockChange.PLACE ? PlayerTracker.Type.CREATOR : PlayerTracker.Type.NOTIFIER;
        spongeChunk.bridge$addTrackedBlockPosition(block, pos, uuid, trackerType);
    });
}
Also used : ViewableInventoryBridge(org.spongepowered.common.bridge.world.inventory.ViewableInventoryBridge) Level(org.apache.logging.log4j.Level) Random(java.util.Random) SpongeCommonEventFactory(org.spongepowered.common.event.SpongeCommonEventFactory) RedstoneTorchBlock(net.minecraft.world.level.block.RedstoneTorchBlock) BlockTransactionReceipt(org.spongepowered.api.block.transaction.BlockTransactionReceipt) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TrackedWorldBridge(org.spongepowered.common.bridge.world.TrackedWorldBridge) ActiveChunkReferantBridge(org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TickPhase(org.spongepowered.common.event.tracking.phase.tick.TickPhase) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) TimingBridge(org.spongepowered.common.bridge.TimingBridge) Transaction(org.spongepowered.api.data.Transaction) LocatableBlock(org.spongepowered.api.world.LocatableBlock) BlockEntityBridge(org.spongepowered.common.bridge.world.level.block.entity.BlockEntityBridge) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) Set(java.util.Set) UUID(java.util.UUID) BlockChange(org.spongepowered.common.world.BlockChange) TickableBlockEntity(net.minecraft.world.level.block.entity.TickableBlockEntity) BlockState(org.spongepowered.api.block.BlockState) MarkerManager(org.apache.logging.log4j.MarkerManager) Keys(org.spongepowered.api.data.Keys) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) BlockPos(net.minecraft.core.BlockPos) FluidState(net.minecraft.world.level.material.FluidState) BlockEventData(net.minecraft.world.level.BlockEventData) Optional(java.util.Optional) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) TrackableBlockEventDataBridge(org.spongepowered.common.bridge.world.level.TrackableBlockEventDataBridge) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) NonNull(org.checkerframework.checker.nullness.qual.NonNull) PrettyPrinter(org.spongepowered.common.util.PrettyPrinter) ServerLevel(net.minecraft.server.level.ServerLevel) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ServerPlayer(net.minecraft.server.level.ServerPlayer) BlockChangeFlag(org.spongepowered.api.world.BlockChangeFlag) TrackableBridge(org.spongepowered.common.bridge.TrackableBridge) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock) TrackedLevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.TrackedLevelChunkBridge) FluidTickContext(org.spongepowered.common.event.tracking.phase.tick.FluidTickContext) Marker(org.apache.logging.log4j.Marker) Nullable(org.checkerframework.checker.nullness.qual.Nullable) RedstoneLampBlock(net.minecraft.world.level.block.RedstoneLampBlock) Timing(co.aikar.timings.Timing) BlockTickContext(org.spongepowered.common.event.tracking.phase.tick.BlockTickContext) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) ShouldFire(org.spongepowered.common.event.ShouldFire) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) SpongeCommon(org.spongepowered.common.SpongeCommon) Entity(org.spongepowered.api.entity.Entity) TickBlockEvent(org.spongepowered.api.event.block.TickBlockEvent) Consumer(java.util.function.Consumer) BlockChangeFlags(org.spongepowered.api.world.BlockChangeFlags) VecHelper(org.spongepowered.common.util.VecHelper) Block(net.minecraft.world.level.block.Block) BlockEventTickContext(org.spongepowered.common.event.tracking.phase.tick.BlockEventTickContext) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockState(org.spongepowered.api.block.BlockState) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) RedstoneTorchBlock(net.minecraft.world.level.block.RedstoneTorchBlock) LocatableBlock(org.spongepowered.api.world.LocatableBlock) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock) RedstoneLampBlock(net.minecraft.world.level.block.RedstoneLampBlock) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) TrackedLevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.TrackedLevelChunkBridge)

Aggregations

List (java.util.List)2 Optional (java.util.Optional)2 BlockPos (net.minecraft.core.BlockPos)2 NonNull (org.checkerframework.checker.nullness.qual.NonNull)2 BlockTransactionReceipt (org.spongepowered.api.block.transaction.BlockTransactionReceipt)2 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)2 SpongeCommon (org.spongepowered.common.SpongeCommon)2 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)2 Timing (co.aikar.timings.Timing)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 LinkedListMultimap (com.google.common.collect.LinkedListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1 Multimap (com.google.common.collect.Multimap)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Random (java.util.Random)1 Set (java.util.Set)1 UUID (java.util.UUID)1