Search in sources :

Example 1 with LanternPlayerInventory

use of org.lanternpowered.server.inventory.vanilla.LanternPlayerInventory 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

ArrayList (java.util.ArrayList)1 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)1 CauseStack (org.lanternpowered.server.event.CauseStack)1 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)1 PlayerInventoryContainer (org.lanternpowered.server.inventory.PlayerInventoryContainer)1 LanternPlayerInventory (org.lanternpowered.server.inventory.vanilla.LanternPlayerInventory)1 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)1