use of org.lanternpowered.server.inventory.PlayerInventoryContainer in project LanternServer by LanternPowered.
the class LanternPlayer method openInventory.
@Override
public Optional<Container> openInventory(Inventory inventory) {
checkNotNull(inventory, "inventory");
final LanternContainer container;
if (inventory instanceof LanternContainer) {
// You cannot open a player inventory container
if (inventory instanceof PlayerInventoryContainer) {
return Optional.empty();
}
container = (LanternContainer) inventory;
} else {
inventory.getInventoryProperty(GuiIdProperty.class).map(GuiIdProperty::getValue).orElseThrow(() -> new UnsupportedOperationException("Unsupported inventory type: " + inventory.getArchetype().getId()));
container = LanternContainer.construct(this.inventory, (AbstractOrderedInventory) inventory);
}
if (this.containerSession.setOpenContainer(container)) {
return Optional.of(container);
}
return Optional.empty();
}
use of org.lanternpowered.server.inventory.PlayerInventoryContainer in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method handlePick.
@Override
public void handlePick(ClientContainer clientContainer, @Nullable ClientSlot clientSlot) {
final LanternPlayer player = clientContainer.getPlayer();
if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || !(clientSlot instanceof ClientSlot.Slot)) {
return;
}
final PlayerInventoryContainer inventoryContainer = player.getInventoryContainer();
final ClientSlot hotbarClientSlot = inventoryContainer.getClientContainer().getSelectedHotbarSlot();
if (!(hotbarClientSlot instanceof ClientSlot.Slot)) {
return;
}
final LanternHotbarInventory hotbar = player.getInventory().getHotbar();
final AbstractSlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
// The slot we will swap items with
AbstractSlot hotbarSlot = hotbar.getSelectedSlot();
if (hotbarSlot.peek().isPresent()) {
final Optional<AbstractSlot> optSlot = Streams.stream(hotbar.<AbstractSlot>slots()).filter(slot1 -> !slot1.peek().isPresent()).findFirst();
if (optSlot.isPresent()) {
hotbarSlot = optSlot.get();
}
}
final ItemStack slotItem = slot.peek().orElse(null);
final ItemStack hotbarItem = hotbarSlot.peek().orElse(null);
hotbarSlot.set(slotItem);
hotbar.setSelectedSlotIndex(hotbar.getSlotIndex(hotbarSlot));
slot.set(hotbarItem);
}
use of org.lanternpowered.server.inventory.PlayerInventoryContainer in project LanternServer by LanternPowered.
the class HandlerPlayInSwapHandItems method handle.
@Override
public void handle(NetworkContext context, MessagePlayInSwapHandItems message) {
final LanternPlayer player = context.getSession().getPlayer();
final LanternPlayerInventory inventory = player.getInventory();
final AbstractSlot hotbarSlot = inventory.getHotbar().getSelectedSlot();
final AbstractSlot offHandSlot = inventory.getOffhand();
final ItemStackSnapshot hotbarItem = hotbarSlot.peek().map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE);
final ItemStackSnapshot offHandItem = offHandSlot.peek().map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE);
final List<SlotTransaction> transactions = new ArrayList<>();
transactions.add(new SlotTransaction(hotbarSlot, hotbarItem, offHandItem));
transactions.add(new SlotTransaction(offHandSlot, offHandItem, hotbarItem));
try (CauseStack.Frame frame = CauseStack.current().pushCauseFrame()) {
frame.addContext(EventContextKeys.PLAYER, player);
frame.pushCause(player);
final ChangeInventoryEvent.SwapHand event = SpongeEventFactory.createChangeInventoryEventSwapHand(frame.getCurrentCause(), inventory, transactions);
Sponge.getEventManager().post(event);
if (!event.isCancelled()) {
transactions.stream().filter(Transaction::isValid).forEach(transaction -> transaction.getSlot().set(transaction.getFinal().createStack()));
final PlayerInventoryContainer inventoryContainer = context.getSession().getPlayer().getInventoryContainer();
inventoryContainer.getClientContainer().queueSilentSlotChange(hotbarSlot);
}
}
}
Aggregations