use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class KeyValueMatcherQuery method execute.
@Override
public Inventory execute(final Inventory inventory, final InventoryAdapter adapter) {
final Fabric fabric = adapter.inventoryAdapter$getFabric();
final Lens lens = adapter.inventoryAdapter$getRootLens();
if (this.matches(lens, null, inventory)) {
return lens.getAdapter(fabric, inventory);
}
return this.toResult(inventory, fabric, this.reduce(fabric, lens, this.search(inventory, lens)));
}
use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class KeyValueMatcherQuery method depthLaterSearch.
private Map<Lens, Integer> depthLaterSearch(final Inventory inventory, final Lens lens) {
final Map<Lens, Integer> matches = new LinkedHashMap<>();
// Search for any match with the direct children
for (final Lens child : lens.getChildren()) {
if (child == null) {
continue;
}
if (this.matches(child, lens, inventory)) {
matches.put(child, 0);
}
}
// If no match was found go one layer deeper
if (matches.isEmpty()) {
for (final Lens child : lens.getChildren()) {
if (child == null) {
continue;
}
if (!child.getChildren().isEmpty()) {
matches.putAll(this.depthLaterSearch(inventory, child));
}
}
}
this.delegateOffset(lens, matches);
return matches;
}
use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class KeyValueMatcherQuery method search.
private Map<Lens, Integer> search(final Inventory inventory, final Lens lens) {
if (inventory instanceof AbstractContainerMenu) {
final Map<Lens, Integer> matches = new LinkedHashMap<>();
// Search for Container Slot properties
for (final net.minecraft.world.inventory.Slot slot : ((AbstractContainerMenu) inventory).slots) {
if (this.matches(null, null, (Inventory) slot)) {
matches.put(((InventoryAdapter) slot).inventoryAdapter$getRootLens(), 0);
}
}
if (!matches.isEmpty()) {
return matches;
}
// Search for Container Viewed inventory properties
final Set<Inventory> viewedInventories = new HashSet<>();
for (final Slot slot : inventory.slots()) {
viewedInventories.add(slot.viewedSlot().parent());
}
// TODO does this work?
for (final Inventory viewedInventory : viewedInventories) {
if (this.matches(null, null, viewedInventory)) {
matches.put(((InventoryAdapter) viewedInventory).inventoryAdapter$getRootLens(), 0);
}
}
if (!matches.isEmpty()) {
this.delegateOffset(lens, matches);
return matches;
}
}
return this.depthLaterSearch(inventory, lens);
}
use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class SpongeDepthQuery method execute.
public Inventory execute(Inventory inventory, InventoryAdapter adapter) {
final Fabric fabric = adapter.inventoryAdapter$getFabric();
final Lens lens = adapter.inventoryAdapter$getRootLens();
if (this.matches(lens, null, inventory)) {
return lens.getAdapter(fabric, inventory);
}
return this.toResult(inventory, fabric, this.reduce(fabric, lens, this.depthFirstSearch(inventory, lens)));
}
use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class SpongeQuery method reduce.
protected Map<Lens, Integer> reduce(Fabric fabric, Lens lens, Map<Lens, Integer> matches) {
if (matches.isEmpty()) {
return Collections.emptyMap();
}
// Check if all matches are the direct children of this lens
List<Lens> lensSlots = lens.getChildren();
if (lensSlots.size() == matches.size() && matches.keySet().containsAll(lensSlots)) {
// return parent lens instead of constructing a new for the query result
matches.clear();
matches.put(lens, 0);
return matches;
}
// Remove duplicate slot-lenses
Map<SlotLens, Map<Key<?>, Object>> lenses = new LinkedHashMap<>();
Map<Lens, Integer> toRemove = new HashMap<>();
for (Map.Entry<Lens, Integer> entry : matches.entrySet()) {
final Lens slotLens = entry.getKey();
if (slotLens.slotCount() == 1) {
// Remove Lens with one slot
toRemove.put(slotLens, matches.get(slotLens));
// Find SlotLens for that Lens
final SlotLens sl = slotLens.getSlotLens(fabric, 0);
final Lens parent = slotLens.getParent();
final Map<Key<?>, Object> dataAt = parent == null ? Collections.emptyMap() : parent.getDataFor(slotLens);
// Collect all data for the SlotLens
lenses.computeIfAbsent(sl, k -> new HashMap<>()).putAll(dataAt);
}
}
// remove all single-slot lenses
matches.keySet().removeAll(toRemove.keySet());
for (Map.Entry<SlotLens, Map<Key<?>, Object>> entry : lenses.entrySet()) {
final Map<Key<?>, Object> data = entry.getValue();
if (data.isEmpty()) {
// add back slot-lenses
matches.put(entry.getKey(), toRemove.getOrDefault(entry.getKey(), 0));
} else {
// with data if found
final QueriedSlotLens delegatingSlotLens = new QueriedSlotLens(entry.getKey(), data);
matches.put(delegatingSlotLens, toRemove.getOrDefault(entry.getKey(), 0));
}
}
return matches;
}
Aggregations