Search in sources :

Example 1 with CraftingManagerSpindle

use of betterwithaddons.crafting.manager.CraftingManagerSpindle in project BetterWithAddons by DaedalusGame.

the class BlockSpindle method spinUp.

@Override
public void spinUp(World world, BlockPos pos, IBlockState state, EnumFacing dir) {
    List<EntityItem> list = world.getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(pos, pos.add(1, 1, 1)), EntitySelectors.IS_ALIVE);
    EnumFacing.Axis axis = state.getValue(AXIS);
    if (list.size() > 0) {
        CraftingManagerSpindle manager = CraftingManagerSpindle.getInstance();
        SpindleRecipe recipe = manager.getMostValidRecipe(list);
        if (recipe != null) {
            ItemUtil.consumeItem(list, recipe.input);
            List<ItemStack> ret = recipe.getOutput();
            if (ret != null && ret.size() > 0) {
                List<EnumFacing> validDirections = new ArrayList<>();
                for (EnumFacing facing : EnumFacing.VALUES) {
                    if (facing.getAxis() == axis || facing == EnumFacing.UP)
                        continue;
                    IBlockState check = world.getBlockState(pos.offset(facing));
                    if (check.getBlock().isReplaceable(world, pos.offset(facing)) || world.isAirBlock(pos.offset(facing)))
                        validDirections.add(facing);
                }
                for (int i = 0; i < ret.size(); i++) {
                    ItemStack item = ret.get(i);
                    if (item.isEmpty())
                        continue;
                    EnumFacing ejectdir;
                    if (validDirections.isEmpty())
                        ejectdir = axis.isVertical() ? EnumFacing.HORIZONTALS[world.rand.nextInt(4)] : EnumFacing.DOWN;
                    else
                        ejectdir = validDirections.get(world.rand.nextInt(validDirections.size()));
                    EntityItem result = new EntityItem(world, pos.getX() + 0.5f + ejectdir.getFrontOffsetX() * 0.2f, pos.getY() + 0.1f, pos.getZ() + 0.5f + ejectdir.getFrontOffsetZ() * 0.2f, item.copy());
                    result.motionX = ejectdir.getFrontOffsetX() * 0.2;
                    result.motionY = 0;
                    result.motionZ = ejectdir.getFrontOffsetZ() * 0.2;
                    result.setDefaultPickupDelay();
                    if (!world.isRemote) {
                        world.spawnEntity(result);
                    }
                }
                if (recipe.consumesSpindle) {
                    world.setBlockToAir(pos);
                }
            }
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) SpindleRecipe(betterwithaddons.crafting.recipes.SpindleRecipe) IBlockState(net.minecraft.block.state.IBlockState) EnumFacing(net.minecraft.util.EnumFacing) CraftingManagerSpindle(betterwithaddons.crafting.manager.CraftingManagerSpindle) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

CraftingManagerSpindle (betterwithaddons.crafting.manager.CraftingManagerSpindle)1 SpindleRecipe (betterwithaddons.crafting.recipes.SpindleRecipe)1 ArrayList (java.util.ArrayList)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityItem (net.minecraft.entity.item.EntityItem)1 ItemStack (net.minecraft.item.ItemStack)1 EnumFacing (net.minecraft.util.EnumFacing)1 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)1