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;
}
}
Aggregations