Search in sources :

Example 6 with ClientContainer

use of org.lanternpowered.server.inventory.client.ClientContainer 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 7 with ClientContainer

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

the class VanillaContainerInteractionBehavior method handlePick.

@Override
public void handlePick(ClientContainer clientContainer, @Nullable ClientSlot clientSlot) {
    final LanternPlayer player = clientContainer.getPlayer();
    if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || !(clientSlot instanceof ClientSlot.Slot)) {
        return;
    }
    final PlayerInventoryContainer inventoryContainer = player.getInventoryContainer();
    final ClientSlot hotbarClientSlot = inventoryContainer.getClientContainer().getSelectedHotbarSlot();
    if (!(hotbarClientSlot instanceof ClientSlot.Slot)) {
        return;
    }
    final LanternHotbarInventory hotbar = player.getInventory().getHotbar();
    final AbstractSlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
    // The slot we will swap items with
    AbstractSlot hotbarSlot = hotbar.getSelectedSlot();
    if (hotbarSlot.peek().isPresent()) {
        final Optional<AbstractSlot> optSlot = Streams.stream(hotbar.<AbstractSlot>slots()).filter(slot1 -> !slot1.peek().isPresent()).findFirst();
        if (optSlot.isPresent()) {
            hotbarSlot = optSlot.get();
        }
    }
    final ItemStack slotItem = slot.peek().orElse(null);
    final ItemStack hotbarItem = hotbarSlot.peek().orElse(null);
    hotbarSlot.set(slotItem);
    hotbar.setSelectedSlotIndex(hotbar.getSlotIndex(hotbarSlot));
    slot.set(hotbarItem);
}
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) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) LanternHotbarInventory(org.lanternpowered.server.inventory.vanilla.LanternHotbarInventory) PlayerInventoryContainer(org.lanternpowered.server.inventory.PlayerInventoryContainer) 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) ClientSlot(org.lanternpowered.server.inventory.client.ClientSlot) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 8 with ClientContainer

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

the class PlayerContainerSession method handleWindowClick.

public void handleWindowClick(MessagePlayInClickWindow message) {
    final int windowId = message.getWindowId();
    if (this.openContainer == null) {
        if (message.getWindowId() == 0) {
            openPlayerContainer();
        } else {
            return;
        }
    } else if (windowId != getContainerId()) {
        return;
    }
    final ClientContainer clientContainer = getClientContainer();
    clientContainer.handleClick(message.getSlot(), message.getMode(), message.getButton());
}
Also used : TradingClientContainer(org.lanternpowered.server.inventory.client.TradingClientContainer) PlayerClientContainer(org.lanternpowered.server.inventory.client.PlayerClientContainer) BeaconClientContainer(org.lanternpowered.server.inventory.client.BeaconClientContainer) EnchantmentTableClientContainer(org.lanternpowered.server.inventory.client.EnchantmentTableClientContainer) AnvilClientContainer(org.lanternpowered.server.inventory.client.AnvilClientContainer) ClientContainer(org.lanternpowered.server.inventory.client.ClientContainer)

Example 9 with ClientContainer

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

the class PlayerContainerSession method handlePickItem.

public void handlePickItem(MessagePlayInPickItem message) {
    final ClientContainer clientContainer = getClientContainer();
    clientContainer.handlePick(message.getSlot());
}
Also used : TradingClientContainer(org.lanternpowered.server.inventory.client.TradingClientContainer) PlayerClientContainer(org.lanternpowered.server.inventory.client.PlayerClientContainer) BeaconClientContainer(org.lanternpowered.server.inventory.client.BeaconClientContainer) EnchantmentTableClientContainer(org.lanternpowered.server.inventory.client.EnchantmentTableClientContainer) AnvilClientContainer(org.lanternpowered.server.inventory.client.AnvilClientContainer) ClientContainer(org.lanternpowered.server.inventory.client.ClientContainer)

Example 10 with ClientContainer

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

the class PlayerContainerSession method handleWindowCreativeClick.

public void handleWindowCreativeClick(MessagePlayInCreativeWindowAction message) {
    if (this.openContainer == null) {
        openPlayerContainer();
    }
    final ClientContainer clientContainer = getClientContainer();
    clientContainer.handleCreativeClick(message.getSlot(), message.getItemStack());
}
Also used : TradingClientContainer(org.lanternpowered.server.inventory.client.TradingClientContainer) PlayerClientContainer(org.lanternpowered.server.inventory.client.PlayerClientContainer) BeaconClientContainer(org.lanternpowered.server.inventory.client.BeaconClientContainer) EnchantmentTableClientContainer(org.lanternpowered.server.inventory.client.EnchantmentTableClientContainer) AnvilClientContainer(org.lanternpowered.server.inventory.client.AnvilClientContainer) ClientContainer(org.lanternpowered.server.inventory.client.ClientContainer)

Aggregations

ClientContainer (org.lanternpowered.server.inventory.client.ClientContainer)12 PlayerClientContainer (org.lanternpowered.server.inventory.client.PlayerClientContainer)10 Nullable (javax.annotation.Nullable)7 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)7 CauseStack (org.lanternpowered.server.event.CauseStack)7 Transaction (org.spongepowered.api.data.Transaction)7 Preconditions.checkState (com.google.common.base.Preconditions.checkState)6 Streams (com.google.common.collect.Streams)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 LanternEventHelper (org.lanternpowered.server.event.LanternEventHelper)6 Lantern (org.lanternpowered.server.game.Lantern)6 AbstractInventory (org.lanternpowered.server.inventory.AbstractInventory)6 AbstractInventorySlot (org.lanternpowered.server.inventory.AbstractInventorySlot)6 AbstractOrderedInventory (org.lanternpowered.server.inventory.AbstractOrderedInventory)6 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)6 IInventory (org.lanternpowered.server.inventory.IInventory)6 LanternContainer (org.lanternpowered.server.inventory.LanternContainer)6