Search in sources :

Example 1 with DuctData

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

the class ItemDuctListener method onBreak.

@Listener
public void onBreak(ChangeBlockEvent.Break event) {
    for (Transaction<BlockSnapshot> trans : event.getTransactions()) {
        if (!trans.getOriginal().getLocation().isPresent()) {
            continue;
        }
        BlockType type = trans.getOriginal().getState().getType();
        Location<World> loc = trans.getOriginal().getLocation().get();
        if (isEndPointType(type)) {
            Direction dir = trans.getOriginal().get(Keys.DIRECTION).orElse(Direction.NONE);
            Optional<DuctData> data = loc.getRelative(dir).get(DuctData.class);
            if (data.isPresent()) {
                data.get().remove(dir.getOpposite());
                if (data.get().getFilters().isEmpty()) {
                    loc.getRelative(dir).remove(DuctData.class);
                } else {
                    loc.getRelative(dir).offer(data.get());
                }
            }
        }
    }
}
Also used : BlockType(org.spongepowered.api.block.BlockType) IDuctData(org.cubeengine.module.itemduct.data.IDuctData) DuctData(org.cubeengine.module.itemduct.data.DuctData) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) Listener(org.spongepowered.api.event.Listener)

Example 2 with DuctData

use of org.cubeengine.module.itemduct.data.DuctData 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)

Example 3 with DuctData

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

the class Network method activate.

public void activate(Inventory inventory, List<ItemStack> filters) {
    if (isStorage()) {
        pullFromStorage(inventory, filters);
        return;
    }
    for (Map.Entry<Location<World>, DuctData> entry : exitPoints.entrySet()) {
        Inventory pollFrom = inventory;
        Location<World> loc = entry.getKey();
        DuctData data = entry.getValue();
        Direction dir = loc.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite();
        Location<World> targetLoc = loc.getRelative(dir.getOpposite());
        TileEntity te = targetLoc.getTileEntity().get();
        Inventory target = ((Carrier) te).getInventory();
        if (te instanceof Dropper) {
            Network nw = manager.findNetwork(targetLoc);
            nw.transferToStorage(queryFiltered(filters, inventory), data.get(dir).get());
            continue;
        }
        if (te instanceof Chest) {
            target = ((Chest) te).getDoubleChestInventory().orElse(target);
        }
        Optional<List<ItemStack>> targetFilter = data.get(dir);
        if (targetFilter.isPresent()) {
            if (// Only allow to extract items in the filter
            !filters.isEmpty()) {
                // TODO more filters
                pollFrom = queryFiltered(filters, inventory);
            }
            Inventory pollFromTo = pollFrom;
            if (// Only allow to insert items in the filter
            !targetFilter.get().isEmpty()) {
                // TODO more filters
                pollFromTo = queryFiltered(targetFilter.get(), inventory);
            }
            // For all filtered slots
            doTransfer(pollFromTo, target);
        }
    }
}
Also used : Chest(org.spongepowered.api.block.tileentity.carrier.Chest) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) Dropper(org.spongepowered.api.block.tileentity.carrier.Dropper) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) DuctData(org.cubeengine.module.itemduct.data.DuctData) List(java.util.List) Carrier(org.spongepowered.api.item.inventory.Carrier) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Inventory(org.spongepowered.api.item.inventory.Inventory) Location(org.spongepowered.api.world.Location)

Example 4 with DuctData

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

the class ItemDuctManager method setup.

public void setup(PluginContainer plugin, ItemductConfig config) {
    if (!this.init) {
        DataRegistration.<DuctData, ImmutableDuctData>builder().dataClass(DuctData.class).immutableClass(ImmutableDuctData.class).builder(new DuctDataBuilder()).manipulatorId("duct").dataName("CubeEngine ItemDuct Data").buildAndRegister(plugin);
        DuctData.FILTERS.getQuery();
        Ingredient hopper = Ingredient.of(ItemTypes.HOPPER);
        activatorItem = ItemStack.of(ItemTypes.HOPPER, 1);
        activatorItem.offer(Keys.ITEM_ENCHANTMENTS, singletonList(Enchantment.builder().type(EnchantmentTypes.LOOTING).level(1).build()));
        activatorItem.offer(Keys.DISPLAY_NAME, Text.of(TextColors.GOLD, "ItemDuct Activator"));
        activatorItem.offer(Keys.HIDE_ENCHANTMENTS, true);
        activatorItem.offer(new DuctData(config.activatorUses));
        activatorItem.offer(Keys.ITEM_LORE, Collections.singletonList(Text.of("Uses: ", config.activatorUses)));
        this.recipe = CraftingRecipe.shapedBuilder().rows().row(hopper, hopper, hopper).row(hopper, Ingredient.of(ItemTypes.DIAMOND), hopper).row(hopper, hopper, hopper).result(activatorItem.copy()).build("ItemDuctActivator", plugin);
        Sponge.getRegistry().getCraftingRecipeRegistry().register(this.recipe);
        activatorItem.offer(new DuctData(config.superActivatorUses));
        activatorItem.offer(Keys.ITEM_LORE, Collections.singletonList(Text.of("Uses: Infinite")));
        activatorItem.offer(Keys.DISPLAY_NAME, Text.of(TextColors.GOLD, "ItemDuct Super Activator"));
        this.superRecipe = CraftingRecipe.shapedBuilder().rows().row(hopper, hopper, hopper).row(hopper, Ingredient.of(ItemTypes.NETHER_STAR), hopper).row(hopper, hopper, hopper).result(activatorItem.copy()).build("ItemDuctSuperActivator", plugin);
        Sponge.getRegistry().getCraftingRecipeRegistry().register(this.superRecipe);
    }
    this.reload(config);
    this.init = true;
}
Also used : Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) ImmutableDuctData(org.cubeengine.module.itemduct.data.ImmutableDuctData) IDuctData(org.cubeengine.module.itemduct.data.IDuctData) DuctData(org.cubeengine.module.itemduct.data.DuctData) ImmutableDuctData(org.cubeengine.module.itemduct.data.ImmutableDuctData) DuctDataBuilder(org.cubeengine.module.itemduct.data.DuctDataBuilder)

Example 5 with DuctData

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

the class ItemDuctFilterListener method onInteractPiston.

@Listener
public void onInteractPiston(InteractBlockEvent.Secondary.MainHand event, @Root Player player) {
    event.getTargetBlock().getLocation().ifPresent(loc -> {
        Direction dir = loc.get(Keys.DIRECTION).orElse(Direction.NONE);
        Location<World> te = loc.getRelative(dir);
        Optional<DuctData> ductData = te.get(DuctData.class);
        Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);
        if (ductData.isPresent() && !itemInHand.isPresent() && player.get(Keys.IS_SNEAKING).orElse(false)) {
            openFilter(player, ductData.get(), dir.getOpposite(), te);
        }
    });
}
Also used : DuctData(org.cubeengine.module.itemduct.data.DuctData) World(org.spongepowered.api.world.World) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Direction(org.spongepowered.api.util.Direction) Listener(org.spongepowered.api.event.Listener)

Aggregations

DuctData (org.cubeengine.module.itemduct.data.DuctData)7 Direction (org.spongepowered.api.util.Direction)6 World (org.spongepowered.api.world.World)6 Listener (org.spongepowered.api.event.Listener)5 ItemStack (org.spongepowered.api.item.inventory.ItemStack)4 IDuctData (org.cubeengine.module.itemduct.data.IDuctData)3 BlockType (org.spongepowered.api.block.BlockType)3 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)3 Carrier (org.spongepowered.api.item.inventory.Carrier)3 Location (org.spongepowered.api.world.Location)3 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Inject (javax.inject.Inject)2 Itemduct (org.cubeengine.module.itemduct.Itemduct)2 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)2 STICKY_PISTON (org.spongepowered.api.block.BlockTypes.STICKY_PISTON)2 Chest (org.spongepowered.api.block.tileentity.carrier.Chest)2 Keys (org.spongepowered.api.data.key.Keys)2 HandTypes (org.spongepowered.api.data.type.HandTypes)2