Search in sources :

Example 16 with AbstractSlot

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

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

the class UserStore method serializePlayerInventory.

private static List<DataView> serializePlayerInventory(AbstractUserInventory<?> inventory) {
    final List<DataView> itemViews = new ArrayList<>();
    final LanternMainPlayerInventory mainInventory = inventory.getMain();
    final LanternPlayerEquipmentInventory equipmentInventory = inventory.getEquipment();
    final AbstractSlot offHandSlot = inventory.getOffhand();
    Iterable<Slot> slots = mainInventory.slots();
    for (Slot slot : slots) {
        serializeSlot(mainInventory, slot, 0, ItemStackStore.INSTANCE, itemViews);
    }
    slots = equipmentInventory.slots();
    for (Slot slot : slots) {
        serializeSlot(equipmentInventory, slot, 100, ItemStackStore.INSTANCE, itemViews);
    }
    serializeSlot(150, offHandSlot, ItemStackStore.INSTANCE, itemViews);
    return itemViews;
}
Also used : DataView(org.spongepowered.api.data.DataView) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) ArrayList(java.util.ArrayList) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) Slot(org.spongepowered.api.item.inventory.Slot) LanternPlayerEquipmentInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerEquipmentInventory) LanternMainPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternMainPlayerInventory)

Example 18 with AbstractSlot

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

the class UserStore method deserializePlayerInventory.

private static void deserializePlayerInventory(AbstractUserInventory<?> inventory, List<DataView> itemViews) {
    final LanternMainPlayerInventory mainInventory = inventory.getMain();
    final LanternPlayerEquipmentInventory equipmentInventory = inventory.getEquipment();
    final AbstractSlot offHandSlot = inventory.getOffhand();
    for (DataView itemView : itemViews) {
        final int slot = itemView.getByte(SLOT).get() & 0xff;
        final LanternItemStack itemStack = ItemStackStore.INSTANCE.deserialize(itemView);
        if (slot >= 0 && slot < mainInventory.capacity()) {
            mainInventory.set(new SlotIndex(slot), itemStack);
        } else if (slot >= 100 && slot - 100 < equipmentInventory.capacity()) {
            equipmentInventory.set(new SlotIndex(slot - 100), itemStack);
        } else if (slot == 150) {
            offHandSlot.set(itemStack);
        }
    }
}
Also used : DataView(org.spongepowered.api.data.DataView) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) LanternPlayerEquipmentInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerEquipmentInventory) LanternMainPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternMainPlayerInventory) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 19 with AbstractSlot

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

the class HandlerPlayInSignBook method handle.

@Override
public void handle(NetworkContext context, MessagePlayInSignBook message) {
    final LanternPlayer player = context.getSession().getPlayer();
    final AbstractSlot slot = player.getInventory().getHotbar().getSelectedSlot();
    ItemStack itemStack = slot.peek().orElse(null);
    if (itemStack != null && itemStack.getType() == ItemTypes.WRITABLE_BOOK) {
        final ItemStack itemStack1 = new LanternItemStack(ItemTypes.WRITTEN_BOOK);
        itemStack.getValues().forEach(itemStack1::offer);
        itemStack1.offer(Keys.BOOK_PAGES, message.getPages().stream().map(Text::of).collect(Collectors.toList()));
        itemStack1.offer(Keys.BOOK_AUTHOR, Text.of(message.getAuthor()));
        itemStack1.offer(Keys.DISPLAY_NAME, Text.of(message.getTitle()));
        slot.set(itemStack1);
    }
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 20 with AbstractSlot

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

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