public static Ammo GetLoadedAmmo(IMyCubeBlock weapon) { MyEntity entity = (MyEntity)weapon; if (!entity.HasInventory) { throw new InvalidOperationException("Has no inventory: " + weapon.getBestName()); } MyInventoryBase inv = entity.GetInventoryBase(0); if (inv.GetItemsCount() == 0) { return(null); } MyDefinitionId magazineId; try { magazineId = inv.GetItems()[0].Content.GetId(); } catch (IndexOutOfRangeException) // because of race condition { return(null); } return(GetAmmo(magazineId)); }