use of org.lanternpowered.server.inventory.client.ClientSlot in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method handleNumberKey.
@Override
public void handleNumberKey(ClientContainer clientContainer, ClientSlot clientSlot, int number) {
if (clientContainer.getPlayer() != this.container.getPlayerInventory().getCarrier().orElse(null) || !(clientSlot instanceof ClientSlot.Slot)) {
return;
}
final ClientSlot hotbarSlot = clientContainer.getClientSlot(clientContainer.getHotbarSlotIndex(number - 1)).orElseThrow(() -> new IllegalStateException("Missing hotbar client slot: " + number));
if (!(hotbarSlot instanceof ClientSlot.Slot)) {
return;
}
final AbstractSlot slot1 = ((ClientSlot.Slot) clientSlot).getSlot();
final AbstractSlot hotbarSlot1 = ((ClientSlot.Slot) hotbarSlot).getSlot();
if (slot1 != hotbarSlot1) {
final List<SlotTransaction> transactions = new ArrayList<>();
final Transaction<ItemStackSnapshot> cursorTransaction;
if (getCursorItem() == null) {
cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
final ItemStack itemStack = slot1.getRawItemStack();
final ItemStack hotbarItemStack = hotbarSlot1.getRawItemStack();
final ItemStackSnapshot itemStackSnapshot = LanternItemStack.toSnapshot(itemStack);
final ItemStackSnapshot hotbarItemStackSnapshot = LanternItemStack.toSnapshot(hotbarItemStack);
if (!(itemStackSnapshot != ItemStackSnapshot.NONE && (!hotbarSlot1.isValidItem(itemStack) || itemStackSnapshot.getQuantity() > hotbarSlot1.getMaxStackSize())) && !(hotbarItemStackSnapshot != ItemStackSnapshot.NONE && (!slot1.isValidItem(hotbarItemStack) || hotbarItemStack.getQuantity() > slot1.getMaxStackSize()))) {
transactions.add(new SlotTransaction(slot1, itemStackSnapshot, hotbarItemStackSnapshot));
transactions.add(new SlotTransaction(hotbarSlot1, hotbarItemStackSnapshot, itemStackSnapshot));
}
} else {
final ItemStackSnapshot cursorItem = getCursorItem().createSnapshot();
cursorTransaction = new Transaction<>(cursorItem, cursorItem);
}
final CauseStack causeStack = CauseStack.current();
final ClickInventoryEvent.NumberPress event = SpongeEventFactory.createClickInventoryEventNumberPress(causeStack.getCurrentCause(), cursorTransaction, this.container, this.container.transformSlots(transactions), number - 1);
finishInventoryEvent(event);
}
}
use of org.lanternpowered.server.inventory.client.ClientSlot in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method handleCreativeClick.
@Override
public void handleCreativeClick(ClientContainer clientContainer, @Nullable ClientSlot clientSlot, @Nullable ItemStack itemStack) {
final LanternPlayer player = clientContainer.getPlayer();
final CauseStack causeStack = CauseStack.current();
if (clientSlot == null) {
if (itemStack != null) {
causeStack.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
LanternEventHelper.handleDroppedItemSpawning(player.getTransform(), itemStack.createSnapshot());
}
} else if (clientSlot instanceof ClientSlot.Slot) {
final AbstractSlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
final PeekedSetTransactionResult result = slot.peekSet(itemStack);
// We do not know the remaining stack in the cursor,
// so just use none as new item
final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(LanternItemStack.toSnapshot(itemStack), ItemStackSnapshot.NONE);
final ClickInventoryEvent.Creative event = SpongeEventFactory.createClickInventoryEventCreative(causeStack.getCurrentCause(), cursorTransaction, this.container, this.container.transformSlots(result.getTransactions()));
finishInventoryEvent(event);
}
}
use of org.lanternpowered.server.inventory.client.ClientSlot in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method handleDoubleClick.
@Override
public void handleDoubleClick(ClientContainer clientContainer, ClientSlot clientSlot) {
final LanternPlayer player = clientContainer.getPlayer();
if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || !(clientSlot instanceof ClientSlot.Slot)) {
return;
}
final AbstractSlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
final ItemStackSnapshot oldItem = LanternItemStack.toSnapshot(getCursorItem());
ItemStackSnapshot newItem = oldItem;
final List<SlotTransaction> transactions = new ArrayList<>();
if (getCursorItem() != null && !(slot instanceof OutputSlot)) {
final ItemStack cursorItem = getCursorItem().copy();
int quantity = cursorItem.getQuantity();
final int maxQuantity = cursorItem.getMaxStackQuantity();
if (quantity < maxQuantity) {
final AbstractInventory inventory;
if (clientContainer instanceof PlayerClientContainer) {
inventory = this.container.getPlayerInventory().getView(LanternPlayerInventory.View.ALL_PRIORITY_MAIN);
} else {
inventory = AbstractOrderedInventory.viewBuilder().inventory(this.container.getOpenInventory()).inventory(this.container.getPlayerInventory().getView(LanternPlayerInventory.View.PRIORITY_MAIN_AND_HOTBAR)).build();
}
// Try first to get enough unfinished stacks
PeekedPollTransactionResult peekResult = inventory.peekPoll(maxQuantity - quantity, stack -> stack.getQuantity() < stack.getMaxStackQuantity() && ((LanternItemStack) cursorItem).similarTo(stack)).orElse(null);
if (peekResult != null) {
quantity += peekResult.getPolledItem().getQuantity();
transactions.addAll(peekResult.getTransactions());
}
// Get the last items for the stack from a full stack
if (quantity <= maxQuantity) {
peekResult = this.container.peekPoll(maxQuantity - quantity, stack -> stack.getQuantity() >= stack.getMaxStackQuantity() && ((LanternItemStack) cursorItem).similarTo(stack)).orElse(null);
if (peekResult != null) {
quantity += peekResult.getPolledItem().getQuantity();
transactions.addAll(peekResult.getTransactions());
}
}
cursorItem.setQuantity(quantity);
newItem = cursorItem.createSnapshot();
}
}
final CauseStack causeStack = CauseStack.current();
final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(oldItem, newItem);
final ClickInventoryEvent.Double event = SpongeEventFactory.createClickInventoryEventDouble(causeStack.getCurrentCause(), cursorTransaction, this.container, this.container.transformSlots(transactions));
finishInventoryEvent(event);
}
use of org.lanternpowered.server.inventory.client.ClientSlot in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method handleDrag.
@Override
public void handleDrag(ClientContainer clientContainer, List<ClientSlot> clientSlots, MouseButton mouseButton) {
final LanternPlayer player = clientContainer.getPlayer();
if (player != this.container.getPlayerInventory().getCarrier().orElse(null)) {
return;
}
final List<AbstractSlot> slots = clientSlots.stream().filter(clientSlot -> clientSlot instanceof ClientSlot.Slot).map(clientSlot -> ((ClientSlot.Slot) clientSlot).getSlot()).collect(Collectors.toList());
if (slots.size() != clientSlots.size()) {
// TODO: Is this the behavior we want?
return;
}
final ItemStack cursorItem = getCursorItem();
if (cursorItem == null || cursorItem.isEmpty()) {
return;
}
final CauseStack causeStack = CauseStack.current();
if (mouseButton == MouseButton.LEFT) {
final int quantity = cursorItem.getQuantity();
final int slotCount = slots.size();
final int itemsPerSlot = quantity / slotCount;
final int rest = quantity - itemsPerSlot * slotCount;
final List<SlotTransaction> transactions = new ArrayList<>();
for (AbstractSlot slot : slots) {
final ItemStack itemStack = cursorItem.copy();
itemStack.setQuantity(itemsPerSlot);
transactions.addAll(slot.peekOffer(itemStack).getTransactions());
}
ItemStackSnapshot newCursorItem = ItemStackSnapshot.NONE;
if (rest > 0) {
final ItemStack itemStack = cursorItem.copy();
itemStack.setQuantity(rest);
newCursorItem = LanternItemStackSnapshot.wrap(itemStack);
}
final ItemStackSnapshot oldCursorItem = cursorItem.createSnapshot();
final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(oldCursorItem, newCursorItem);
final ClickInventoryEvent.Drag.Primary event = SpongeEventFactory.createClickInventoryEventDragPrimary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions);
finishInventoryEvent(event);
} else if (mouseButton == MouseButton.RIGHT) {
int quantity = cursorItem.getQuantity();
final int size = Math.min(slots.size(), quantity);
final List<SlotTransaction> transactions = new ArrayList<>();
for (AbstractSlot slot : slots) {
final ItemStack itemStack = cursorItem.copy();
itemStack.setQuantity(1);
transactions.addAll(slot.peekOffer(itemStack).getTransactions());
}
quantity -= size;
ItemStackSnapshot newCursorItem = ItemStackSnapshot.NONE;
if (quantity > 0) {
final ItemStack itemStack = cursorItem.copy();
itemStack.setQuantity(quantity);
newCursorItem = LanternItemStackSnapshot.wrap(itemStack);
}
final ItemStackSnapshot oldCursorItem = getCursorItem().createSnapshot();
final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(oldCursorItem, newCursorItem);
final ClickInventoryEvent.Drag.Secondary event = SpongeEventFactory.createClickInventoryEventDragSecondary(causeStack.getCurrentCause(), cursorTransaction, this.container, this.container.transformSlots(transactions));
finishInventoryEvent(event);
} else {
// TODO: Middle mouse drag mode
}
}
use of org.lanternpowered.server.inventory.client.ClientSlot in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method handleShiftClick.
@Override
public void handleShiftClick(ClientContainer clientContainer, ClientSlot clientSlot, MouseButton mouseButton) {
final LanternPlayer player = clientContainer.getPlayer();
if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || !(clientSlot instanceof ClientSlot.Slot) || mouseButton == MouseButton.MIDDLE) {
return;
}
final AbstractInventorySlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
final ItemStack itemStack = slot.peek().orElse(null);
final Transaction<ItemStackSnapshot> cursorTransaction;
final List<SlotTransaction> transactions = new ArrayList<>();
if (slot instanceof CraftingOutput) {
final ItemStackSnapshot cursorItem = LanternItemStack.toSnapshot(getCursorItem());
cursorTransaction = new Transaction<>(cursorItem, cursorItem);
final AbstractInventory parent = slot.parent();
if (parent instanceof CraftingInventory) {
final CraftingInventory inventory = (CraftingInventory) parent;
final Optional<ExtendedCraftingResult> optResult = Lantern.getRegistry().getCraftingRecipeRegistry().getExtendedResult(inventory.getCraftingGrid(), player.getWorld());
if (optResult.isPresent()) {
final ExtendedCraftingResult result = optResult.get();
final ItemStackSnapshot resultItem = result.getResult().getResult();
int times = result.getMaxTimes();
final ItemStack itemStack1 = resultItem.createStack();
itemStack1.setQuantity(times * itemStack1.getQuantity());
final AbstractInventory targetInventory = this.container.getPlayerInventory().getView(LanternPlayerInventory.View.REVERSE_MAIN_AND_HOTBAR);
PeekedOfferTransactionResult peekResult = targetInventory.peekOffer(itemStack1);
if (peekResult.isSuccess()) {
transactions.add(new SlotTransaction(slot, resultItem, ItemStackSnapshot.NONE));
final ItemStack rejectedItem = peekResult.getRejectedItem().orElse(null);
if (rejectedItem != null) {
final int added = itemStack1.getQuantity() - rejectedItem.getQuantity();
times = added / resultItem.getQuantity();
final int diff = added % resultItem.getQuantity();
if (diff != 0) {
itemStack1.setQuantity(resultItem.getQuantity() * times);
peekResult = targetInventory.peekOffer(itemStack1);
checkState(peekResult.isSuccess());
}
}
transactions.addAll(peekResult.getTransactions());
updateCraftingGrid(player, inventory, result.getMatrixResult(times), transactions);
}
} else {
// No actual transaction, there shouldn't have been a item in the crafting result slot
transactions.add(new SlotTransaction(slot, ItemStackSnapshot.NONE, ItemStackSnapshot.NONE));
}
} else {
Lantern.getLogger().warn("Found a CraftingOutput slot without a CraftingInventory as parent.");
return;
}
} else {
final ItemStackSnapshot cursorItem = LanternItemStack.toSnapshot(getCursorItem());
cursorTransaction = new Transaction<>(cursorItem, cursorItem);
if (itemStack != null) {
final IInventory target = this.container.getOpenInventory().getShiftClickBehavior().getTarget(this.container, slot);
final PeekedOfferTransactionResult result = target.peekOffer(itemStack.copy());
if (result.isSuccess()) {
transactions.addAll(result.getTransactions());
final ItemStack rejectedItem = result.getRejectedItem().orElse(null);
if (rejectedItem != null) {
slot.peekPoll(itemStack.getQuantity() - rejectedItem.getQuantity(), stack -> true).ifPresent(peekResult -> transactions.addAll(peekResult.getTransactions()));
} else {
slot.peekPoll(stack -> true).ifPresent(peekResult -> transactions.addAll(peekResult.getTransactions()));
}
}
}
}
final List<SlotTransaction> transactions1 = this.container.transformSlots(transactions);
final CauseStack causeStack = CauseStack.current();
final ClickInventoryEvent.Shift event;
if (mouseButton == MouseButton.LEFT) {
event = SpongeEventFactory.createClickInventoryEventShiftPrimary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions1);
} else {
event = SpongeEventFactory.createClickInventoryEventShiftSecondary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions1);
}
finishInventoryEvent(event);
}
Aggregations