Search in sources :

Example 1 with AbstractInventorySlot

use of org.lanternpowered.server.inventory.AbstractInventorySlot in project LanternServer by LanternPowered.

the class ClientContainer method bind.

/**
 * Binds this {@link ClientContainer} to the
 * given {@link LanternPlayer}.
 *
 * @param player The player
 */
public void bind(Player player) {
    checkNotNull(player, "player");
    checkState(this.player == null, "There is already a player bound");
    populate();
    this.player = (LanternPlayer) player;
    // Add the tracker to each slot
    for (AbstractInventorySlot slot : this.slotMap.keySet()) {
        slot.addTracker(this);
    }
}
Also used : AbstractInventorySlot(org.lanternpowered.server.inventory.AbstractInventorySlot)

Example 2 with AbstractInventorySlot

use of org.lanternpowered.server.inventory.AbstractInventorySlot in project LanternServer by LanternPowered.

the class VanillaContainerInteractionBehavior method handleShiftClick.

@Override
public void handleShiftClick(ClientContainer clientContainer, ClientSlot clientSlot, MouseButton mouseButton) {
    final LanternPlayer player = clientContainer.getPlayer();
    if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || !(clientSlot instanceof ClientSlot.Slot) || mouseButton == MouseButton.MIDDLE) {
        return;
    }
    final AbstractInventorySlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
    final ItemStack itemStack = slot.peek().orElse(null);
    final Transaction<ItemStackSnapshot> cursorTransaction;
    final List<SlotTransaction> transactions = new ArrayList<>();
    if (slot instanceof CraftingOutput) {
        final ItemStackSnapshot cursorItem = LanternItemStack.toSnapshot(getCursorItem());
        cursorTransaction = new Transaction<>(cursorItem, cursorItem);
        final AbstractInventory parent = slot.parent();
        if (parent instanceof CraftingInventory) {
            final CraftingInventory inventory = (CraftingInventory) parent;
            final Optional<ExtendedCraftingResult> optResult = Lantern.getRegistry().getCraftingRecipeRegistry().getExtendedResult(inventory.getCraftingGrid(), player.getWorld());
            if (optResult.isPresent()) {
                final ExtendedCraftingResult result = optResult.get();
                final ItemStackSnapshot resultItem = result.getResult().getResult();
                int times = result.getMaxTimes();
                final ItemStack itemStack1 = resultItem.createStack();
                itemStack1.setQuantity(times * itemStack1.getQuantity());
                final AbstractInventory targetInventory = this.container.getPlayerInventory().getView(LanternPlayerInventory.View.REVERSE_MAIN_AND_HOTBAR);
                PeekedOfferTransactionResult peekResult = targetInventory.peekOffer(itemStack1);
                if (peekResult.isSuccess()) {
                    transactions.add(new SlotTransaction(slot, resultItem, ItemStackSnapshot.NONE));
                    final ItemStack rejectedItem = peekResult.getRejectedItem().orElse(null);
                    if (rejectedItem != null) {
                        final int added = itemStack1.getQuantity() - rejectedItem.getQuantity();
                        times = added / resultItem.getQuantity();
                        final int diff = added % resultItem.getQuantity();
                        if (diff != 0) {
                            itemStack1.setQuantity(resultItem.getQuantity() * times);
                            peekResult = targetInventory.peekOffer(itemStack1);
                            checkState(peekResult.isSuccess());
                        }
                    }
                    transactions.addAll(peekResult.getTransactions());
                    updateCraftingGrid(player, inventory, result.getMatrixResult(times), transactions);
                }
            } else {
                // No actual transaction, there shouldn't have been a item in the crafting result slot
                transactions.add(new SlotTransaction(slot, ItemStackSnapshot.NONE, ItemStackSnapshot.NONE));
            }
        } else {
            Lantern.getLogger().warn("Found a CraftingOutput slot without a CraftingInventory as parent.");
            return;
        }
    } else {
        final ItemStackSnapshot cursorItem = LanternItemStack.toSnapshot(getCursorItem());
        cursorTransaction = new Transaction<>(cursorItem, cursorItem);
        if (itemStack != null) {
            final IInventory target = this.container.getOpenInventory().getShiftClickBehavior().getTarget(this.container, slot);
            final PeekedOfferTransactionResult result = target.peekOffer(itemStack.copy());
            if (result.isSuccess()) {
                transactions.addAll(result.getTransactions());
                final ItemStack rejectedItem = result.getRejectedItem().orElse(null);
                if (rejectedItem != null) {
                    slot.peekPoll(itemStack.getQuantity() - rejectedItem.getQuantity(), stack -> true).ifPresent(peekResult -> transactions.addAll(peekResult.getTransactions()));
                } else {
                    slot.peekPoll(stack -> true).ifPresent(peekResult -> transactions.addAll(peekResult.getTransactions()));
                }
            }
        }
    }
    final List<SlotTransaction> transactions1 = this.container.transformSlots(transactions);
    final CauseStack causeStack = CauseStack.current();
    final ClickInventoryEvent.Shift event;
    if (mouseButton == MouseButton.LEFT) {
        event = SpongeEventFactory.createClickInventoryEventShiftPrimary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions1);
    } else {
        event = SpongeEventFactory.createClickInventoryEventShiftSecondary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions1);
    }
    finishInventoryEvent(event);
}
Also used : CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) IInventory(org.lanternpowered.server.inventory.IInventory) 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) ExtendedCraftingResult(org.lanternpowered.server.item.recipe.crafting.ExtendedCraftingResult) CraftingOutput(org.spongepowered.api.item.inventory.crafting.CraftingOutput) ArrayList(java.util.ArrayList) PeekedOfferTransactionResult(org.lanternpowered.server.inventory.PeekedOfferTransactionResult) ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) AbstractInventory(org.lanternpowered.server.inventory.AbstractInventory) 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) AbstractInventorySlot(org.lanternpowered.server.inventory.AbstractInventorySlot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 3 with AbstractInventorySlot

use of org.lanternpowered.server.inventory.AbstractInventorySlot in project LanternServer by LanternPowered.

the class ClientContainer method bindBottom.

public BottomContainerPart bindBottom(BottomContainerPart bottomContainerPart) {
    populate();
    if (this.bottomContainerPart == null) {
        this.bottomContainerPart = new BottomContainerPartImpl();
    }
    final ClientContainer clientContainer = bottomContainerPart.getRoot();
    final int s1 = getTopSlotsCount();
    final int s2 = clientContainer.getTopSlotsCount();
    for (int i = 0; i < MAIN_INVENTORY_FLAGS.length; i++) {
        final int index = s1 + i;
        removeSlot(index);
        BaseClientSlot clientSlot = clientContainer.slots[s2 + i];
        if (clientSlot instanceof SlotClientSlot) {
            final AbstractInventorySlot slot = ((SlotClientSlot) clientSlot).slot;
            clientSlot = new SlotClientSlot(index, slot);
            this.slotMap.put(slot, (SlotClientSlot) clientSlot);
            if (this.player != null) {
                slot.addTracker(this);
            }
        } else if (clientSlot instanceof IconClientSlot) {
            final ItemStack itemStack = clientSlot.getItem();
            clientSlot = new IconClientSlot(index);
            ((IconClientSlot) clientSlot).setItem(itemStack);
        } else {
            clientSlot = new EmptyClientSlot(index);
        }
        this.slots[index] = clientSlot;
    }
    return this.bottomContainerPart;
}
Also used : AbstractInventorySlot(org.lanternpowered.server.inventory.AbstractInventorySlot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 4 with AbstractInventorySlot

use of org.lanternpowered.server.inventory.AbstractInventorySlot in project LanternServer by LanternPowered.

the class ClientContainer method release.

/**
 * Releases all the {@link AbstractInventorySlot} and
 * removes the {@link LanternPlayer}.
 */
public void release() {
    populate();
    if (this.player == null) {
        return;
    }
    this.player = null;
    // Remove the tracker from each slot
    for (AbstractInventorySlot slot : this.slotMap.keySet()) {
        slot.removeTracker(this);
    }
}
Also used : AbstractInventorySlot(org.lanternpowered.server.inventory.AbstractInventorySlot)

Aggregations

AbstractInventorySlot (org.lanternpowered.server.inventory.AbstractInventorySlot)4 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Streams (com.google.common.collect.Streams)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)1 CauseStack (org.lanternpowered.server.event.CauseStack)1 LanternEventHelper (org.lanternpowered.server.event.LanternEventHelper)1 Lantern (org.lanternpowered.server.game.Lantern)1 AbstractInventory (org.lanternpowered.server.inventory.AbstractInventory)1 AbstractOrderedInventory (org.lanternpowered.server.inventory.AbstractOrderedInventory)1 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)1 IInventory (org.lanternpowered.server.inventory.IInventory)1 LanternContainer (org.lanternpowered.server.inventory.LanternContainer)1 LanternItemStackSnapshot (org.lanternpowered.server.inventory.LanternItemStackSnapshot)1