use of net.minecraft.network.play.server.SPacketOpenWindow 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;
}
Aggregations