Search in sources :

Example 11 with Carrier

use of org.spongepowered.api.item.inventory.Carrier in project modules-extra by CubeEngine.

the class ItemDuctManager method findNetwork.

private void findNetwork(LastDuct last, Network network, int depth) {
    if (depth > maxDepth) {
        network.errors.add(last.loc);
        return;
    }
    Map<Direction, Location<World>> map = new HashMap<>();
    Queue<Location<World>> next = new LinkedList<>();
    next.offer(last.loc);
    do {
        // TODO limit direction with glass panes
        Set<Direction> dirs = directions;
        for (Direction dir : dirs) {
            if (!dir.equals(last.from.getOpposite())) {
                Location<World> rel = last.loc.getRelative(dir);
                if (last.isCompatible(rel)) {
                    if (// No loops allowed
                    network.pipes.contains(rel)) {
                        network.errors.add(rel);
                        network.errors.add(last.loc);
                    } else {
                        network.pipes.add(rel);
                        map.put(dir, rel);
                    }
                }
                // ExitPiston?
                if (rel.getBlockType().equals(PISTON) && rel.get(Keys.DIRECTION).orElse(Direction.NONE).equals(dir)) {
                    if (rel.getRelative(dir).get(DuctData.class).map(d -> d.get(dir.getOpposite()).isPresent()).orElse(false)) {
                        network.exitPoints.put(rel, rel.getRelative(dir).get(DuctData.class).get());
                    }
                }
                // Storage Chest
                if (last.storage) {
                    rel.getTileEntity().ifPresent(te -> {
                        if (te instanceof Carrier) {
                            network.storage.add(rel);
                        }
                    });
                }
            }
        }
        if (map.size() > 1) {
            for (Map.Entry<Direction, Location<World>> entry : map.entrySet()) {
                findNetwork(new LastDuct(entry.getValue(), entry.getKey()), network, depth + 1);
            }
        } else if (map.size() == 1) {
            for (Map.Entry<Direction, Location<World>> entry : map.entrySet()) {
                last.update(entry.getValue(), entry.getKey());
            }
            next.offer(last.loc);
        }
        // else nothing found here
        next.poll();
        map.clear();
    } while (!next.isEmpty());
}
Also used : Arrays(java.util.Arrays) Keys(org.spongepowered.api.data.key.Keys) ParticleOptions(org.spongepowered.api.effect.particle.ParticleOptions) PISTON(org.spongepowered.api.block.BlockTypes.PISTON) DataRegistration(org.spongepowered.api.data.DataRegistration) ItemTypes(org.spongepowered.api.item.ItemTypes) QUARTZ_BLOCK(org.spongepowered.api.block.BlockTypes.QUARTZ_BLOCK) Vector3d(com.flowpowered.math.vector.Vector3d) HashMap(java.util.HashMap) STAINED_GLASS(org.spongepowered.api.block.BlockTypes.STAINED_GLASS) EnchantmentTypes(org.spongepowered.api.item.enchantment.EnchantmentTypes) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) Collections.singletonList(java.util.Collections.singletonList) ImmutableDuctData(org.cubeengine.module.itemduct.data.ImmutableDuctData) HashSet(java.util.HashSet) ShapedCraftingRecipe(org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe) ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) ItemStack(org.spongepowered.api.item.inventory.ItemStack) IDuctData(org.cubeengine.module.itemduct.data.IDuctData) Text(org.spongepowered.api.text.Text) ParticleTypes(org.spongepowered.api.effect.particle.ParticleTypes) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) SoundTypes(org.spongepowered.api.effect.sound.SoundTypes) Map(java.util.Map) DROPPER(org.spongepowered.api.block.BlockTypes.DROPPER) LinkedList(java.util.LinkedList) PluginContainer(org.spongepowered.api.plugin.PluginContainer) TextColors(org.spongepowered.api.text.format.TextColors) EnumSet(java.util.EnumSet) GLASS_PANE(org.spongepowered.api.block.BlockTypes.GLASS_PANE) Location(org.spongepowered.api.world.Location) DyeColor(org.spongepowered.api.data.type.DyeColor) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) DuctDataBuilder(org.cubeengine.module.itemduct.data.DuctDataBuilder) STAINED_GLASS_PANE(org.spongepowered.api.block.BlockTypes.STAINED_GLASS_PANE) Carrier(org.spongepowered.api.item.inventory.Carrier) Direction(org.spongepowered.api.util.Direction) GLASS(org.spongepowered.api.block.BlockTypes.GLASS) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) Color(org.spongepowered.api.util.Color) Queue(java.util.Queue) Collections(java.util.Collections) DuctData(org.cubeengine.module.itemduct.data.DuctData) HashMap(java.util.HashMap) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) LinkedList(java.util.LinkedList) Carrier(org.spongepowered.api.item.inventory.Carrier) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.spongepowered.api.world.Location)

Example 12 with Carrier

use of org.spongepowered.api.item.inventory.Carrier in project modules-extra by CubeEngine.

the class Network method pullFromStorage.

private void pullFromStorage(Inventory inventory, List<ItemStack> filters) {
    for (Location<World> targetLoc : storage) {
        TileEntity te = targetLoc.getTileEntity().get();
        Inventory pollFrom = ((Carrier) te).getInventory();
        if (te instanceof Chest) {
            pollFrom = ((Chest) te).getDoubleChestInventory().orElse(pollFrom);
        }
        if (// Only allow to extract items in the filter
        !filters.isEmpty()) {
            // TODO more filters
            pollFrom = queryFiltered(filters, inventory);
        }
        doTransfer(pollFrom, inventory);
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Chest(org.spongepowered.api.block.tileentity.carrier.Chest) Carrier(org.spongepowered.api.item.inventory.Carrier) World(org.spongepowered.api.world.World) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 13 with Carrier

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

the class TestInventoryPlugin method onInit.

@Listener
public void onInit(GameInitializationEvent event) {
    this.myArchetype = InventoryArchetype.builder().with(InventoryArchetypes.MENU_ROW).title(Text.of("My Fancy Title")).property(InventoryDimension.of(9, 1)).property(new GuiIdProperty(GuiIds.CHEST)).build("inventory_test:test", "Test Inventory");
    Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(t("Only players may use this command."));
        }
        final Inventory inventory = Inventory.builder().of(this.myArchetype).withCarrier((Carrier) src).build(this);
        System.out.println(inventory.getClass().getName());
        final ItemStack itemStack = ItemStack.of(ItemTypes.LOG, 64);
        itemStack.offer(Keys.TREE_TYPE, TreeTypes.JUNGLE);
        inventory.offer(itemStack);
        ((Player) src).openInventory(inventory);
        return CommandResult.success();
    }).build(), "test-a-inv");
    Keys.COAL_TYPE.registerEvent(ItemStack.class, event1 -> {
        final DataTransactionResult result = event1.getEndResult();
        final List<ImmutableValue<?>> newSuccessfulData = new ArrayList<>(result.getSuccessfulData());
        Iterator<ImmutableValue<?>> it = newSuccessfulData.iterator();
        while (it.hasNext()) {
            final ImmutableValue<?> value = it.next();
            if (value.getKey() == Keys.COAL_TYPE) {
                System.out.println("Changed coal type to: " + ((CoalType) value.get()).getId() + ", but this not allowed");
                it.remove();
                break;
            }
        }
        final List<ImmutableValue<?>> newReplacedData = new ArrayList<>(result.getReplacedData());
        it = newSuccessfulData.iterator();
        while (it.hasNext()) {
            final ImmutableValue<?> value = it.next();
            if (value.getKey() == Keys.COAL_TYPE) {
                it.remove();
                break;
            }
        }
        event1.proposeChanges(DataTransactionResult.builder().result(result.getType()).reject(result.getRejectedData()).replace(newReplacedData).success(newSuccessfulData).build());
    });
}
Also used : GuiIdProperty(org.spongepowered.api.item.inventory.property.GuiIdProperty) Player(org.spongepowered.api.entity.living.player.Player) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) ArrayList(java.util.ArrayList) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) CommandException(org.spongepowered.api.command.CommandException) Carrier(org.spongepowered.api.item.inventory.Carrier) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) Listener(org.spongepowered.api.event.Listener)

Example 14 with Carrier

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

the class LanternItem method tryToPickupItems.

private void tryToPickupItems() {
    final Set<Entity> entities = getWorld().getIntersectingEntities(getBoundingBox().get().expand(2.0, 0.5, 2.0), entity -> entity != this && entity instanceof Carrier);
    if (entities.isEmpty()) {
        return;
    }
    ItemStack itemStack = get(Keys.REPRESENTED_ITEM).map(ItemStackSnapshot::createStack).orElse(null);
    if (itemStack == null) {
        remove();
        return;
    }
    // TODO: Call pre pickup event
    for (Entity entity : entities) {
        // Ignore dead entities
        if (entity instanceof LanternLiving && ((LanternLiving) entity).isDead()) {
            continue;
        }
        Inventory inventory = ((Carrier) entity).getInventory();
        if (inventory instanceof PlayerInventory) {
            inventory = ((PlayerInventory) inventory).getMain();
        }
        final PeekedOfferTransactionResult peekResult = ((IInventory) inventory).peekOffer(itemStack);
        final ItemStack rejected = peekResult.getRejectedItem().orElse(null);
        final CauseStack causeStack = CauseStack.current();
        final ChangeInventoryEvent.Pickup event;
        try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
            frame.addContext(LanternEventContextKeys.ORIGINAL_ITEM_STACK, itemStack);
            if (rejected != null) {
                frame.addContext(LanternEventContextKeys.REST_ITEM_STACK, rejected);
            }
            event = SpongeEventFactory.createChangeInventoryEventPickup(causeStack.getCurrentCause(), inventory, peekResult.getTransactions());
            event.setCancelled(!peekResult.isSuccess());
            Sponge.getEventManager().post(event);
        }
        if (event.isCancelled() && !isRemoved()) {
            // Don't continue if the entity was removed during the event
            continue;
        }
        event.getTransactions().stream().filter(Transaction::isValid).forEach(transaction -> transaction.getSlot().set(transaction.getFinal().createStack()));
        final int added;
        if (rejected != null) {
            added = itemStack.getQuantity() - rejected.getQuantity();
            itemStack = rejected;
        } else {
            added = itemStack.getQuantity();
        }
        if (added != 0 && entity instanceof Living) {
            triggerEvent(new CollectEntityEvent((Living) entity, added));
        }
        if (rejected == null || isRemoved()) {
            itemStack = null;
        }
        if (itemStack == null) {
            break;
        }
    }
    if (itemStack != null) {
        offer(Keys.REPRESENTED_ITEM, itemStack.createSnapshot());
    } else {
        remove();
    }
}
Also used : IInventory(org.lanternpowered.server.inventory.IInventory) Entity(org.spongepowered.api.entity.Entity) CauseStack(org.lanternpowered.server.event.CauseStack) Living(org.spongepowered.api.entity.living.Living) PeekedOfferTransactionResult(org.lanternpowered.server.inventory.PeekedOfferTransactionResult) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) CollectEntityEvent(org.lanternpowered.server.entity.event.CollectEntityEvent) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) Carrier(org.spongepowered.api.item.inventory.Carrier) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) IInventory(org.lanternpowered.server.inventory.IInventory) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory)

Example 15 with Carrier

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

the class LanternLiving method handleDeath.

private void handleDeath() {
    // have already been thrown.
    if (getWorld() == null) {
        setDead(true);
    }
    if (isDead()) {
        return;
    }
    setDead(true);
    final CauseStack causeStack = CauseStack.current();
    // Only players can keep their inventory
    final boolean keepsInventory = this instanceof LanternPlayer && getWorld().getOrCreateRule(RuleTypes.KEEP_INVENTORY).getValue();
    // Post the entity destruction event
    final DestructEntityEvent.Death event = SpongeEventFactory.createDestructEntityEventDeath(causeStack.getCurrentCause(), MessageChannel.TO_NONE, Optional.empty(), new MessageEvent.MessageFormatter(), this, keepsInventory, false);
    postDestructEvent(event);
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        // Add the destruct event to the cause, this can be used
        // to track the cause of the entity death.
        frame.pushCause(event);
        // Post the harvest event
        handleDeath(causeStack);
    }
    // Clear the inventory, if keepsInventory is false in the thrown Death event
    if (!event.getKeepInventory() && this instanceof Carrier) {
        ((Carrier) this).getInventory().clear();
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) DestructEntityEvent(org.spongepowered.api.event.entity.DestructEntityEvent) MessageEvent(org.spongepowered.api.event.message.MessageEvent) Carrier(org.spongepowered.api.item.inventory.Carrier) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

Carrier (org.spongepowered.api.item.inventory.Carrier)17 Inventory (org.spongepowered.api.item.inventory.Inventory)10 World (org.spongepowered.api.world.World)8 ItemStack (org.spongepowered.api.item.inventory.ItemStack)7 Listener (org.spongepowered.api.event.Listener)5 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)4 Map (java.util.Map)3 DuctData (org.cubeengine.module.itemduct.data.DuctData)3 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)3 Chest (org.spongepowered.api.block.tileentity.carrier.Chest)3 Direction (org.spongepowered.api.util.Direction)3 Vector3d (com.flowpowered.math.vector.Vector3d)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CauseStack (org.lanternpowered.server.event.CauseStack)2 BlockType (org.spongepowered.api.block.BlockType)2 Entity (org.spongepowered.api.entity.Entity)2 Player (org.spongepowered.api.entity.living.player.Player)2 Container (org.spongepowered.api.item.inventory.Container)2