public static bool TryGetContainerDefinition(MyObjectBuilderType type, MyStringHash subtypeName, out MyContainerDefinition definition)
        {
            definition = null;

            if (MyDefinitionManager.Static != null)
            {                
                MyDefinitionId containerSubtypeId = new MyDefinitionId(type, subtypeName);
                if (MyDefinitionManager.Static.TryGetContainerDefinition(containerSubtypeId, out definition))
                    return true;

                // Fallback to EntityBase type
                if (subtypeName != MyStringHash.NullOrEmpty)
                {
                    MyDefinitionId containerSubtypeId_Fallback = new MyDefinitionId(typeof(MyObjectBuilder_EntityBase), subtypeName);
                    if (MyDefinitionManager.Static.TryGetContainerDefinition(containerSubtypeId_Fallback, out definition))
                        return true;
                }

                MyDefinitionId containerDefaultId = new MyDefinitionId(type);
                if (MyDefinitionManager.Static.TryGetContainerDefinition(containerDefaultId, out definition))
                    return true;

            }

            return false;
        }
        public static MyObjectBuilder_ComponentBase FindComponentBuilder(MyContainerDefinition.DefaultComponent component, MyObjectBuilder_ComponentContainer builder)
        {
            MyObjectBuilder_ComponentBase componentBuilder = null;

            if (builder != null && component.IsValid())
            {
                MyObjectBuilderType componentObType = null;
                
                if (!component.BuilderType.IsNull)
                {   
                    var componentData = builder.Components.Find(x => x.Component.TypeId == component.BuilderType);

                    if (componentData != null)
                    {
                        componentBuilder = componentData.Component;
                    }
                }
            }

            return componentBuilder;
        }
        void confirmButton_OnButtonClick(MyGuiControlButton sender)
        {
            MyContainerDefinition newDefinition = new MyContainerDefinition();

            foreach (var addComp in m_containersListBox.SelectedItems)
            {
                if (addComp.UserData is MyDefinitionId)
                {
                    var defId = (MyDefinitionId)addComp.UserData;

                    MyEntity entity = MyEntities.CreateEntityAndAdd(defId, true, m_position);
                    Debug.Assert(entity != null, "Entity wasn't created!");
                }
            }

            CloseScreen();
        }
        /// <summary>
        /// Spawns bag around position given by "baseTransform", checks all 4 directions around - forwards (forward, right, backward, left) and on each such direction moves test sphere 
        /// in 3 directions forward (frontChecks), sides (perpendicular to forward direction - rights) and up. If spawn position is not found then position above "worldAabbTopPosition"
        /// is selected.
        /// </summary>
        private static MyEntity SpawnBagAround(MyEntity itemOwner, MyContainerDefinition bagDefinition,
            int sideCheckCount = 3, int frontCheckCount = 2, int upCheckCount = 5, float stepSize = 1f)
        {
            Debug.Assert(Sandbox.Game.Multiplayer.Sync.IsServer);

            Vector3D? finalPos = null;

            // Model sphere
            MyModel bagModel = null;
            foreach (var componentDef in bagDefinition.DefaultComponents)
            {
                if (typeof(MyObjectBuilder_ModelComponent).IsAssignableFrom(componentDef.BuilderType))
                {
                    MyComponentDefinitionBase componentDefinition = null;
                    var componentSubtype = bagDefinition.Id.SubtypeId;
                    if (componentDef.SubtypeId.HasValue)
                        componentSubtype = componentDef.SubtypeId.Value;

                    if (MyComponentContainerExtension.TryGetComponentDefinition(componentDef.BuilderType, componentSubtype, out componentDefinition))
                    {
                        var modelComponentDef = componentDefinition as MyModelComponentDefinition;
                        Debug.Assert(modelComponentDef != null);
                        if (modelComponentDef != null)
                            bagModel = MyModels.GetModelOnlyData(modelComponentDef.Model);
                    }

                    break;
                }
            }

            Debug.Assert(bagModel != null);
            if (bagModel == null)
                return null;

            float bagBoxRadius = bagModel.BoundingBox.HalfExtents.Max();
            HkShape sphere = new HkSphereShape(bagBoxRadius);

            try
            {
                Vector3D basePos = itemOwner.PositionComp.WorldMatrix.Translation;
                float step = bagBoxRadius * stepSize;

                // Calculate right, up and forward vectors from gravity
                Vector3 upDir = -MyGravityProviderSystem.CalculateNaturalGravityInPoint(itemOwner.PositionComp.WorldMatrix.Translation);
                if (upDir == Vector3.Zero)
                    upDir = Vector3.Up;
                else
                    upDir.Normalize();

                Vector3 forwardDir;
                upDir.CalculatePerpendicularVector(out forwardDir);

                Vector3 rightDir = Vector3.Cross(forwardDir, upDir);
                rightDir.Normalize();

                Vector3D currentPos;
                Quaternion rot = Quaternion.Identity;

                Vector3[] forwards = new Vector3[] 
                {
                    forwardDir,
                    rightDir,
                    -forwardDir,
                    -rightDir
                };

                Vector3[] rights = new Vector3[] 
                {
                    rightDir,
                    -forwardDir,
                    -rightDir,
                    forwardDir
                };

                // All sides
                for (int i = 0; i < forwards.Length && finalPos == null; ++i)
                {
                    var forward = forwards[i];
                    var right = rights[i];

                    // Move forward
                    for (int frontMove = 0; frontMove < frontCheckCount && finalPos == null; ++frontMove)
                    {
                        Vector3D sidePosBase = basePos + 0.25f * forward + bagBoxRadius * forward + frontMove * step * forward - 0.5f * (sideCheckCount - 1) * step * right;

                        // Move perp to forward
                        for (int sideMove = 0; sideMove < sideCheckCount && finalPos == null; ++sideMove)
                        {
                            // Move up
                            for (int upMove = 0; upMove < upCheckCount && finalPos == null; ++upMove)
                            {
                                currentPos = sidePosBase + sideMove * step * right + upMove * step * upDir;

                                if (MyEntities.IsInsideWorld(currentPos) && !MyEntities.IsShapePenetrating(sphere, ref currentPos, ref rot))
                                {
                                    BoundingSphereD boundingSphere = new BoundingSphereD(currentPos, bagBoxRadius);
                                    MyVoxelBase overlappedVoxelmap = MySession.Static.VoxelMaps.GetOverlappingWithSphere(ref boundingSphere);

                                    if (overlappedVoxelmap == null)
                                    {
                                        finalPos = currentPos;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                // If not found position then select position above aabb's top.
                if (finalPos == null)
                {
                    MyOrientedBoundingBoxD obb = new MyOrientedBoundingBoxD((BoundingBoxD)itemOwner.PositionComp.LocalAABB, itemOwner.PositionComp.WorldMatrix);
                    Vector3D[] corners = new Vector3D[8];
                    obb.GetCorners(corners, 0);
                    float dotUp = float.MinValue;
                    foreach (var corner in corners)
                    {
                        var localDot = Vector3.Dot(corner - obb.Center, upDir);
                        dotUp = Math.Max(dotUp, localDot);
                    }

                    finalPos = itemOwner.PositionComp.WorldMatrix.Translation;
                    Debug.Assert(dotUp > 0);
                    if (dotUp > 0)
                        finalPos = obb.Center + dotUp * upDir;
                }
            }
            finally
            {
                sphere.RemoveReference();
            }

            Debug.Assert(finalPos != null);

            MatrixD transform = itemOwner.PositionComp.WorldMatrix;
            transform.Translation = finalPos.Value;

            MyEntity bagEntity = MyEntities.CreateFromComponentContainerDefinitionAndAdd(bagDefinition.Id);
            if (bagEntity == null)
                return null;

            bagEntity.PositionComp.SetWorldMatrix(transform);

            bagEntity.Physics.LinearVelocity = Vector3.Zero;
            bagEntity.Physics.AngularVelocity = Vector3.Zero;

            return bagEntity;
        }
        void confirmButton_OnButtonClick(MyGuiControlButton sender)
        {
            MyContainerDefinition newDefinition = new MyContainerDefinition();

            foreach (var addComp in m_addComponentsListBox.SelectedItems)
            {                
                if (addComp.UserData is MyDefinitionId)
                {
                    var defId = (MyDefinitionId)addComp.UserData;
                    
                    MyContainerDefinition.DefaultComponent component = new MyContainerDefinition.DefaultComponent();
                    component.BuilderType = defId.TypeId;
                    component.SubtypeId = defId.SubtypeId;

                    newDefinition.DefaultComponents.Add(component);
                }
            }
            
            MyObjectBuilder_EntityBase entityOb = null;
            if (m_replicableEntityCheckBox.IsChecked)
            {
                entityOb = new MyObjectBuilder_ReplicableEntity();
                newDefinition.Id = new MyDefinitionId(typeof(MyObjectBuilder_ReplicableEntity), "DebugTest");
            }
            else
            {
                entityOb = new MyObjectBuilder_EntityBase();
                newDefinition.Id = new MyDefinitionId(typeof(MyObjectBuilder_EntityBase), "DebugTest");
            }
            MyDefinitionManager.Static.SetEntityContainerDefinition(newDefinition);

            entityOb.SubtypeName = newDefinition.Id.SubtypeName;
            entityOb.PositionAndOrientation = new MyPositionAndOrientation(m_position, Vector3.Forward, Vector3.Up);

            MyEntity entity = MyEntities.CreateFromObjectBuilderAndAdd(entityOb);

            Debug.Assert(entity != null, "Entity wasn't created!");

            CloseScreen();
        }