use of org.lanternpowered.server.inventory.IInventory in project LanternServer by LanternPowered.
the class FurnaceShiftClickBehavior method getTarget.
@Override
public IInventory getTarget(LanternContainer container, AbstractInventorySlot slot) {
if (container.getOpenInventory().containsInventory(slot)) {
return getDefaultTarget(container, slot);
}
// The item stack should be present
// Wrap, peek creates a copy
final ItemStackSnapshot snapshot = LanternItemStackSnapshot.wrap(slot.peek().get());
// Check if the item can be used as a ingredient
final Optional<SmeltingRecipe> optSmeltingRecipe = Lantern.getRegistry().getSmeltingRecipeRegistry().findMatchingRecipe(snapshot);
final List<IInventory> inventories = new ArrayList<>();
final FurnaceInventory furnaceInventory = (FurnaceInventory) container.getOpenInventory();
if (optSmeltingRecipe.isPresent()) {
inventories.add(furnaceInventory.getInputSlot());
}
// Check if the item can be used as a fuel
final Optional<IFuel> optFuel = Lantern.getRegistry().getFuelRegistry().findMatching(snapshot);
if (optFuel.isPresent()) {
inventories.add(furnaceInventory.getFuelSlot());
}
return inventories.isEmpty() ? getDefaultTarget(container, slot) : inventories.size() == 1 ? inventories.get(0) : inventories.get(0).union(inventories.get(1));
}
use of org.lanternpowered.server.inventory.IInventory in project LanternServer by LanternPowered.
the class LanternItem method tryToPickupItems.
private void tryToPickupItems() {
final Set<Entity> entities = getWorld().getIntersectingEntities(getBoundingBox().get().expand(2.0, 0.5, 2.0), entity -> entity != this && entity instanceof Carrier);
if (entities.isEmpty()) {
return;
}
ItemStack itemStack = get(Keys.REPRESENTED_ITEM).map(ItemStackSnapshot::createStack).orElse(null);
if (itemStack == null) {
remove();
return;
}
// TODO: Call pre pickup event
for (Entity entity : entities) {
// Ignore dead entities
if (entity instanceof LanternLiving && ((LanternLiving) entity).isDead()) {
continue;
}
Inventory inventory = ((Carrier) entity).getInventory();
if (inventory instanceof PlayerInventory) {
inventory = ((PlayerInventory) inventory).getMain();
}
final PeekedOfferTransactionResult peekResult = ((IInventory) inventory).peekOffer(itemStack);
final ItemStack rejected = peekResult.getRejectedItem().orElse(null);
final CauseStack causeStack = CauseStack.current();
final ChangeInventoryEvent.Pickup event;
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(LanternEventContextKeys.ORIGINAL_ITEM_STACK, itemStack);
if (rejected != null) {
frame.addContext(LanternEventContextKeys.REST_ITEM_STACK, rejected);
}
event = SpongeEventFactory.createChangeInventoryEventPickup(causeStack.getCurrentCause(), inventory, peekResult.getTransactions());
event.setCancelled(!peekResult.isSuccess());
Sponge.getEventManager().post(event);
}
if (event.isCancelled() && !isRemoved()) {
// Don't continue if the entity was removed during the event
continue;
}
event.getTransactions().stream().filter(Transaction::isValid).forEach(transaction -> transaction.getSlot().set(transaction.getFinal().createStack()));
final int added;
if (rejected != null) {
added = itemStack.getQuantity() - rejected.getQuantity();
itemStack = rejected;
} else {
added = itemStack.getQuantity();
}
if (added != 0 && entity instanceof Living) {
triggerEvent(new CollectEntityEvent((Living) entity, added));
}
if (rejected == null || isRemoved()) {
itemStack = null;
}
if (itemStack == null) {
break;
}
}
if (itemStack != null) {
offer(Keys.REPRESENTED_ITEM, itemStack.createSnapshot());
} else {
remove();
}
}
use of org.lanternpowered.server.inventory.IInventory 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