Search in sources :

Example 1 with ContainerEntity

use of net.glowstone.block.entity.ContainerEntity in project Glowstone by GlowstoneMC.

the class BlockHopper method pullItems.

private void pullItems(GlowBlock block, HopperEntity hopper) {
    GlowBlock source = block.getRelative(BlockFace.UP);
    MaterialData data = source.getState().getData();
    if (!source.getType().isSolid() || (data instanceof Step && !((Step) data).isInverted()) || (data instanceof WoodenStep && !((WoodenStep) data).isInverted()) || (data instanceof Sign) || (data instanceof Rails)) {
        GlowItem item = getFirstDroppedItem(source.getLocation());
        if (item == null) {
            return;
        }
        ItemStack stack = item.getItemStack();
        HashMap<Integer, ItemStack> add = hopper.getInventory().addItem(stack);
        if (add.size() > 0) {
            item.setItemStack(add.get(0));
        } else {
            item.remove();
        }
    } else if (source.getBlockEntity() != null && source.getBlockEntity() instanceof ContainerEntity) {
        ContainerEntity sourceContainer = (ContainerEntity) source.getBlockEntity();
        if (sourceContainer.getInventory() == null || sourceContainer.getInventory().getContents().length == 0) {
            return;
        }
        ItemStack item = getFirstItem(sourceContainer);
        if (item == null) {
            return;
        }
        ItemStack clone = item.clone();
        clone.setAmount(1);
        if (hopper.getInventory().addItem(clone).size() > 0) {
            return;
        }
        if (item.getAmount() - 1 == 0) {
            sourceContainer.getInventory().remove(item);
        } else {
            item.setAmount(item.getAmount() - 1);
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) ContainerEntity(net.glowstone.block.entity.ContainerEntity) ItemStack(org.bukkit.inventory.ItemStack) GlowItem(net.glowstone.entity.objects.GlowItem)

Example 2 with ContainerEntity

use of net.glowstone.block.entity.ContainerEntity in project Glowstone by GlowstoneMC.

the class BlockHopper method pushItems.

private boolean pushItems(GlowBlock block, HopperEntity hopper) {
    if (hopper.getInventory() == null || hopper.getInventory().getContents().length == 0) {
        return false;
    }
    GlowBlock target = block.getRelative(((Hopper) block.getState().getData()).getFacing());
    if (target.getType() != null && target.getBlockEntity() instanceof ContainerEntity) {
        if (target.getState() instanceof GlowHopper) {
            if (((Hopper) block.getState().getData()).getFacing() == BlockFace.DOWN) {
                // If the hopper is facing downwards, the target hopper can do the pulling task itself
                return false;
            }
        }
        ItemStack item = getFirstItem(hopper);
        if (item == null) {
            return false;
        }
        ItemStack clone = item.clone();
        clone.setAmount(1);
        if (((ContainerEntity) target.getBlockEntity()).getInventory().addItem(clone).size() > 0) {
            return false;
        }
        if (item.getAmount() - 1 == 0) {
            hopper.getInventory().remove(item);
        } else {
            item.setAmount(item.getAmount() - 1);
        }
        return true;
    }
    return false;
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) GlowHopper(net.glowstone.block.state.GlowHopper) ContainerEntity(net.glowstone.block.entity.ContainerEntity) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

GlowBlock (net.glowstone.block.GlowBlock)2 ContainerEntity (net.glowstone.block.entity.ContainerEntity)2 ItemStack (org.bukkit.inventory.ItemStack)2 GlowHopper (net.glowstone.block.state.GlowHopper)1 GlowItem (net.glowstone.entity.objects.GlowItem)1