Search in sources :

Example 11 with AbstractSlot

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

the class PlayerInteractionHandler method handleFinishItemInteraction0.

private void handleFinishItemInteraction0(AbstractSlot slot, HandType handType) {
    final Optional<ItemStack> handItem = slot.peek();
    if (handItem.isPresent()) {
        final CauseStack causeStack = CauseStack.current();
        try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
            frame.pushCause(this.player);
            frame.addContext(ContextKeys.PLAYER, this.player);
            if (handItem.isPresent()) {
                final LanternItemType itemType = (LanternItemType) handItem.get().getType();
                frame.addContext(ContextKeys.USED_ITEM_STACK, handItem.get());
                frame.addContext(ContextKeys.USED_SLOT, slot);
                frame.addContext(ContextKeys.INTERACTION_HAND, handType);
                frame.addContext(ContextKeys.ITEM_TYPE, itemType);
                final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
                if (context.process(itemType.getPipeline().pipeline(FinishUsingItemBehavior.class), (ctx, behavior) -> behavior.tryUse(itemType.getPipeline(), ctx)).isSuccess()) {
                    context.accept();
                }
            }
        }
    }
    resetItemUseTime();
}
Also used : MessagePlayOutEntityAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) MessagePlayInPlayerSwingArm(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerSwingArm) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayInOutFinishUsingItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutFinishUsingItem) MessagePlayInPlayerDigging(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging) HandType(org.spongepowered.api.data.type.HandType) MinimumUseDurationProperty(org.lanternpowered.server.item.property.MinimumUseDurationProperty) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) BreakBlockBehavior(org.lanternpowered.server.block.behavior.types.BreakBlockBehavior) ItemStack(org.spongepowered.api.item.inventory.ItemStack) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) CauseStack(org.lanternpowered.server.event.CauseStack) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) MessagePlayOutBlockBreakAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutBlockBreakAnimation) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) LanternWorld(org.lanternpowered.server.world.LanternWorld) InteractWithBlockBehavior(org.lanternpowered.server.block.behavior.types.InteractWithBlockBehavior) Nullable(javax.annotation.Nullable) ContextKeys(org.lanternpowered.server.behavior.ContextKeys) FinishUsingItemBehavior(org.lanternpowered.server.item.behavior.types.FinishUsingItemBehavior) Location(org.spongepowered.api.world.Location) MaximumUseDurationProperty(org.lanternpowered.server.item.property.MaximumUseDurationProperty) LanternItemType(org.lanternpowered.server.item.LanternItemType) MessagePlayInPlayerUseItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerUseItem) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) BlockTypes(org.spongepowered.api.block.BlockTypes) SwingHandEntityEvent(org.lanternpowered.server.entity.event.SwingHandEntityEvent) Set(java.util.Set) LanternGame(org.lanternpowered.server.game.LanternGame) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) Direction(org.spongepowered.api.util.Direction) DualWieldProperty(org.lanternpowered.server.item.property.DualWieldProperty) Lantern(org.lanternpowered.server.game.Lantern) Vector3i(com.flowpowered.math.vector.Vector3i) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) MessagePlayInPlayerBlockPlacement(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement) CauseStack(org.lanternpowered.server.event.CauseStack) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) LanternItemType(org.lanternpowered.server.item.LanternItemType) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 12 with AbstractSlot

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

the class PlayerInteractionHandler method handleItemInteraction.

public void handleItemInteraction(MessagePlayInPlayerUseItem message) {
    // Prevent duplicate messages
    final long time = System.currentTimeMillis();
    if (this.lastInteractionTime != -1L && time - this.lastInteractionTime < 40) {
        return;
    }
    this.lastInteractionTime = time;
    final CauseStack causeStack = CauseStack.current();
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
        frame.pushCause(this.player);
        frame.addContext(ContextKeys.PLAYER, this.player);
        final BehaviorContext.Snapshot snapshot = context.pushSnapshot();
        if (!handleItemInteraction(context, snapshot)) {
            if (!this.player.get(LanternKeys.CAN_DUAL_WIELD).orElse(false)) {
                return;
            }
            final AbstractSlot offHandSlot = this.player.getInventory().getOffhand();
            final Optional<ItemStack> handItem = offHandSlot.peek();
            if (handItem.isPresent()) {
                final DualWieldProperty property = handItem.get().getProperty(DualWieldProperty.class).orElse(null);
                // noinspection ConstantConditions
                if (property != null && property.getValue()) {
                    /*
                    final Vector3d position = this.player.getPosition().add(0, this.player.get(Keys.IS_SNEAKING).get() ? 1.54 : 1.62, 0);
                    final Optional<BlockRayHit<LanternWorld>> hit = BlockRay.from(this.player.getWorld(), position)
                            .direction(this.player.getDirectionVector())
                            .distanceLimit(5)
                            // Is this supposed to be inverted?
                            .skipFilter(Predicates.not(BlockRay.onlyAirFilter()))
                            .build()
                            .end();
                    if (hit.isPresent() && hit.get().getLocation().getBlock().getType() != BlockTypes.AIR) {
                        return;
                    }
                    */
                    this.player.getConnection().send(new MessagePlayOutEntityAnimation(this.player.getNetworkId(), 3));
                    this.player.triggerEvent(SwingHandEntityEvent.of(HandTypes.OFF_HAND));
                /*
                    final CooldownTracker cooldownTracker = this.player.getCooldownTracker();
                    cooldownTracker.set(handItem.get().getType(), 15);
                    */
                }
            }
        }
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) MessagePlayOutEntityAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DualWieldProperty(org.lanternpowered.server.item.property.DualWieldProperty)

Example 13 with AbstractSlot

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

the class AbstractEquipable method canEquip.

@Override
default boolean canEquip(EquipmentType type, @Nullable ItemStack equipment) {
    checkNotNull(type, "type");
    final Inventory inventory = getInventory().query(Holder.EQUIPMENT_INVENTORY_OPERATION);
    if (inventory instanceof EmptyInventory) {
        return false;
    }
    final AbstractSlot slot = (AbstractSlot) inventory.<IEquipmentInventory>first().getSlot(type).orElse(null);
    return slot != null && (equipment == null || slot.isValidItem(equipment));
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) EmptyInventory(org.spongepowered.api.item.inventory.EmptyInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) IEquipmentInventory(org.lanternpowered.server.inventory.IEquipmentInventory) EmptyInventory(org.spongepowered.api.item.inventory.EmptyInventory)

Example 14 with AbstractSlot

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

the class VanillaContainerInteractionBehavior method handleClick.

@Override
public void handleClick(ClientContainer clientContainer, @Nullable ClientSlot clientSlot, MouseButton mouseButton) {
    final LanternPlayer player = clientContainer.getPlayer();
    if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || (clientSlot != null && !(clientSlot instanceof ClientSlot.Slot))) {
        return;
    }
    final CauseStack causeStack = CauseStack.current();
    if (clientSlot == null) {
        causeStack.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
        final List<Entity> entities = new ArrayList<>();
        final Transaction<ItemStackSnapshot> cursorTransaction;
        // Clicking outside the container
        ItemStackSnapshot oldItem = ItemStackSnapshot.NONE;
        ItemStackSnapshot newItem = ItemStackSnapshot.NONE;
        if (getCursorItem() != null) {
            oldItem = getCursorItem().createSnapshot();
            final ItemStackSnapshot droppedItem;
            if (mouseButton != MouseButton.LEFT) {
                final ItemStack stack = getCursorItem().copy();
                stack.setQuantity(stack.getQuantity() - 1);
                newItem = LanternItemStack.toSnapshot(stack);
                stack.setQuantity(1);
                droppedItem = LanternItemStack.toSnapshot(stack);
            } else {
                droppedItem = oldItem;
            }
            LanternEventHelper.handlePreDroppedItemSpawning(player.getTransform(), droppedItem).ifPresent(entities::add);
        }
        cursorTransaction = new Transaction<>(oldItem, newItem);
        final ClickInventoryEvent.Drop event;
        if (mouseButton == MouseButton.LEFT) {
            event = SpongeEventFactory.createClickInventoryEventDropOutsidePrimary(causeStack.getCurrentCause(), cursorTransaction, entities, this.container, new ArrayList<>());
        } else {
            event = SpongeEventFactory.createClickInventoryEventDropOutsideSecondary(causeStack.getCurrentCause(), cursorTransaction, entities, this.container, new ArrayList<>());
        }
        finishInventoryEvent(event);
        return;
    }
    // Clicking inside the container
    final AbstractSlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
    if (mouseButton == MouseButton.MIDDLE) {
        final ItemStackSnapshot oldItem = LanternItemStack.toSnapshot(getCursorItem());
        Transaction<ItemStackSnapshot> cursorTransaction = null;
        final Optional<GameMode> gameMode = player.get(Keys.GAME_MODE);
        if (gameMode.isPresent() && gameMode.get().equals(GameModes.CREATIVE) && getCursorItem() == null) {
            final ItemStack stack = slot.peek().orElse(null);
            if (stack != null) {
                stack.setQuantity(stack.getMaxStackQuantity());
                cursorTransaction = new Transaction<>(oldItem, stack.createSnapshot());
            }
        }
        if (cursorTransaction == null) {
            cursorTransaction = new Transaction<>(oldItem, oldItem);
        }
        final ClickInventoryEvent.Middle event = SpongeEventFactory.createClickInventoryEventMiddle(causeStack.getCurrentCause(), cursorTransaction, this.container, new ArrayList<>());
        finishInventoryEvent(event);
    } else {
        // Crafting slots have special click behavior
        if (slot instanceof CraftingOutput) {
            List<SlotTransaction> transactions = new ArrayList<>();
            Transaction<ItemStackSnapshot> cursorTransaction;
            final AbstractInventory parent = slot.parent();
            if (parent instanceof CraftingInventory) {
                ClickInventoryEvent event;
                final CraftingInventory inventory = (CraftingInventory) parent;
                final Optional<ExtendedCraftingResult> optResult = Lantern.getRegistry().getCraftingRecipeRegistry().getExtendedResult(inventory.getCraftingGrid(), player.getWorld());
                final ItemStackSnapshot originalCursorItem = LanternItemStack.toSnapshot(getCursorItem());
                if (optResult.isPresent()) {
                    final CraftingResult result = optResult.get().getResult();
                    final ItemStackSnapshot resultItem = result.getResult();
                    int quantity = -1;
                    if (getCursorItem() == null) {
                        quantity = resultItem.getQuantity();
                    } else if (LanternItemStack.areSimilar(resultItem.createStack(), getCursorItem())) {
                        final int quantity1 = resultItem.getQuantity() + getCursorItem().getQuantity();
                        if (quantity1 < getCursorItem().getMaxStackQuantity()) {
                            quantity = quantity1;
                        }
                    }
                    if (quantity == -1) {
                        cursorTransaction = new Transaction<>(originalCursorItem, originalCursorItem);
                        transactions.add(new SlotTransaction(slot, resultItem, resultItem));
                    } else {
                        final LanternItemStack itemStack = (LanternItemStack) resultItem.createStack();
                        itemStack.setQuantity(quantity);
                        cursorTransaction = new Transaction<>(originalCursorItem, itemStack.createSnapshot());
                        transactions.add(new SlotTransaction(slot, resultItem, ItemStackSnapshot.NONE));
                        updateCraftingGrid(player, inventory, optResult.get().getMatrixResult(1), transactions);
                    }
                } else {
                    cursorTransaction = new Transaction<>(originalCursorItem, originalCursorItem);
                    // No actual transaction, there shouldn't have been a item in the crafting result slot
                    transactions.add(new SlotTransaction(slot, ItemStackSnapshot.NONE, ItemStackSnapshot.NONE));
                }
                transactions = this.container.transformSlots(transactions);
                if (mouseButton == MouseButton.LEFT) {
                    event = SpongeEventFactory.createClickInventoryEventPrimary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions);
                } else {
                    event = SpongeEventFactory.createClickInventoryEventSecondary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions);
                }
                finishInventoryEvent(event);
                return;
            } else {
                Lantern.getLogger().warn("Found a CraftingOutput slot without a CraftingInventory as parent.");
            }
        }
        ClickInventoryEvent event;
        if (mouseButton == MouseButton.LEFT) {
            final List<SlotTransaction> transactions = new ArrayList<>();
            Transaction<ItemStackSnapshot> cursorTransaction = null;
            if (getCursorItem() != null && !(slot instanceof OutputSlot)) {
                final PeekedOfferTransactionResult result = slot.peekOffer(getCursorItem());
                if (result.isSuccess()) {
                    transactions.addAll(result.getTransactions());
                    cursorTransaction = new Transaction<>(getCursorItem().createSnapshot(), LanternItemStack.toSnapshot(result.getRejectedItem().orElse(null)));
                } else {
                    final PeekedSetTransactionResult result1 = slot.peekSet(getCursorItem());
                    if (result1.isSuccess()) {
                        cursorTransaction = new Transaction<>(getCursorItem().createSnapshot(), LanternItemStack.toSnapshot(result1.getReplacedItem().orElse(null)));
                        transactions.addAll(result1.getTransactions());
                    }
                }
            } else if (getCursorItem() == null) {
                final PeekedPollTransactionResult result = slot.peekPoll(stack -> true).orElse(null);
                if (result != null) {
                    cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, LanternItemStack.toSnapshot(result.getPolledItem()));
                    transactions.addAll(result.getTransactions());
                } else {
                    cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
                }
            }
            if (cursorTransaction == null) {
                final ItemStackSnapshot cursorItem = LanternItemStack.toSnapshot(getCursorItem());
                cursorTransaction = new Transaction<>(cursorItem, cursorItem);
            }
            event = SpongeEventFactory.createClickInventoryEventPrimary(causeStack.getCurrentCause(), cursorTransaction, this.container, this.container.transformSlots(transactions));
        } else {
            final List<SlotTransaction> transactions = new ArrayList<>();
            Transaction<ItemStackSnapshot> cursorTransaction = null;
            if (getCursorItem() == null) {
                int stackSize = slot.getStackSize();
                if (stackSize != 0) {
                    stackSize = stackSize - (stackSize / 2);
                    final PeekedPollTransactionResult result = slot.peekPoll(stackSize, stack -> true).get();
                    transactions.addAll(result.getTransactions());
                    cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, result.getPolledItem().createSnapshot());
                }
            } else {
                final ItemStack itemStack = getCursorItem().copy();
                itemStack.setQuantity(1);
                final PeekedOfferTransactionResult result = slot.peekOffer(itemStack);
                if (result.isSuccess()) {
                    final ItemStackSnapshot oldCursor = getCursorItem().createSnapshot();
                    int quantity = getCursorItem().getQuantity() - 1;
                    if (quantity <= 0) {
                        cursorTransaction = new Transaction<>(oldCursor, ItemStackSnapshot.NONE);
                    } else {
                        final ItemStack newCursorItem = getCursorItem().copy();
                        newCursorItem.setQuantity(quantity);
                        cursorTransaction = new Transaction<>(oldCursor, newCursorItem.createSnapshot());
                    }
                    transactions.addAll(result.getTransactions());
                } else {
                    final PeekedSetTransactionResult result1 = slot.peekSet(getCursorItem());
                    if (result1.isSuccess()) {
                        final ItemStack replacedItem = result1.getReplacedItem().orElse(null);
                        if (replacedItem != null) {
                            setCursorItem(replacedItem);
                            cursorTransaction = new Transaction<>(getCursorItem().createSnapshot(), LanternItemStack.toSnapshot(replacedItem));
                        } else {
                            cursorTransaction = new Transaction<>(getCursorItem().createSnapshot(), ItemStackSnapshot.NONE);
                        }
                        transactions.addAll(result1.getTransactions());
                    }
                }
            }
            if (cursorTransaction == null) {
                final ItemStackSnapshot cursorItem = LanternItemStack.toSnapshot(getCursorItem());
                cursorTransaction = new Transaction<>(cursorItem, cursorItem);
            }
            event = SpongeEventFactory.createClickInventoryEventSecondary(causeStack.getCurrentCause(), cursorTransaction, this.container, this.container.transformSlots(transactions));
        }
        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) Entity(org.spongepowered.api.entity.Entity) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) CraftingOutput(org.spongepowered.api.item.inventory.crafting.CraftingOutput) ArrayList(java.util.ArrayList) ClientSlot(org.lanternpowered.server.inventory.client.ClientSlot) AbstractInventory(org.lanternpowered.server.inventory.AbstractInventory) OutputSlot(org.spongepowered.api.item.inventory.slot.OutputSlot) CraftingResult(org.spongepowered.api.item.recipe.crafting.CraftingResult) ExtendedCraftingResult(org.lanternpowered.server.item.recipe.crafting.ExtendedCraftingResult) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) CauseStack(org.lanternpowered.server.event.CauseStack) ExtendedCraftingResult(org.lanternpowered.server.item.recipe.crafting.ExtendedCraftingResult) ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) PeekedOfferTransactionResult(org.lanternpowered.server.inventory.PeekedOfferTransactionResult) PeekedSetTransactionResult(org.lanternpowered.server.inventory.PeekedSetTransactionResult) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) PeekedPollTransactionResult(org.lanternpowered.server.inventory.PeekedPollTransactionResult) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) LanternItemStackSnapshot(org.lanternpowered.server.inventory.LanternItemStackSnapshot) 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) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 15 with AbstractSlot

use of org.lanternpowered.server.inventory.AbstractSlot 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)

Aggregations

AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)21 ItemStack (org.spongepowered.api.item.inventory.ItemStack)15 CauseStack (org.lanternpowered.server.event.CauseStack)12 ArrayList (java.util.ArrayList)10 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)10 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)9 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)9 Optional (java.util.Optional)8 Nullable (javax.annotation.Nullable)8 Lantern (org.lanternpowered.server.game.Lantern)8 LanternWorld (org.lanternpowered.server.world.LanternWorld)8 Keys (org.spongepowered.api.data.key.Keys)8 EventContextKeys (org.spongepowered.api.event.cause.EventContextKeys)8 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)8 World (org.spongepowered.api.world.World)8 LanternItemStackSnapshot (org.lanternpowered.server.inventory.LanternItemStackSnapshot)7 PlayerInventoryContainer (org.lanternpowered.server.inventory.PlayerInventoryContainer)7 ClientSlot (org.lanternpowered.server.inventory.client.ClientSlot)7 LanternPlayerInventory (org.lanternpowered.server.inventory.vanilla.LanternPlayerInventory)7 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)7