use of org.spongepowered.api.item.inventory.Slot in project LanternServer by LanternPowered.
the class ContainerTileEntityStore method serialize.
@Override
public void serialize(T object, DataView dataView) {
super.serialize(object, dataView);
final ObjectSerializer<LanternItemStack> itemStackSerializer = ObjectSerializerRegistry.get().get(LanternItemStack.class).get();
final List<DataView> itemViews = new ArrayList<>();
final Inventory inventory = object.getInventory();
final Iterable<Slot> slots = inventory.slots();
for (Slot slot : slots) {
final Optional<ItemStack> optItemStack = slot.peek();
if (!optItemStack.isPresent()) {
continue;
}
final DataView itemView = itemStackSerializer.serialize((LanternItemStack) optItemStack.get());
// noinspection ConstantConditions
itemView.set(SLOT, (byte) inventory.getProperty(slot, SlotIndex.class, null).get().getValue().intValue());
itemViews.add(itemView);
}
dataView.set(ITEMS, itemViews);
}
use of org.spongepowered.api.item.inventory.Slot in project LanternServer by LanternPowered.
the class InventorySnapshot method ofInventory.
/**
* Creates a {@link InventorySnapshot} for the specified {@link Inventory}.
*
* @param inventory The inventory
* @return The snapshot
*/
public static InventorySnapshot ofInventory(Inventory inventory) {
final Iterator<Slot> it = inventory.<Slot>slots().iterator();
final Int2ObjectMap<ItemStackSnapshot> itemStackSnapshots = new Int2ObjectOpenHashMap<>();
while (it.hasNext()) {
final Slot slot = it.next();
slot.peek().map(ItemStack::createSnapshot).ifPresent(itemStackSnapshot -> {
// noinspection ConstantConditions
final SlotIndex index = inventory.getProperty(slot, SlotIndex.class, null).get();
itemStackSnapshots.put(index.getValue(), itemStackSnapshot);
});
}
return new InventorySnapshot(itemStackSnapshots);
}
use of org.spongepowered.api.item.inventory.Slot in project LanternServer by LanternPowered.
the class AbstractChildrenInventory method peek.
@Override
public Optional<ItemStack> peek(int limit, Predicate<ItemStack> matcher) {
checkNotNull(matcher, "matcher");
checkArgument(limit >= 0, "Limit may not be negative");
if (limit == 0) {
return Optional.empty();
}
ItemStack stack = null;
for (AbstractMutableInventory inventory : getChildren()) {
// Check whether the slot a item contains
if (stack == null) {
stack = inventory.peek(limit, matcher).orElse(null);
if (stack != null) {
if (stack.getQuantity() >= limit) {
return Optional.of(stack);
} else {
limit -= stack.getQuantity();
if (!(matcher instanceof SimilarItemMatcher)) {
matcher = new SimilarItemMatcher(stack);
}
}
}
} else {
int peekedStackSize = 0;
// boxing/unboxing and cloning the item stack
if (inventory instanceof Slot) {
final ItemStack stack1 = ((AbstractSlot) inventory).getRawItemStack();
if (stack1 != null && matcher.test(stack1)) {
peekedStackSize = Math.min(((Slot) inventory).getStackSize(), limit);
}
} else {
final Optional<ItemStack> optItemStack = inventory.peek(limit, matcher);
if (optItemStack.isPresent()) {
peekedStackSize = optItemStack.get().getQuantity();
}
}
if (peekedStackSize > 0) {
limit -= peekedStackSize;
stack.setQuantity(stack.getQuantity() + peekedStackSize);
if (limit <= 0) {
return Optional.of(stack);
}
}
}
}
return Optional.ofNullable(stack);
}
use of org.spongepowered.api.item.inventory.Slot in project Nucleus by NucleusPowered.
the class SellAllCommand method executeCommand.
@Override
public CommandResult executeCommand(final Player src, CommandContext args) throws Exception {
boolean accepted = args.hasAny("a");
CatalogType ct = getCatalogTypeFromHandOrArgs(src, itemKey, args);
String id = ct.getId();
ItemStack query;
if (ct instanceof BlockState) {
query = ItemStack.builder().fromBlockState((BlockState) ct).quantity(1).build();
// Yeah...
query.setQuantity(-1);
} else {
// Having a quantity of -1 causes an IllegalArgumentException here...
query = ItemStack.of((ItemType) ct, 1);
// and doesn't care here...
query.setQuantity(-1);
}
ItemDataNode node = itemDataService.getDataForItem(id);
final double sellPrice = node.getServerSellPrice();
if (sellPrice < 0) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.itemsell.notforselling"));
}
Iterable<Slot> slots = Util.getStandardInventory(src).query(query).slots();
List<ItemStack> itemsToSell = StreamSupport.stream(Util.getStandardInventory(src).query(query).slots().spliterator(), false).map(Inventory::peek).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
// Get the cost.
final int amt = itemsToSell.stream().mapToInt(ItemStack::getQuantity).sum();
if (amt <= 0) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsellall.none", Text.of(query)));
}
final double overallCost = sellPrice * amt;
if (accepted) {
if (econHelper.depositInPlayer(src, overallCost, false)) {
slots.forEach(Inventory::clear);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.summary", Text.of(amt), Text.of(query), Text.of(econHelper.getCurrencySymbol(overallCost))));
return CommandResult.success();
}
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.error", Text.of(query)));
}
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsellall.summary", Text.of(amt), Text.of(query), Text.of(econHelper.getCurrencySymbol(overallCost)), Text.of(id)).toBuilder().onClick(TextActions.runCommand("/nucleus:itemsellall -a " + id)).build());
return CommandResult.success();
}
Aggregations