Search in sources :

Example 1 with Network

use of org.cubeengine.module.itemduct.Network in project modules-extra by CubeEngine.

the class ItemDuctTransferListener method activate.

private void activate() {
    List<Network> networks = new ArrayList<>();
    for (Iterator<Location<World>> it = this.promptedActivations.keySet().iterator(); it.hasNext(); ) {
        Location<World> loc = it.next();
        if (this.promptedActivations.get(loc) - 1000 > System.currentTimeMillis()) {
            promptedActivations.clear();
            continue;
        }
        it.remove();
        // Check if data is still present
        Optional<DuctData> data = loc.get(DuctData.class);
        if (data.isPresent()) {
            for (Direction dir : Direction.values()) {
                if (dir.isCardinal() || dir.isUpright()) {
                    BlockType type = loc.getRelative(dir).getBlockType();
                    if (STICKY_PISTON.equals(type) || OBSERVER.equals(type)) {
                        Optional<List<ItemStack>> filters = data.get().get(dir);
                        if (filters.isPresent()) {
                            Network network = manager.findNetwork(loc.getRelative(dir));
                            TileEntity te = loc.getTileEntity().get();
                            Inventory inventory = ((Carrier) te).getInventory();
                            if (te instanceof Chest) {
                                inventory = ((Chest) te).getDoubleChestInventory().orElse(inventory);
                            }
                            network.activate(inventory, filters.get());
                            networks.add(network);
                        }
                    }
                }
            }
        }
    }
    for (Network network : networks) {
        for (Location<World> exitLoc : network.exitPoints.keySet()) {
            Direction exitDir = exitLoc.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite();
            exitLoc = exitLoc.getRelative(exitDir.getOpposite());
            promptActivation(exitLoc.getTileEntity().filter(t -> t instanceof Carrier).map(Carrier.class::cast).orElse(null), true, null);
        }
    }
    if (promptedActivations.isEmpty()) {
        task.cancel();
    }
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) Chest(org.spongepowered.api.block.tileentity.carrier.Chest) Getter(org.spongepowered.api.event.filter.Getter) Keys(org.spongepowered.api.data.key.Keys) HashMap(java.util.HashMap) Itemduct(org.cubeengine.module.itemduct.Itemduct) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Network(org.cubeengine.module.itemduct.Network) OBSERVER(org.spongepowered.api.block.BlockTypes.OBSERVER) ItemStack(org.spongepowered.api.item.inventory.ItemStack) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) Map(java.util.Map) Task(org.spongepowered.api.scheduler.Task) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ItemDuctManager(org.cubeengine.module.itemduct.ItemDuctManager) Location(org.spongepowered.api.world.Location) Iterator(java.util.Iterator) STICKY_PISTON(org.spongepowered.api.block.BlockTypes.STICKY_PISTON) Sponge(org.spongepowered.api.Sponge) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) Carrier(org.spongepowered.api.item.inventory.Carrier) Root(org.spongepowered.api.event.filter.cause.Root) Direction(org.spongepowered.api.util.Direction) List(java.util.List) Container(org.spongepowered.api.item.inventory.Container) MultiBlockCarrier(org.spongepowered.api.item.inventory.MultiBlockCarrier) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) DuctData(org.cubeengine.module.itemduct.data.DuctData) Chest(org.spongepowered.api.block.tileentity.carrier.Chest) ArrayList(java.util.ArrayList) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) BlockType(org.spongepowered.api.block.BlockType) Network(org.cubeengine.module.itemduct.Network) DuctData(org.cubeengine.module.itemduct.data.DuctData) ArrayList(java.util.ArrayList) List(java.util.List) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) Carrier(org.spongepowered.api.item.inventory.Carrier) MultiBlockCarrier(org.spongepowered.api.item.inventory.MultiBlockCarrier) Inventory(org.spongepowered.api.item.inventory.Inventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Location(org.spongepowered.api.world.Location)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1 ItemDuctManager (org.cubeengine.module.itemduct.ItemDuctManager)1 Itemduct (org.cubeengine.module.itemduct.Itemduct)1 Network (org.cubeengine.module.itemduct.Network)1 DuctData (org.cubeengine.module.itemduct.data.DuctData)1 Sponge (org.spongepowered.api.Sponge)1 BlockType (org.spongepowered.api.block.BlockType)1 OBSERVER (org.spongepowered.api.block.BlockTypes.OBSERVER)1 STICKY_PISTON (org.spongepowered.api.block.BlockTypes.STICKY_PISTON)1 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)1 Chest (org.spongepowered.api.block.tileentity.carrier.Chest)1 Keys (org.spongepowered.api.data.key.Keys)1 HandTypes (org.spongepowered.api.data.type.HandTypes)1 Player (org.spongepowered.api.entity.living.player.Player)1