Search in sources :

Example 1 with AbstractContainerMenuAccessor

use of org.spongepowered.common.accessor.world.inventory.AbstractContainerMenuAccessor 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)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 ContainerListener (net.minecraft.world.inventory.ContainerListener)1 Slot (net.minecraft.world.inventory.Slot)1 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)1 Container (org.spongepowered.api.item.inventory.Container)1 AbstractContainerMenuAccessor (org.spongepowered.common.accessor.world.inventory.AbstractContainerMenuAccessor)1