Search in sources :

Example 46 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.

the class VanillaContainerInteractionBehavior method handleDropKey.

@Override
public void handleDropKey(ClientContainer clientContainer, ClientSlot clientSlot, boolean ctrl) {
    final LanternPlayer player = clientContainer.getPlayer();
    if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || !(clientSlot instanceof ClientSlot.Slot)) {
        return;
    }
    final AbstractSlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
    final CauseStack causeStack = CauseStack.current();
    causeStack.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
    final List<Entity> entities = new ArrayList<>();
    final Transaction<ItemStackSnapshot> cursorTransaction;
    List<SlotTransaction> slotTransactions = new ArrayList<>();
    final ItemStackSnapshot item = LanternItemStack.toSnapshot(getCursorItem());
    cursorTransaction = new Transaction<>(item, item);
    final Optional<PeekedPollTransactionResult> result = ctrl ? slot.peekPoll(itemStack -> true) : slot.peekPoll(1, itemStack -> true);
    if (result.isPresent()) {
        final List<SlotTransaction> transactions = result.get().getTransactions();
        slotTransactions.addAll(transactions);
        final ItemStack itemStack = transactions.get(0).getOriginal().createStack();
        itemStack.setQuantity(itemStack.getQuantity() - transactions.get(0).getFinal().getQuantity());
        LanternEventHelper.handlePreDroppedItemSpawning(player.getTransform(), LanternItemStackSnapshot.wrap(itemStack)).ifPresent(entities::add);
    }
    slotTransactions = this.container.transformSlots(slotTransactions);
    final ClickInventoryEvent.Drop event;
    if (ctrl) {
        event = SpongeEventFactory.createClickInventoryEventDropFull(causeStack.getCurrentCause(), cursorTransaction, entities, this.container, slotTransactions);
    } else {
        event = SpongeEventFactory.createClickInventoryEventDropSingle(causeStack.getCurrentCause(), cursorTransaction, entities, this.container, slotTransactions);
    }
    finishInventoryEvent(event);
}
Also used : EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) LanternPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerInventory) LanternItemStackSnapshot(org.lanternpowered.server.inventory.LanternItemStackSnapshot) CraftingResult(org.spongepowered.api.item.recipe.crafting.CraftingResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) Transaction(org.spongepowered.api.data.Transaction) OutputSlot(org.spongepowered.api.item.inventory.slot.OutputSlot) ExtendedCraftingResult(org.lanternpowered.server.item.recipe.crafting.ExtendedCraftingResult) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) MatrixResult(org.lanternpowered.server.item.recipe.crafting.MatrixResult) Sponge(org.spongepowered.api.Sponge) CraftingGridInventory(org.spongepowered.api.item.inventory.crafting.CraftingGridInventory) Tuple(org.spongepowered.api.util.Tuple) Streams(com.google.common.collect.Streams) CraftingOutput(org.spongepowered.api.item.inventory.crafting.CraftingOutput) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Lantern(org.lanternpowered.server.game.Lantern) Transform(org.spongepowered.api.entity.Transform) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) World(org.spongepowered.api.world.World) Optional(java.util.Optional) AbstractOrderedInventory(org.lanternpowered.server.inventory.AbstractOrderedInventory) Player(org.spongepowered.api.entity.living.player.Player) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) ClientContainer(org.lanternpowered.server.inventory.client.ClientContainer) IInventory(org.lanternpowered.server.inventory.IInventory) Keys(org.spongepowered.api.data.key.Keys) PeekedSetTransactionResult(org.lanternpowered.server.inventory.PeekedSetTransactionResult) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) ArrayList(java.util.ArrayList) CauseStack(org.lanternpowered.server.event.CauseStack) PlayerInventoryContainer(org.lanternpowered.server.inventory.PlayerInventoryContainer) LanternContainer(org.lanternpowered.server.inventory.LanternContainer) PlayerClientContainer(org.lanternpowered.server.inventory.client.PlayerClientContainer) LanternWorld(org.lanternpowered.server.world.LanternWorld) CraftingMatrix(org.lanternpowered.server.item.recipe.crafting.CraftingMatrix) Nullable(javax.annotation.Nullable) ClientSlot(org.lanternpowered.server.inventory.client.ClientSlot) AbstractInventorySlot(org.lanternpowered.server.inventory.AbstractInventorySlot) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) LanternEventHelper(org.lanternpowered.server.event.LanternEventHelper) PeekedPollTransactionResult(org.lanternpowered.server.inventory.PeekedPollTransactionResult) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) Slot(org.spongepowered.api.item.inventory.Slot) Entity(org.spongepowered.api.entity.Entity) PeekedOfferTransactionResult(org.lanternpowered.server.inventory.PeekedOfferTransactionResult) SpawnTypes(org.spongepowered.api.event.cause.entity.spawn.SpawnTypes) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) LanternHotbarInventory(org.lanternpowered.server.inventory.vanilla.LanternHotbarInventory) AbstractInventory(org.lanternpowered.server.inventory.AbstractInventory) CauseStack(org.lanternpowered.server.event.CauseStack) Entity(org.spongepowered.api.entity.Entity) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) ArrayList(java.util.ArrayList) ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) ClientSlot(org.lanternpowered.server.inventory.client.ClientSlot) PeekedPollTransactionResult(org.lanternpowered.server.inventory.PeekedPollTransactionResult) OutputSlot(org.spongepowered.api.item.inventory.slot.OutputSlot) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) ClientSlot(org.lanternpowered.server.inventory.client.ClientSlot) AbstractInventorySlot(org.lanternpowered.server.inventory.AbstractInventorySlot) Slot(org.spongepowered.api.item.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) LanternItemStackSnapshot(org.lanternpowered.server.inventory.LanternItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 47 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.

the class AbstractInventorySlot method peekSet.

@Override
public PeekedSetTransactionResult peekSet(@Nullable ItemStack stack) {
    if (!LanternItemStack.isEmpty(stack) && !isValidItem(stack)) {
        return new PeekedSetTransactionResult(InventoryTransactionResult.Type.FAILURE, ImmutableList.of(), stack, null);
    }
    stack = LanternItemStack.toNullable(stack);
    final List<SlotTransaction> transactions = new ArrayList<>();
    final ItemStackSnapshot oldItem = LanternItemStack.toSnapshot(this.itemStack);
    ItemStack rejectedItem = null;
    ItemStack replacedItem = oldItem.isEmpty() ? null : this.itemStack.copy();
    ItemStackSnapshot newItem = ItemStackSnapshot.NONE;
    if (stack != null) {
        final int maxStackSize = Math.min(stack.getMaxStackQuantity(), this.maxStackSize);
        final int quantity = stack.getQuantity();
        if (quantity > maxStackSize) {
            stack = stack.copy();
            stack.setQuantity(maxStackSize);
            newItem = LanternItemStack.toSnapshot(stack);
            // Create the rest stack that was rejected,
            // because the inventory doesn't allow so many items
            rejectedItem = stack.copy();
            rejectedItem.setQuantity(quantity - maxStackSize);
        } else {
            newItem = LanternItemStack.toSnapshot(stack);
        }
    }
    transactions.add(new SlotTransaction(this, oldItem, newItem));
    return new PeekedSetTransactionResult(InventoryTransactionResult.Type.SUCCESS, transactions, rejectedItem, replacedItem);
}
Also used : ArrayList(java.util.ArrayList) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Example 48 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.

the class AbstractChildrenInventory method peekPoll.

@Override
public Optional<PeekedPollTransactionResult> peekPoll(int limit, Predicate<ItemStack> matcher) {
    checkNotNull(matcher, "matcher");
    checkArgument(limit >= 0, "Limit may not be negative");
    if (limit == 0) {
        return Optional.empty();
    }
    PeekedPollTransactionResult peekResult = null;
    // Loop through the children inventories
    for (AbstractMutableInventory inventory : getChildren()) {
        // Check whether the slot a item contains
        if (peekResult == null) {
            peekResult = inventory.peekPoll(limit, matcher).orElse(null);
            if (peekResult != null) {
                if (peekResult.getPolledItem().getQuantity() >= limit) {
                    return Optional.of(peekResult);
                } else {
                    limit -= peekResult.getPolledItem().getQuantity();
                    if (!(matcher instanceof SimilarItemMatcher)) {
                        matcher = new SimilarItemMatcher(peekResult.getPolledItem());
                    }
                }
            }
        } else {
            final PeekedPollTransactionResult peekResult1 = inventory.peekPoll(limit, matcher).orElse(null);
            if (peekResult1 != null) {
                final int peekedStackSize = peekResult1.getPolledItem().getQuantity();
                final ItemStack peekedItem = peekResult.getPolledItem();
                limit -= peekedStackSize;
                peekedItem.setQuantity(peekedItem.getQuantity() + peekedStackSize);
                final List<SlotTransaction> transactions = new ArrayList<>();
                transactions.addAll(peekResult.getTransactions());
                transactions.addAll(peekResult1.getTransactions());
                peekResult = new PeekedPollTransactionResult(transactions, peekedItem);
                if (limit <= 0) {
                    return Optional.of(peekResult);
                }
            }
        }
    }
    return Optional.ofNullable(peekResult);
}
Also used : ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Example 49 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.

the class HandlerPlayInSwapHandItems method handle.

@Override
public void handle(NetworkContext context, MessagePlayInSwapHandItems message) {
    final LanternPlayer player = context.getSession().getPlayer();
    final LanternPlayerInventory inventory = player.getInventory();
    final AbstractSlot hotbarSlot = inventory.getHotbar().getSelectedSlot();
    final AbstractSlot offHandSlot = inventory.getOffhand();
    final ItemStackSnapshot hotbarItem = hotbarSlot.peek().map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE);
    final ItemStackSnapshot offHandItem = offHandSlot.peek().map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE);
    final List<SlotTransaction> transactions = new ArrayList<>();
    transactions.add(new SlotTransaction(hotbarSlot, hotbarItem, offHandItem));
    transactions.add(new SlotTransaction(offHandSlot, offHandItem, hotbarItem));
    try (CauseStack.Frame frame = CauseStack.current().pushCauseFrame()) {
        frame.addContext(EventContextKeys.PLAYER, player);
        frame.pushCause(player);
        final ChangeInventoryEvent.SwapHand event = SpongeEventFactory.createChangeInventoryEventSwapHand(frame.getCurrentCause(), inventory, transactions);
        Sponge.getEventManager().post(event);
        if (!event.isCancelled()) {
            transactions.stream().filter(Transaction::isValid).forEach(transaction -> transaction.getSlot().set(transaction.getFinal().createStack()));
            final PlayerInventoryContainer inventoryContainer = context.getSession().getPlayer().getInventoryContainer();
            inventoryContainer.getClientContainer().queueSilentSlotChange(hotbarSlot);
        }
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) LanternPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerInventory) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) PlayerInventoryContainer(org.lanternpowered.server.inventory.PlayerInventoryContainer) ArrayList(java.util.ArrayList) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 50 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.

the class JukeboxInteractionBehavior method tryInteract.

@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Location<World> location = context.requireContext(ContextKeys.INTERACTION_LOCATION);
    final Optional<TileEntity> optTile = location.getTileEntity();
    if (optTile.isPresent()) {
        final TileEntity tile = optTile.get();
        if (tile instanceof Jukebox) {
            final LanternJukebox jukebox = (LanternJukebox) tile;
            final Optional<Entity> optEjectedItem = jukebox.ejectRecordItem();
            boolean success = false;
            if (optEjectedItem.isPresent()) {
                final Entity entity = optEjectedItem.get();
                entity.getWorld().spawnEntity(optEjectedItem.get());
                // TODO: Include the entity in the behavior context
                success = true;
            }
            final Optional<ItemStack> optItemStack = context.getContext(ContextKeys.USED_ITEM_STACK);
            if (optItemStack.isPresent()) {
                final ItemStack itemStack = optItemStack.get();
                final RecordProperty property = itemStack.getProperty(RecordProperty.class).orElse(null);
                final RecordType recordType = property == null ? null : property.getValue();
                if (recordType != null) {
                    final ItemStackSnapshot oldSnapshot = itemStack.createSnapshot();
                    itemStack.setQuantity(itemStack.getQuantity() - 1);
                    final ItemStackSnapshot newSnapshot = itemStack.createSnapshot();
                    context.getContext(ContextKeys.PLAYER).ifPresent(player -> {
                        if (!player.get(Keys.GAME_MODE).orElse(GameModes.NOT_SET).equals(GameModes.CREATIVE)) {
                            context.getContext(ContextKeys.USED_SLOT).ifPresent(slot -> context.addSlotChange(new SlotTransaction(slot, oldSnapshot, newSnapshot)));
                        }
                    });
                    itemStack.setQuantity(1);
                    jukebox.insertRecord(itemStack);
                    jukebox.playRecord();
                    success = true;
                }
            }
            if (success) {
                return BehaviorResult.SUCCESS;
            }
        }
    }
    return BehaviorResult.PASS;
}
Also used : LanternJukebox(org.lanternpowered.server.block.tile.vanilla.LanternJukebox) Jukebox(org.spongepowered.api.block.tileentity.Jukebox) Entity(org.spongepowered.api.entity.Entity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) World(org.spongepowered.api.world.World) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) LanternJukebox(org.lanternpowered.server.block.tile.vanilla.LanternJukebox) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) RecordType(org.spongepowered.api.effect.sound.record.RecordType) RecordProperty(org.spongepowered.api.data.property.item.RecordProperty) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)50 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)30 ArrayList (java.util.ArrayList)20 ItemStack (org.spongepowered.api.item.inventory.ItemStack)16 Inventory (org.spongepowered.api.item.inventory.Inventory)14 CraftingInventory (org.spongepowered.api.item.inventory.crafting.CraftingInventory)14 Entity (org.spongepowered.api.entity.Entity)13 Transaction (org.spongepowered.api.data.Transaction)11 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)11 ClickInventoryEvent (org.spongepowered.api.event.item.inventory.ClickInventoryEvent)11 Slot (org.spongepowered.api.item.inventory.Slot)11 Slot (net.minecraft.inventory.Slot)9 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)9 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)9 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)9 IMixinContainer (org.spongepowered.common.interfaces.IMixinContainer)9 Nullable (javax.annotation.Nullable)8 ItemStack (net.minecraft.item.ItemStack)8 CauseStack (org.lanternpowered.server.event.CauseStack)8 List (java.util.List)7