Search in sources :

Example 1 with MessagePlayInOutCloseWindow

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutCloseWindow in project LanternServer by LanternPowered.

the class PlayerContainerSession method setRawOpenContainer.

private boolean setRawOpenContainer(CauseStack causeStack, @Nullable LanternContainer container, boolean sendClose, boolean client) {
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        if (this.openContainer != container) {
            frame.addContext(EventContextKeys.PLAYER, this.player);
            ItemStackSnapshot cursorItem = ItemStackSnapshot.NONE;
            if (this.openContainer != null) {
                final ItemStackSnapshot cursorItemSnapshot = this.openContainer.getCursorSlot().peek().map(LanternItemStackSnapshot::wrap).orElse(LanternItemStackSnapshot.none());
                final InteractInventoryEvent.Close event = SpongeEventFactory.createInteractInventoryEventClose(frame.getCurrentCause(), new Transaction<>(cursorItemSnapshot, ItemStackSnapshot.NONE), this.openContainer);
                Sponge.getEventManager().post(event);
                if (event.isCancelled()) {
                    // Stop the client from closing the container, resend the open message
                    if (client) {
                        // This can't be done to the player inventory, player inventory uses index 0
                        // The optional should always return something at this point, otherwise
                        // something is broken
                        final ClientContainer clientContainer = getClientContainer();
                        if (clientContainer.getContainerId() != 0) {
                            // Reinitialize the client container
                            clientContainer.init();
                            return false;
                        }
                    } else {
                        // Just return
                        return false;
                    }
                }
                final Transaction<ItemStackSnapshot> transaction = event.getCursorTransaction();
                if (transaction.isValid()) {
                    if (transaction.getFinal().isEmpty()) {
                        // Add the event that caused the drop to the cause
                        frame.pushCause(event);
                        LanternEventHelper.handleDroppedItemSpawning(this.player.getTransform(), transaction.getOriginal());
                        frame.popCause();
                    } else {
                        cursorItem = transaction.getFinal();
                    }
                }
                // Close the inventory
                this.openContainer.close(causeStack);
            } else {
                sendClose = false;
            }
            if (container != null) {
                final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, cursorItem);
                final InteractInventoryEvent.Open event = SpongeEventFactory.createInteractInventoryEventOpen(frame.getCurrentCause(), cursorTransaction, container);
                Sponge.getEventManager().post(event);
                if (event.isCancelled()) {
                    if (cursorTransaction.isValid()) {
                        final ItemStackSnapshot cursorItem1 = cursorTransaction.getFinal();
                        if (!cursorItem1.isEmpty()) {
                            // Add the event that caused the drop to the cause
                            frame.pushCause(event);
                            LanternEventHelper.handleDroppedItemSpawning(this.player.getTransform(), cursorItem1);
                            frame.popCause();
                        }
                    }
                    return false;
                }
                if (cursorTransaction.isValid()) {
                    final ItemStackSnapshot cursorItem1 = cursorTransaction.getFinal();
                    container.getCursorSlot().setRawItemStack(cursorItem1.createStack());
                }
                sendClose = false;
                container.addViewer(this.player);
            }
            if (sendClose && getContainerId() != 0) {
                this.player.getConnection().send(new MessagePlayInOutCloseWindow(getContainerId()));
            }
            if (this.openContainer != null) {
                this.openContainer.removeViewer(this.player);
            }
        }
        this.openContainer = container;
        return true;
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) Transaction(org.spongepowered.api.data.Transaction) MessagePlayInOutCloseWindow(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutCloseWindow) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) 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) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Aggregations

CauseStack (org.lanternpowered.server.event.CauseStack)1 AnvilClientContainer (org.lanternpowered.server.inventory.client.AnvilClientContainer)1 BeaconClientContainer (org.lanternpowered.server.inventory.client.BeaconClientContainer)1 ClientContainer (org.lanternpowered.server.inventory.client.ClientContainer)1 EnchantmentTableClientContainer (org.lanternpowered.server.inventory.client.EnchantmentTableClientContainer)1 PlayerClientContainer (org.lanternpowered.server.inventory.client.PlayerClientContainer)1 TradingClientContainer (org.lanternpowered.server.inventory.client.TradingClientContainer)1 MessagePlayInOutCloseWindow (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutCloseWindow)1 Transaction (org.spongepowered.api.data.Transaction)1 InteractInventoryEvent (org.spongepowered.api.event.item.inventory.InteractInventoryEvent)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1