Search in sources :

Example 1 with IInteractionObject

use of net.minecraft.world.IInteractionObject in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callInteractInventoryCloseEvent.

public static InteractInventoryEvent.Close callInteractInventoryCloseEvent(Container container, EntityPlayerMP player, ItemStackSnapshot lastCursor, ItemStackSnapshot newCursor, boolean clientSource) {
    Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(lastCursor, newCursor);
    final InteractInventoryEvent.Close event = SpongeEventFactory.createInteractInventoryEventClose(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, ContainerUtil.fromNative(container));
    SpongeImpl.postEvent(event);
    if (event.isCancelled()) {
        if (clientSource && container.getSlot(0) != null) {
            if (!(container instanceof ContainerPlayer)) {
                // Inventory closed by client, reopen window and send
                // container
                player.openContainer = container;
                final String guiId;
                final Slot slot = container.getSlot(0);
                final IInventory slotInventory = slot.inventory;
                if (slotInventory instanceof IInteractionObject) {
                    guiId = ((IInteractionObject) slotInventory).getGuiID();
                } else {
                    // expected fallback for unknown types
                    guiId = "minecraft:container";
                }
                slotInventory.openInventory(player);
                player.connection.sendPacket(new SPacketOpenWindow(container.windowId, guiId, slotInventory.getDisplayName(), slotInventory.getSizeInventory()));
                // resync data to client
                player.sendContainerToPlayer(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.
            }
        }
        // Handle cursor
        if (!event.getCursorTransaction().isValid()) {
            handleCustomCursor(player, event.getCursorTransaction().getOriginal());
        }
    } else {
        IMixinContainer mixinContainer = (IMixinContainer) player.openContainer;
        mixinContainer.getCapturedTransactions().clear();
        mixinContainer.setCaptureInventory(false);
        // Handle cursor
        if (!event.getCursorTransaction().isValid()) {
            handleCustomCursor(player, event.getCursorTransaction().getOriginal());
        } else if (event.getCursorTransaction().getCustom().isPresent()) {
            handleCustomCursor(player, event.getCursorTransaction().getFinal());
        }
        if (!clientSource && player.openContainer != null && player.connection != null) {
            player.closeScreen();
        }
    }
    return event;
}
Also used : ContainerPlayer(net.minecraft.inventory.ContainerPlayer) IInventory(net.minecraft.inventory.IInventory) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) IInteractionObject(net.minecraft.world.IInteractionObject) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot) SPacketOpenWindow(net.minecraft.network.play.server.SPacketOpenWindow)

Example 2 with IInteractionObject

use of net.minecraft.world.IInteractionObject in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method displayContainer.

@Nullable
public static Container displayContainer(EntityPlayerMP player, Inventory inventory) {
    net.minecraft.inventory.Container previousContainer = player.openContainer;
    net.minecraft.inventory.Container container;
    if (inventory instanceof CustomInventory) {
        if (!checkValidVanillaCustomInventory(((CustomInventory) inventory))) {
            // Invalid size for vanilla inventory ; This is to
            return null;
        // prevent crashing the client with invalid data
        }
    }
    if (inventory instanceof IInteractionObject) {
        final String guiId = ((IInteractionObject) inventory).getGuiID();
        switch(guiId) {
            case "EntityHorse":
                if (inventory instanceof CarriedInventory) {
                    CarriedInventory<?> cinventory = (CarriedInventory<?>) inventory;
                    if (cinventory.getCarrier().isPresent() && cinventory.getCarrier().get() instanceof AbstractHorse) {
                        player.openGuiHorseInventory(((AbstractHorse) cinventory.getCarrier().get()), (IInventory) inventory);
                    }
                }
                break;
            case "minecraft:chest":
                player.displayGUIChest((IInventory) inventory);
                break;
            case "minecraft:crafting_table":
            case "minecraft:anvil":
            case "minecraft:enchanting_table":
                player.displayGui((IInteractionObject) inventory);
                break;
            default:
                player.displayGUIChest((IInventory) inventory);
                break;
        }
    } else if (inventory instanceof IInventory) {
        player.displayGUIChest(((IInventory) inventory));
    } else {
        return null;
    }
    container = player.openContainer;
    if (previousContainer == container) {
        return null;
    }
    if (!callInteractInventoryOpenEvent(player)) {
        return null;
    }
    if (container instanceof IMixinContainer) {
        // This overwrites the normal container behaviour and allows viewing
        // inventories that are more than 8 blocks away
        // This currently actually only works for the Containers mixed into
        // by MixinContainerCanInteract ; but throws no errors for other
        // containers
        // Allow viewing inventory; except when dead
        ((IMixinContainer) container).setCanInteractWith(p -> !p.isDead);
    }
    return container;
}
Also used : IInventory(net.minecraft.inventory.IInventory) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) AbstractHorse(net.minecraft.entity.passive.AbstractHorse) IInteractionObject(net.minecraft.world.IInteractionObject) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) CustomInventory(org.spongepowered.common.item.inventory.custom.CustomInventory) Container(net.minecraft.inventory.Container) Nullable(javax.annotation.Nullable)

Aggregations

IInventory (net.minecraft.inventory.IInventory)2 IInteractionObject (net.minecraft.world.IInteractionObject)2 IMixinContainer (org.spongepowered.common.interfaces.IMixinContainer)2 Nullable (javax.annotation.Nullable)1 AbstractHorse (net.minecraft.entity.passive.AbstractHorse)1 Container (net.minecraft.inventory.Container)1 ContainerPlayer (net.minecraft.inventory.ContainerPlayer)1 Slot (net.minecraft.inventory.Slot)1 SPacketOpenWindow (net.minecraft.network.play.server.SPacketOpenWindow)1 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)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 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)1 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)1 CustomInventory (org.spongepowered.common.item.inventory.custom.CustomInventory)1