Search in sources :

Example 21 with SlotTransaction

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

the class VanillaContainerInteractionBehavior method updateCraftingGrid.

private void updateCraftingGrid(Player player, CraftingInventory craftingInventory, MatrixResult matrixResult, List<SlotTransaction> transactions) {
    final CraftingMatrix matrix = matrixResult.getCraftingMatrix();
    final CraftingGridInventory grid = craftingInventory.getCraftingGrid();
    for (int x = 0; x < matrix.width(); x++) {
        for (int y = 0; y < matrix.height(); y++) {
            final ItemStack itemStack = matrix.get(x, y);
            final Slot slot = grid.getSlot(x, y).get();
            transactions.add(new SlotTransaction(slot, slot.peek().map(LanternItemStackSnapshot::wrap).orElse(LanternItemStackSnapshot.none()), LanternItemStackSnapshot.wrap(itemStack)));
        }
    }
    final CauseStack causeStack = CauseStack.current();
    causeStack.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
    final Transform<World> transform = player.getTransform();
    final List<Entity> entities = LanternEventHelper.handlePreDroppedItemSpawning(matrixResult.getRest().stream().map(itemStack -> new Tuple<ItemStackSnapshot, Transform<World>>(LanternItemStackSnapshot.wrap(itemStack), transform)).collect(Collectors.toList()));
    final SpawnEntityEvent event = SpongeEventFactory.createDropItemEventDispense(causeStack.getCurrentCause(), entities);
    Sponge.getEventManager().post(event);
    // Spawn all the entities in the world if the event isn't cancelled
    LanternWorld.finishSpawnEntityEvent(event);
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) Entity(org.spongepowered.api.entity.Entity) CraftingMatrix(org.lanternpowered.server.item.recipe.crafting.CraftingMatrix) World(org.spongepowered.api.world.World) LanternWorld(org.lanternpowered.server.world.LanternWorld) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) 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) Transform(org.spongepowered.api.entity.Transform) CraftingGridInventory(org.spongepowered.api.item.inventory.crafting.CraftingGridInventory)

Example 22 with SlotTransaction

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

the class AbstractChildrenInventory method peekOffer.

@Override
public PeekedOfferTransactionResult peekOffer(ItemStack itemStack) {
    checkNotNull(itemStack, "itemStack");
    final PeekedOfferTransactionResult peekResult;
    final Set<Inventory> processed = new HashSet<>();
    final Inventory inventory = query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(itemStack));
    if (inventory instanceof AbstractChildrenInventory) {
        peekResult = ((AbstractChildrenInventory) inventory).peekOffer(itemStack, processed, true);
        if (peekResult.isSuccess()) {
            if (!peekResult.getRejectedItem().isPresent()) {
                return peekResult;
            }
            itemStack = peekResult.getRejectedItem().get();
        }
    } else {
        peekResult = null;
    }
    final PeekedOfferTransactionResult peekResult1 = peekOffer(itemStack, processed, false);
    if (peekResult == null || !peekResult.isSuccess()) {
        return peekResult1;
    } else if (!peekResult1.isSuccess()) {
        return peekResult;
    }
    return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.SUCCESS, ImmutableList.<SlotTransaction>builder().addAll(peekResult.getTransactions()).addAll(peekResult1.getTransactions()).build(), peekResult1.getRejectedItem().orElse(null));
}
Also used : SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory) EmptyInventory(org.spongepowered.api.item.inventory.EmptyInventory) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 23 with SlotTransaction

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

the class AbstractInventorySlot method peekOffer.

@Override
public PeekedOfferTransactionResult peekOffer(ItemStack itemStack) {
    checkNotNull(itemStack, "itemStack");
    if (itemStack.isEmpty()) {
        return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.FAILURE, ImmutableList.of(), itemStack);
    }
    final int maxStackSize = Math.min(itemStack.getMaxStackQuantity(), this.maxStackSize);
    if ((this.itemStack != null && (!this.itemStack.similarTo(itemStack) || this.itemStack.getQuantity() >= maxStackSize)) || !isValidItem(itemStack)) {
        return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.FAILURE, ImmutableList.of(), itemStack);
    }
    final List<SlotTransaction> transactions = new ArrayList<>();
    // Get the amount of space we have left
    final int availableSpace = this.itemStack == null ? maxStackSize : maxStackSize - this.itemStack.getQuantity();
    final int quantity = itemStack.getQuantity();
    if (quantity > availableSpace) {
        ItemStack newStack;
        if (this.itemStack == null) {
            newStack = itemStack.copy();
        } else {
            newStack = this.itemStack.copy();
        }
        newStack.setQuantity(maxStackSize);
        itemStack = itemStack.copy();
        itemStack.setQuantity(quantity - availableSpace);
        transactions.add(new SlotTransaction(this, LanternItemStack.toSnapshot(this.itemStack), LanternItemStackSnapshot.wrap(newStack)));
        return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.SUCCESS, transactions, itemStack);
    } else {
        final ItemStack newStack;
        if (this.itemStack == null) {
            newStack = itemStack.copy();
        } else {
            newStack = this.itemStack.copy();
            newStack.setQuantity(newStack.getQuantity() + quantity);
        }
        transactions.add(new SlotTransaction(this, LanternItemStack.toSnapshot(this.itemStack), LanternItemStackSnapshot.wrap(newStack)));
        return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.SUCCESS, transactions, null);
    }
}
Also used : ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Example 24 with SlotTransaction

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

the class AbstractInventorySlot method peekPoll.

@Override
public Optional<PeekedPollTransactionResult> peekPoll(Predicate<ItemStack> matcher) {
    checkNotNull(matcher, "matcher");
    if (this.itemStack == null || !matcher.test(this.itemStack)) {
        return Optional.empty();
    }
    final List<SlotTransaction> transactions = new ArrayList<>();
    transactions.add(new SlotTransaction(this, this.itemStack.createSnapshot(), ItemStackSnapshot.NONE));
    return Optional.of(new PeekedPollTransactionResult(transactions, this.itemStack.copy()));
}
Also used : ArrayList(java.util.ArrayList) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Example 25 with SlotTransaction

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

the class AbstractInventorySlot method peekPoll.

@Override
public Optional<PeekedPollTransactionResult> peekPoll(int limit, Predicate<ItemStack> matcher) {
    checkNotNull(matcher, "matcher");
    checkArgument(limit >= 0, "Limit may not be negative");
    ItemStack itemStack = this.itemStack;
    // There is no item available
    if (limit == 0 || itemStack == null || !matcher.test(itemStack)) {
        return Optional.empty();
    }
    final ItemStackSnapshot oldItem = itemStack.createSnapshot();
    itemStack = itemStack.copy();
    final int quantity = itemStack.getQuantity();
    final ItemStackSnapshot newItem;
    if (limit >= quantity) {
        newItem = ItemStackSnapshot.NONE;
    } else {
        itemStack.setQuantity(quantity - limit);
        newItem = LanternItemStack.toSnapshot(itemStack);
        itemStack.setQuantity(limit);
    }
    final List<SlotTransaction> transactions = new ArrayList<>();
    transactions.add(new SlotTransaction(this, oldItem, newItem));
    return Optional.of(new PeekedPollTransactionResult(transactions, itemStack));
}
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)

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