
function ccSearchContainer( ply, cmd, args )

	if( args[1] == nil ) then
		return;
	end
	
	local entity = ents.GetByIndex( tonumber( args[ 1 ] ) );
	
	if( !entity:IsValid() ) then
		return;
	end
	
	local min, max = entity:WorldSpaceAABB();
	local offset = max - min;
	
	-- a good size is 20 in each dimension minimum.
	
	umsg.Start( "clearsearchgui", ply )
	umsg.End( );
	
	if( offset.x < 20 or offset.y < 20 or offset.z < 20 ) then
		CAKE.Notify( ply, "You find nothing." );
		return;
	else
		
		if( entity.inventory == nil ) then
			entity.inventory = {};
		end
	
		for k,v in pairs( entity.inventory ) do

			local ItemTable = ItemData[ v ];

			umsg.Start( "addsearchitem", ply )
			umsg.String( ItemTable.Model )
			umsg.Bool( true )
			umsg.String( ItemTable.Class )
			umsg.Entity( entity )
			umsg.String( ItemTable.Name )
			umsg.String( ItemTable.Description )
			umsg.End( );

		end
	
		umsg.Start( "startsearch", ply )
		umsg.String( "Items" )
		umsg.Entity( entity )
		umsg.End( );
	
	end
	
end
concommand.Add( "rp_search", ccSearchContainer );

function ccContainerTakeItem( ply, cmd, args )

	if( args[1] == nil || args[2] == nil ) then
		return;
	end

    local entity = ents.GetByIndex( tonumber( args[ 1 ] ) );
	local argitem = args[ 2 ];
	local entinv = { }
	local hasitem = false;
	
	if( entity:GetPos():Distance( ply:GetPos() ) <= 100 ) then

		local itemtable = ItemData[ argitem ];
	
		for k,v in pairs( entity.inventory ) do
	
			if( v == argitem ) then
				argitemz = k
				hasitem = true;
			end
	
		end
	
		if( hasitem ) then
			ply:GiveItem( itemtable.Class );
			
			table.remove( entity.inventory, argitemz );
		end
	
		umsg.Start( "clearsearchgui", ply );
		umsg.End( );
	
		ply:ClearInventory();

		for k,v in pairs( entity.inventory ) do

			local ItemTable = ItemData[ v ];

			umsg.Start( "addsearchitem", ply )
			umsg.String( ItemTable.Model )
			umsg.Bool( ItemTable.Takeable )
			umsg.String( ItemTable.Class )
			umsg.Entity( entity )
			umsg.End( );

		end
	
		ply:RefreshInventory();
		
		ply:ConCommand("rp_search " .. entity:EntIndex());
		
	end
end
concommand.Add( "container_takeitem", ccContainerTakeItem )

function ccContainerPutItem( ply, cmd, args )

	if( args[1] == nil || args[2] == nil ) then
		return;
	end

	local entity = ents.GetByIndex( tonumber( args[ 1 ] ) );
	local item = args[ 2 ];
	local count = 0;

	for k,v in pairs( entity.inventory ) do
		count = count + 1;
	end

	local min, max = entity:WorldSpaceAABB();
	local offset = max - min;
	
	local maxItems = math.Round( (offset.x / 20)*(offset.y / 20)*(offset.z / 20) ) * 2;

	if( count >= maxItems ) then
		CAKE.Notify( ply, "That container is full!");
		return false;
	end

	local hadItem = ply:TakeItem( item );
	
	if( hadItem == false ) then
		return;
	end
	
	table.insert( entity.inventory, item )

	umsg.Start( "clearsearchgui", ply ) umsg.End( );
	ply:ClearInventory();

	for k,v in pairs( entity.inventory ) do

		local ItemTable = ItemData[ v ];

		umsg.Start( "addsearchitem", ply )
		umsg.String( ItemTable.Model )
		umsg.Bool( ItemTable.Takeable )
		umsg.String( ItemTable.Class )
		umsg.Entity( entity )
		umsg.End( );

    end
	
	ply:RefreshInventory();
	
	ply:ConCommand("rp_search " .. entity:EntIndex());

end
concommand.Add( "container_putitem", ccContainerPutItem )