Search in sources :

Example 21 with Transaction

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

the class InventoryEventFactory method callUpdateAnvilEvent.

public static UpdateAnvilEvent callUpdateAnvilEvent(final AnvilMenu anvil, final ItemStack slot1, final ItemStack slot2, final ItemStack result, final String name, final int levelCost, final int materialCost) {
    final Transaction<ItemStackSnapshot> transaction = new Transaction<>(ItemStackSnapshot.empty(), ItemStackUtil.snapshotOf(result));
    final UpdateAnvilEventCost costs = new UpdateAnvilEventCost(levelCost, materialCost);
    final UpdateAnvilEvent event = SpongeEventFactory.createUpdateAnvilEvent(PhaseTracker.getCauseStackManager().currentCause(), new Transaction<>(costs, costs), (Inventory) anvil, name, ItemStackUtil.snapshotOf(slot1), transaction, ItemStackUtil.snapshotOf(slot2));
    SpongeCommon.post(event);
    return event;
}
Also used : UpdateAnvilEvent(org.spongepowered.api.event.item.inventory.UpdateAnvilEvent) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) Transaction(org.spongepowered.api.data.Transaction) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Example 22 with Transaction

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

the class InventoryEventFactory method callEnchantEventEnchantmentList.

public static List<EnchantmentInstance> callEnchantEventEnchantmentList(EnchantmentMenu container, int seed, ItemStack itemStack, int option, int level, List<EnchantmentInstance> list) {
    List<Enchantment> enchList = Collections.unmodifiableList(SpongeRandomEnchantmentListBuilder.fromNative(list));
    org.spongepowered.api.item.inventory.Container enchantContainer = ContainerUtil.fromNative(container);
    Player viewer = (Player) enchantContainer.viewer();
    ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(viewer.inventory.getCarried());
    Transaction<ItemStackSnapshot> cursorTrans = new Transaction<>(cursor, cursor);
    EnchantItemEvent.CalculateEnchantment event = SpongeEventFactory.createEnchantItemEventCalculateEnchantment(PhaseTracker.getCauseStackManager().currentCause(), enchList, enchList, enchantContainer, cursorTrans, ItemStackUtil.snapshotOf(itemStack), level, option, seed);
    SpongeCommon.post(event);
    if (event.enchantments() != event.originalEnchantments()) {
        return SpongeRandomEnchantmentListBuilder.toNative(event.enchantments());
    }
    return list;
}
Also used : Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) 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) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Enchantment(org.spongepowered.api.item.enchantment.Enchantment)

Example 23 with Transaction

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

the class ClickCreativeMenuTransaction method createInventoryEvent.

@Override
Optional<ClickContainerEvent> createInventoryEvent(final List<SlotTransaction> slotTransactions, final List<Entity> entities, final PhaseContext<@NonNull ?> context, final Cause cause) {
    if (slotTransactions.isEmpty() && this.slotNum >= 0 && this.slot != null) {
        // No SlotTransaction was captured. So we add the clicked slot as a transaction with the creative stack
        final ItemStackSnapshot item = this.slot.peek().createSnapshot();
        slotTransactions.add(new SlotTransaction(this.slot, item, this.creativeStack));
    }
    // Creative doesn't inform server of cursor status so there is no way of knowing what the final stack is
    final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(this.originalCursor, ItemStackSnapshot.empty());
    if (entities.isEmpty()) {
        return Optional.of(SpongeEventFactory.createClickContainerEventCreativeSet(cause, (Container) this.menu, cursorTransaction, Optional.ofNullable(this.slot), slotTransactions));
    }
    final Cause causeWithSpawnType = Cause.builder().from(cause).build(EventContext.builder().from(cause.context()).add(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM.get()).build());
    return Optional.of(SpongeEventFactory.createClickContainerEventCreativeDrop(causeWithSpawnType, (Container) this.menu, cursorTransaction, this.creativeStack, entities, Optional.ofNullable(this.slot), slotTransactions));
}
Also used : Container(org.spongepowered.api.item.inventory.Container) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) Cause(org.spongepowered.api.event.Cause) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Example 24 with Transaction

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

the class ClickMenuTransaction method createInventoryEvent.

@Override
Optional<ClickContainerEvent> createInventoryEvent(final List<SlotTransaction> slotTransactions, final List<Entity> entities, final PhaseContext<@NonNull ?> context, final Cause cause) {
    final ItemStackSnapshot resultingCursor = ItemStackUtil.snapshotOf(this.player.inventory.getCarried());
    final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(this.cursor, resultingCursor);
    @Nullable final ClickContainerEvent event = context.createContainerEvent(cause, this.player, (Container) this.menu, cursorTransaction, slotTransactions, entities, this.buttonNum, this.slot);
    return Optional.ofNullable(event);
}
Also used : SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) ClickContainerEvent(org.spongepowered.api.event.item.inventory.container.ClickContainerEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 25 with Transaction

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

the class CloseMenuTransaction 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.Close event = SpongeEventFactory.createInteractContainerEventClose(currentCause, (Container) this.menu, cursorTransaction, (Container) this.menu, this.slotTransactions == null ? Collections.emptyList() : this.slotTransactions);
    return Optional.of(event);
}
Also used : SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) GameTransaction(org.spongepowered.common.event.tracking.context.transaction.GameTransaction) InteractContainerEvent(org.spongepowered.api.event.item.inventory.container.InteractContainerEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

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