Search in sources :

Example 1 with InteractInventoryEvent

use of org.spongepowered.api.event.item.inventory.InteractInventoryEvent in project LanternServer by LanternPowered.

the class LanternInventoryBuilder method build.

@Override
public T build(Object plugin) {
    checkState(this.inventoryArchetype != null, "The inventory archetype must be set");
    AbstractBuilder builder = this.builder;
    LanternInventoryArchetype<T> inventoryArchetype = this.inventoryArchetype;
    if (builder == null) {
        builder = this.inventoryArchetype.getBuilder();
    }
    if (this.forCarrierType != null || this.forCarrier != null || this.carrier != null) {
        AbstractArchetypeBuilder builder1 = (AbstractArchetypeBuilder) builder;
        if (builder1.carrierTransformer != null) {
            final Supplier supplier = () -> {
                final AbstractArchetypeBuilder copy = builder1.copy();
                copy.carrierTransformer = null;
                return copy;
            };
            final LanternInventoryArchetype archetype;
            if (this.forCarrier != null) {
                archetype = builder1.carrierTransformer.apply(this.forCarrier, supplier);
            } else if (this.forCarrierType != null) {
                archetype = builder1.carrierTransformer.apply(this.forCarrierType, supplier);
            } else {
                // Fall back to actual carrier
                archetype = builder1.carrierTransformer.apply(this.carrier, supplier);
            }
            if (archetype != null) {
                builder = archetype.getBuilder();
            }
        }
    }
    final AbstractInventory inventory = builder.build(this.carrier != null, plugin, inventoryArchetype);
    if (inventory instanceof AbstractMutableInventory && this.carrier != null) {
        final AbstractMutableInventory mutableInventory = (AbstractMutableInventory) inventory;
        mutableInventory.setCarrier0(this.carrier);
    }
    if (this.carrier instanceof AbstractCarrier) {
        ((AbstractCarrier) this.carrier).setInventory((CarriedInventory<?>) inventory);
    }
    if (!this.listeners.isEmpty()) {
        final List<InventoryListeners> listenersList = new ArrayList<>();
        for (Map.Entry<Class<? extends InteractInventoryEvent>, List<Consumer<? extends InteractInventoryEvent>>> entry : this.listeners.entrySet()) {
            final InventoryListeners listeners = new InventoryListeners(inventory, entry.getKey(), entry.getValue());
            Sponge.getEventManager().registerListener(plugin, entry.getKey(), listeners);
        }
        inventoryListeners.put(inventory, listenersList);
    }
    return (T) inventory;
}
Also used : InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) ArrayList(java.util.ArrayList) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Supplier (java.util.function.Supplier)1 InteractInventoryEvent (org.spongepowered.api.event.item.inventory.InteractInventoryEvent)1