Search in sources :

Example 1 with Slot

use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.

the class CloseMenuTransaction method reopen.

private void reopen(final ServerPlayer player, final AbstractContainerMenu container) {
    if (container.getSlot(0) == null) {
        return;
    }
    if (!(container instanceof InventoryMenu)) {
        // Inventory closed by client, reopen window and send container
        player.containerMenu = container;
        final Slot slot = container.getSlot(0);
        final net.minecraft.world.Container slotInventory = slot.container;
        final net.minecraft.network.chat.@Nullable Component title;
        // TODO get name from last open
        if (slotInventory instanceof MenuProvider) {
            title = ((MenuProvider) slotInventory).getDisplayName();
        } else {
            // expected fallback for unknown types
            title = null;
        }
        slotInventory.startOpen(player);
        player.connection.send(new ClientboundOpenScreenPacket(container.containerId, container.getType(), title));
        // resync data to client
        player.refreshContainer(container);
    } else {
    // TODO: Maybe print a warning or throw an exception here?
    // The player gui cannot be opened from the
    // server so allowing this event to be cancellable when the
    // GUI has been closed already would result
    // in opening the wrong GUI window.
    }
}
Also used : InventoryMenu(net.minecraft.world.inventory.InventoryMenu) Slot(net.minecraft.world.inventory.Slot) ClientboundOpenScreenPacket(net.minecraft.network.protocol.game.ClientboundOpenScreenPacket) MenuProvider(net.minecraft.world.MenuProvider) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with Slot

use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.

the class SpongeInventoryMenu method setCurrentInventory.

@Override
public void setCurrentInventory(ViewableInventory inventory) {
    if (inventory.getClass().equals(this.inventory.getClass()) && inventory instanceof ViewableCustomInventory && inventory.capacity() == this.inventory.capacity()) {
        this.inventory = inventory;
        for (Map.Entry<Container, ServerPlayer> entry : this.tracked.entrySet()) {
            final net.minecraft.world.inventory.AbstractContainerMenu container = (net.minecraft.world.inventory.AbstractContainerMenu) entry.getKey();
            final ServerPlayer player = entry.getValue();
            // create a new container for the viewable inventory
            final net.minecraft.world.inventory.AbstractContainerMenu newContainer = ((ViewableCustomInventory) inventory).createMenu(-1, ((Player) player).inventory, (Player) player);
            for (int i = 0; i < inventory.capacity(); i++) {
                // And put its slots into the old container
                final Slot slot = newContainer.slots.get(i);
                container.slots.set(i, slot);
                // Update Container items
                ((AbstractContainerMenuAccessor) container).accessor$lastSlots().set(i, slot.getItem());
            }
            // send update to Client
            for (ContainerListener listener : ((AbstractContainerMenuAccessor) container).accessor$containerListeners()) {
                listener.refreshContainer(container, ((AbstractContainerMenuAccessor) container).accessor$lastSlots());
            }
        }
    } else {
        // Get all distinct players and reopen inventory for them
        this.inventory = inventory;
        this.reopen();
    }
}
Also used : Container(org.spongepowered.api.item.inventory.Container) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) AbstractContainerMenuAccessor(org.spongepowered.common.accessor.world.inventory.AbstractContainerMenuAccessor) Slot(net.minecraft.world.inventory.Slot) ContainerListener(net.minecraft.world.inventory.ContainerListener) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Slot

use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.

the class SpongeInventoryMenu method onClick.

public boolean onClick(int slotId, int dragType, ClickType clickTypeIn, Player player, Container container) {
    try (CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(player);
        Cause cause = frame.currentCause();
        if (clickTypeIn == ClickType.QUICK_CRAFT) {
            return this.onClickDrag(cause, slotId, dragType, container);
        }
        Optional<org.spongepowered.api.item.inventory.Slot> slot = container.slot(slotId);
        if (slot.isPresent()) {
            switch(clickTypeIn) {
                case SWAP:
                    if (dragType >= 0 && dragType < 9) {
                        Optional<org.spongepowered.api.item.inventory.Slot> slot2 = container.slot(dragType);
                        if (slot2.isPresent() && this.keySwapHandler != null) {
                            return this.keySwapHandler.handle(cause, container, slot.get(), slotId, ClickTypes.KEY_SWAP.get(), slot2.get());
                        }
                    }
                    break;
                case CLONE:
                    if (this.slotClickHandler != null) {
                        return this.slotClickHandler.handle(cause, container, slot.get(), slotId, ClickTypes.CLICK_MIDDLE.get());
                    }
                    break;
                case PICKUP_ALL:
                    if (this.slotClickHandler != null) {
                        return this.slotClickHandler.handle(cause, container, slot.get(), slotId, ClickTypes.DOUBLE_CLICK.get());
                    }
                    break;
                default:
                    if (this.slotClickHandler != null) {
                        if (dragType == 0) {
                            return this.onClickLeft(cause, this.slotClickHandler, clickTypeIn, container, slotId, slot.get());
                        } else if (dragType == 1) {
                            return this.onClickRight(cause, this.slotClickHandler, clickTypeIn, container, slotId, slot.get());
                        }
                    // else unknown drag-type
                    }
            }
            return true;
        }
        // else no slot present
        switch(clickTypeIn) {
            case PICKUP:
                if (slotId == -999 && this.clickHandler != null) {
                    if (dragType == 0) {
                        return this.clickHandler.handle(cause, container, ClickTypes.CLICK_LEFT_OUTSIDE.get());
                    } else if (dragType == 1) {
                        return this.clickHandler.handle(cause, container, ClickTypes.CLICK_RIGHT_OUTSIDE.get());
                    }
                }
                // else unknown slotId/drag-type
                break;
            case THROW:
                if (slotId == -999) {
                // TODO check packets - does THROW with slotid -999 exist or is this actually PICKUP?
                // its supposed to be l/r-click with nothing in hand
                // TODO check if those exist
                // /**
                // * Throwing one item on the cursor by clicking outside the inventory window.
                // */
                // public static final org.spongepowered.api.item.inventory.menu.ClickType
                // CLICK_THROW_ONE = DummyObjectProvider.createFor(org.spongepowered.api.item.inventory.menu.ClickType.class, "click_throw_one");
                // /**
                // * Throwing all items on the cursor by clicking outside the inventory window.
                // */
                // public static final org.spongepowered.api.item.inventory.menu.ClickType
                // CLICK_THROW_ALL = DummyObjectProvider.createFor(org.spongepowered.api.item.inventory.menu.ClickType.class, "click_throw_all");
                }
                // else unknown slotId/drag-type
                break;
        }
        return true;
    }
}
Also used : CauseStackManager(org.spongepowered.api.event.CauseStackManager) Cause(org.spongepowered.api.event.Cause) Slot(net.minecraft.world.inventory.Slot)

Example 4 with Slot

use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.

the class PacketPhaseUtil method handleSlotRestore.

public static void handleSlotRestore(@Nullable final Player player, @Nullable final AbstractContainerMenu containerMenu, final List<SlotTransaction> slotTransactions, final boolean eventCancelled) {
    try (PhaseContext<@NonNull ?> ignored = BlockPhase.State.RESTORING_BLOCKS.createPhaseContext(PhaseTracker.SERVER).buildAndSwitch()) {
        boolean restoredAny = false;
        for (final SlotTransaction slotTransaction : slotTransactions) {
            if ((!slotTransaction.custom().isPresent() && slotTransaction.isValid()) && !eventCancelled) {
                continue;
            }
            restoredAny = true;
            final org.spongepowered.api.item.inventory.Slot slot = slotTransaction.slot();
            final ItemStackSnapshot snapshot = eventCancelled || !slotTransaction.isValid() ? slotTransaction.original() : slotTransaction.custom().get();
            if (containerMenu == null || slot.viewedSlot() == slot) {
                slot.set(snapshot.createStack());
            } else {
                final int slotNumber = ((SlotAdapter) slot).getOrdinal();
                final Slot nmsSlot = containerMenu.getSlot(slotNumber);
                if (nmsSlot != null) {
                    nmsSlot.set(ItemStackUtil.fromSnapshotToNative(snapshot));
                }
            }
        }
        if (restoredAny && player instanceof net.minecraft.server.level.ServerPlayer) {
            if (containerMenu != null) {
                containerMenu.broadcastChanges();
                if (player.containerMenu == containerMenu) {
                    ((net.minecraft.server.level.ServerPlayer) player).refreshContainer(containerMenu);
                }
            } else {
                player.inventoryMenu.broadcastChanges();
            }
        }
    }
}
Also used : SlotAdapter(org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Slot(net.minecraft.world.inventory.Slot)

Example 5 with Slot

use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.

the class PacketPhaseUtil method validateCapturedTransactions.

public static void validateCapturedTransactions(final int slotId, final AbstractContainerMenu containerMenu, final List<SlotTransaction> capturedTransactions) {
    if (capturedTransactions.size() == 0 && slotId >= 0 && slotId < containerMenu.slots.size()) {
        final Slot slot = containerMenu.getSlot(slotId);
        if (slot != null) {
            final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(slot.getItem());
            final SlotTransaction slotTransaction = new SlotTransaction(((InventoryAdapter) containerMenu).inventoryAdapter$getSlot(slotId).get(), snapshot, snapshot);
            capturedTransactions.add(slotTransaction);
        }
    }
}
Also used : InventoryAdapter(org.spongepowered.common.inventory.adapter.InventoryAdapter) Slot(net.minecraft.world.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Aggregations

Slot (net.minecraft.world.inventory.Slot)12 ContainerListener (net.minecraft.world.inventory.ContainerListener)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CraftingContainer (net.minecraft.world.inventory.CraftingContainer)2 ResultContainer (net.minecraft.world.inventory.ResultContainer)2 ItemStack (net.minecraft.world.item.ItemStack)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)2 Container (org.spongepowered.api.item.inventory.Container)2 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)2 AbstractContainerMenuAccessor (org.spongepowered.common.accessor.world.inventory.AbstractContainerMenuAccessor)2 ResultSlotAccessor (org.spongepowered.common.accessor.world.inventory.ResultSlotAccessor)2 InventoryBridge (org.spongepowered.common.bridge.world.inventory.InventoryBridge)2 Lens (org.spongepowered.common.inventory.lens.Lens)2 CompoundLens (org.spongepowered.common.inventory.lens.impl.CompoundLens)2 DelegatingLens (org.spongepowered.common.inventory.lens.impl.DelegatingLens)2 CraftingInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens)2 GridInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens)2