Search in sources :

Example 41 with Transaction

use of org.spongepowered.api.data.Transaction in project Skree by Skelril.

the class BuildWorldWrapper method onBlockBreak.

@Listener
public void onBlockBreak(ChangeBlockEvent.Break event, @Named(NamedCause.SOURCE) Entity srcEnt) {
    if (!isApplicable(srcEnt)) {
        return;
    }
    List<Transaction<BlockSnapshot>> transactions = event.getTransactions();
    for (Transaction<BlockSnapshot> block : transactions) {
        BlockSnapshot original = block.getOriginal();
        if (original.getCreator().isPresent()) {
            continue;
        }
        Optional<Location<World>> optLoc = original.getLocation();
        if (!optLoc.isPresent()) {
            continue;
        }
        Location<World> loc = optLoc.get();
        BlockState state = original.getState();
        // Prevent item dupe glitch by removing the position before subsequent breaks
        markedOrePoints.remove(loc);
        if (config.getDropModification().blocks(state)) {
            markedOrePoints.add(loc);
        }
    }
}
Also used : BlockState(org.spongepowered.api.block.BlockState) Transaction(org.spongepowered.api.data.Transaction) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Example 42 with Transaction

use of org.spongepowered.api.data.Transaction in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method createChangeBlockEventBreak.

public static ChangeBlockEvent.Break createChangeBlockEventBreak(BlockEvent.BreakEvent forgeEvent) {
    final BlockPos pos = forgeEvent.getPos();
    final net.minecraft.world.World world = forgeEvent.getWorld();
    if (world.isRemote) {
        return null;
    }
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final PhaseData data = phaseTracker.getCurrentPhaseData();
    BlockSnapshot originalSnapshot = ((World) forgeEvent.getWorld()).createSnapshot(pos.getX(), pos.getY(), pos.getZ());
    BlockSnapshot finalSnapshot = BlockTypes.AIR.getDefaultState().snapshotFor(new Location<>((World) world, VecHelper.toVector3d(pos)));
    ImmutableList<Transaction<BlockSnapshot>> blockSnapshots = new ImmutableList.Builder<Transaction<BlockSnapshot>>().add(new Transaction<>(originalSnapshot, finalSnapshot)).build();
    User owner = data.context.getOwner().orElse(null);
    User notifier = data.context.getNotifier().orElse(null);
    EntityPlayer player = forgeEvent.getPlayer();
    if (SpongeImplHooks.isFakePlayer(player)) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.FAKE_PLAYER, EntityUtil.toPlayer(player));
    } else if (Sponge.getCauseStackManager().getCurrentCause() == null) {
        Sponge.getCauseStackManager().pushCause(player);
    }
    if (owner != null) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
        if (Sponge.getCauseStackManager().getCurrentCause() == null) {
            Sponge.getCauseStackManager().pushCause(owner);
        }
    } else {
        Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, (User) player);
        if (Sponge.getCauseStackManager().getCurrentCause() == null) {
            Sponge.getCauseStackManager().pushCause(player);
        }
    }
    if (notifier != null) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
    }
    Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_BREAK, (World) world);
    return SpongeEventFactory.createChangeBlockEventBreak(Sponge.getCauseStackManager().getCurrentCause(), blockSnapshots);
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) User(org.spongepowered.api.entity.living.player.User) IMixinBlockSnapshot(org.spongepowered.mod.interfaces.IMixinBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Transaction(org.spongepowered.api.data.Transaction) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos)

Example 43 with Transaction

use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.

the class OpenMenuTransaction method generateEvent.

@Override
public Optional<InteractContainerEvent> generateEvent(final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent, final ImmutableList<GameTransaction<InteractContainerEvent>> gameTransactions, final Cause currentCause) {
    final ItemStackSnapshot resultingCursor = ItemStackUtil.snapshotOf(this.player.inventory.getCarried());
    final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(this.cursor, resultingCursor);
    final InteractContainerEvent.Open event = SpongeEventFactory.createInteractContainerEventOpen(currentCause, (Container) this.menu, cursorTransaction);
    return Optional.of(event);
}
Also used : GameTransaction(org.spongepowered.common.event.tracking.context.transaction.GameTransaction) Transaction(org.spongepowered.api.data.Transaction) InteractContainerEvent(org.spongepowered.api.event.item.inventory.container.InteractContainerEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Example 44 with Transaction

use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.

the class BlockEventBasedTransaction method generateEvent.

@Override
public final Optional<ChangeBlockEvent.All> generateEvent(final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent, final ImmutableList<GameTransaction<ChangeBlockEvent.All>> transactions, final Cause currentCause) {
    final Optional<ServerWorld> o = ((SpongeServer) SpongeCommon.server()).worldManager().world(this.worldKey);
    if (!o.isPresent()) {
        return Optional.empty();
    }
    final ListMultimap<BlockPos, SpongeBlockSnapshot> positions = LinkedListMultimap.create();
    for (final GameTransaction<@NonNull ?> transaction : transactions) {
        final BlockEventBasedTransaction blockTransaction = (BlockEventBasedTransaction) transaction;
        if (!positions.containsKey(blockTransaction.affectedPosition)) {
            positions.put(blockTransaction.affectedPosition, blockTransaction.getOriginalSnapshot());
        }
        if (blockTransaction.getResultingSnapshot() != null) {
            positions.put(blockTransaction.affectedPosition, blockTransaction.getResultingSnapshot());
        }
    }
    final ImmutableList<BlockTransaction> eventTransactions = positions.asMap().values().stream().map(spongeBlockSnapshots -> {
        final List<SpongeBlockSnapshot> snapshots = new ArrayList<>(spongeBlockSnapshots);
        if (snapshots.isEmpty() || snapshots.size() < 2) {
            // Error case
            return Optional.<BlockTransaction>empty();
        }
        final SpongeBlockSnapshot original = snapshots.get(0);
        final SpongeBlockSnapshot result = snapshots.get(snapshots.size() - 1);
        final ImmutableList<BlockSnapshot> intermediary;
        if (snapshots.size() > 2) {
            intermediary = ImmutableList.copyOf(snapshots.subList(1, snapshots.size() - 2));
        } else {
            intermediary = ImmutableList.of();
        }
        final Operation operation = context.getBlockOperation(original, result);
        final BlockTransaction eventTransaction = new BlockTransaction(original, result, intermediary, operation);
        return Optional.of(eventTransaction);
    }).filter(Optional::isPresent).map(Optional::get).collect(ImmutableList.toImmutableList());
    if (eventTransactions.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(SpongeEventFactory.createChangeBlockEventAll(currentCause, eventTransactions, o.get()));
}
Also used : 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) BlockState(net.minecraft.world.level.block.state.BlockState) TransactionTypes(org.spongepowered.common.event.tracking.context.transaction.type.TransactionTypes) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) BlockTransaction(org.spongepowered.api.block.transaction.BlockTransaction) ResourceKey(org.spongepowered.api.ResourceKey) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Transaction(org.spongepowered.api.data.Transaction) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) SpongeCommon(org.spongepowered.common.SpongeCommon) Cause(org.spongepowered.api.event.Cause) List(java.util.List) GameTransaction(org.spongepowered.common.event.tracking.context.transaction.GameTransaction) WorldBasedTransaction(org.spongepowered.common.event.tracking.context.transaction.world.WorldBasedTransaction) BlockPos(net.minecraft.core.BlockPos) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) Optional(java.util.Optional) Operation(org.spongepowered.api.block.transaction.Operation) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Vector3i(org.spongepowered.math.vector.Vector3i) Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) Operation(org.spongepowered.api.block.transaction.Operation) ServerWorld(org.spongepowered.api.world.server.ServerWorld) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockPos(net.minecraft.core.BlockPos) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BlockTransaction(org.spongepowered.api.block.transaction.BlockTransaction)

Example 45 with Transaction

use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.

the class InventoryEventFactory method callEnchantEventEnchantPost.

public static EnchantItemEvent.Post callEnchantEventEnchantPost(Player playerIn, EnchantmentMenu container, SlotTransaction enchantedItem, SlotTransaction lapisItem, int option, int seed) {
    org.spongepowered.api.item.inventory.Container enchantContainer = ContainerUtil.fromNative(container);
    ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(playerIn.inventory.getCarried());
    Transaction<ItemStackSnapshot> cursorTrans = new Transaction<>(cursor, cursor);
    List<SlotTransaction> slotTrans = new ArrayList<>();
    slotTrans.add(lapisItem);
    slotTrans.add(enchantedItem);
    EnchantItemEvent.Post event = SpongeEventFactory.createEnchantItemEventPost(PhaseTracker.getCauseStackManager().currentCause(), enchantContainer, cursorTrans, enchantedItem.slot(), Optional.empty(), slotTrans, option, seed);
    SpongeCommon.post(event);
    PacketPhaseUtil.handleSlotRestore(playerIn, container, event.transactions(), event.isCancelled());
    PacketPhaseUtil.handleCursorRestore(playerIn, event.cursorTransaction());
    return event;
}
Also used : SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) Transaction(org.spongepowered.api.data.Transaction) EnchantItemEvent(org.spongepowered.api.event.item.inventory.EnchantItemEvent) ArrayList(java.util.ArrayList) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Aggregations

Transaction (org.spongepowered.api.data.Transaction)50 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)32 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)28 World (org.spongepowered.api.world.World)18 ArrayList (java.util.ArrayList)15 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)14 CauseStackManager (org.spongepowered.api.event.CauseStackManager)11 Entity (org.spongepowered.api.entity.Entity)10 ClickInventoryEvent (org.spongepowered.api.event.item.inventory.ClickInventoryEvent)10 ChangeBlockEvent (org.spongepowered.api.event.block.ChangeBlockEvent)9 List (java.util.List)8 BlockPos (net.minecraft.util.math.BlockPos)8 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)8 ItemStack (org.spongepowered.api.item.inventory.ItemStack)8 CauseStack (org.lanternpowered.server.event.CauseStack)7 Sponge (org.spongepowered.api.Sponge)7 Player (org.spongepowered.api.entity.living.player.Player)7 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)7 ImmutableList (com.google.common.collect.ImmutableList)6 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)6